Skip to main content

Boxine - bx_py_utils

Various Python utility functions

tests codecov bx_py_utils @ PyPi Python Versions License MIT

Quickstart

pip install bx_py_utils

Existing stuff

Here only a simple list about existing utilities. Please take a look into the sources and tests for deeper informations.

bx_py_utils.anonymize

  • anonymize() - Anonymize the given string with special handling for eMail addresses and the possibility to truncate the output.
  • anonymize_dict() - Returns a new dict with anonymized values for keys containing one of the given keywords.

bx_py_utils.auto_doc

bx_py_utils.aws.client_side_cert_manager

bx_py_utils.aws.secret_manager

bx_py_utils.compat

bx_py_utils.dict_utils

bx_py_utils.doc_write

Doc-Write, see: https://github.com/boxine/bx_py_utils/blob/master/bx_py_utils/doc_write/README.md

bx_py_utils.environ

bx_py_utils.error_handling

  • exception2str() - Converts any exception into a short "ClassName: message" string or just "ClassName" if the message is empty.
  • print_exc_plus() - Print traceback information with a listing of all the local variables in each frame.

bx_py_utils.file_utils

  • EmptyFileError() - Will be raised from get_and_assert_file_size() if a 0-bytes file was found.
  • FileError() - Base error class for all 'file_utils' exceptions.
  • FileHasher() - Context Manager for generate different hashes from file content while processing a file.
  • FileSizeError() - File size is not the same as the expected size.
  • NamedTemporaryFile2() - Generates a temp file with the given filename without any random name sequence.
  • OverlongFilenameError() - cut_filename() error: The file name can not be shortened, because sterm is to short.
  • TempFileHasher() - File like context manager that combines NamedTemporaryFile2 and FileHasher.
  • cut_filename() - Short the file name (and keep the last suffix). Raise OverlongFilenameError if it can't fit.
  • get_and_assert_file_size() - Check file size of given file object. Raise EmptyFileError for empty files or return size
  • safe_filename() - Makes an arbitrary input suitable to be used as a filename.

bx_py_utils.filename_matcher

bx_py_utils.graphql_introspection

bx_py_utils.hash_utils

  • collect_hashes() - Get all hash values from a dictionary. Use hashlib.algorithms_available for key names.
  • compare_hashes() - Compare hashes from two dictionaries. Return DictCompareResult with the results.
  • url_safe_encode() - Encode bytes into a URL safe string.
  • url_safe_hash() - Generate a URL safe hash with max_size from given string/bytes.

bx_py_utils.html_utils

bx_py_utils.humanize.pformat

  • pformat() - Format given object: Try JSON fist and fallback to pformat()

bx_py_utils.humanize.time

bx_py_utils.import_utils

  • import_all_files() - Helper to import all Python files from a package. Helpful for registry via imports.
  • import_string() - Import a dotted module path and return the attribute/class designated by the last name in the path.

bx_py_utils.iteration

  • chunk_iterable() - Returns a generator that yields slices of iterable of the given chunk_size.

bx_py_utils.path

bx_py_utils.processify

bx_py_utils.pyproject_toml

bx_py_utils.rison

bx_py_utils.stack_info

bx_py_utils.string_utils

bx_py_utils.test_utils.assertion

bx_py_utils.test_utils.context_managers

bx_py_utils.test_utils.datetime

  • parse_dt() - Helper for easy generate a datetime instance via string.

bx_py_utils.test_utils.deny_requests

bx_py_utils.test_utils.filesystem_utils

  • FileWatcher() - Helper to record which new files have been created.

bx_py_utils.test_utils.log_utils

  • NoLogs() - Context manager to Suppress all logger outputs
  • RaiseLogUsage() - A log handler, that raise an error on every log output.

bx_py_utils.test_utils.mock_aws_secret_manager

bx_py_utils.test_utils.mock_boto3session

bx_py_utils.test_utils.mock_uuid

  • MockUUIDGenerator() - Helper to mock uuid.uuid4() with reproducible results (e.g. for snapshot tests)

bx_py_utils.test_utils.mocks3

A simple mock for Boto3's S3 modules.

bx_py_utils.test_utils.redirect

  • RedirectOut() - Redirect stdout + stderr into a buffer (with optional strip the output)

bx_py_utils.test_utils.requests_mock_assertion

bx_py_utils.test_utils.snapshot

Assert complex output via auto updated snapshot files with nice diff error messages.

bx_py_utils.test_utils.time

bx_py_utils.test_utils.unittest_utils

  • BaseDocTests() - Helper to include all doctests in unittests, without change unittest setup. Just add a normal TestCase.
  • assert_no_flat_tests_functions() - Check if there exists normal test functions (That will not be executed by normal unittests)

bx_py_utils.test_utils.xlsx

  • FreezeXlsxTimes() - Context manager / decorator intended to freeze timestamps of xlsx files creation by e.g.: openpyxl.
  • generate_xlsx_md_snapshot() - Generate a markdown snapshot of a XLSX: Display ZIP info + Sheets content as Markdown.
  • xlsx2dict() - Convert a XLSX file content into a dictionary: Every sheet is a key, and the value is a list of dictionaries.
  • xlsx2markdown() - Convert all Sheets of a XLSX into markdown tables.

bx_py_utils.test_utils.zip_file_utils

  • FreezeZipFileDatetime() - Context manager / decorator to freezes the modification time of files written to a zip file.
  • zip_info() - Generates similar information than unzip -v: Yields ZipFileInfo for each file in the zip file.
  • zip_info_markdown() - Generates a markdown representation of the zip file content. Similar to unzip -v output.

bx_py_utils.text_tools

  • cutout() - Mark a point in a long text by line no + column with context lines around.

Notes about snapshot

Quick hint about snapshot. If you have many snapshots in your project and you need to change many with a code change, then you can run the tests without a snapshot change leading to an error, by set RAISE_SNAPSHOT_ERRORS=0 in your environment.

e.g.:

RAISE_SNAPSHOT_ERRORS=0 python3 -m unittest

Renew all snapshot files with:

make update-test-snapshot-files

Backwards-incompatible changes

v36 -> v37 - Outsourcing Django stuff

We split bx_py_utils and moved all Django related utilities into the separated project:

So, bx_py_utils is better usable in non-Django projects, because Django will not installed as decency of "bx_py_utils"

developing

To start developing, just run make install to create a .venv and install all needed packages. The minimal requirements are only python3-venv and python3-pip (uv will be installed via pip in .venv)

e.g.:

~$ git clone https://github.com/boxine/bx_py_utils.git
~$ cd bx_py_utils
~/bx_py_utils$ make
help                       List all commands
install-base-req           Install needed base packages via apt
install                    Install the project in a Python virtualenv
update-requirements        Update requirements
lint                       Check/fix code style by running: "ruff check"
lint-fix                   Check/fix code style by running: "ruff check --fix"
nox                        Run tests via nox with all environments
test                       Run tests
coverage                   Run tests with coverage
update-test-snapshot-files Update all snapshot files (by remove and recreate all snapshot files)
mypy                       Run mypy
pip-audit                  Run https://github.com/pypa/pip-audit
publish                    Release new version to PyPi
clean                      Remove created files from the test project

Create a release

  • Increase verion number in bx_py_utils/__init__.py
  • Create pull request
  • After merge, call: make publish

License

MIT. Patches welcome!

About us

We’ve been rethinking the listening experience for kids and have created an ecosystem where haptic and listening experience are combined via smart technology - the Toniebox.

We are constantly looking for engineers to join our team in different areas. If you’d be interested in contributing to our platform, have a look at: https://tonies.com/jobs/

Links

Metadata

Release files for bx-py-utils 120

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for bx-py-utils 120
File Size Uploaded
bx_py_utils-120.tar.gz 148.8 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for bx-py-utils 120
File Interpreter ABI Platform
bx_py_utils-120-py3-none-any.whl Python 3 none any Details

Total release size: 211.0 kB

Release files / bx_py_utils-120.tar.gz

Download URL bx_py_utils-120.tar.gz
Size 148.8 kB
Tags Source
SHA-256 checksum
How to use checksums
b7bc3c28b22509b6df3519b9198dedcaa81c6e6b7db25efd48c1cd1675658190
BLAKE2b-256 checksum
How to use checksums
d2a23c061a7637f15704f998527b274ea3848b5e17944854e3facf7c3602c329
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.14.4

Release files / bx_py_utils-120-py3-none-any.whl

Download URL bx_py_utils-120-py3-none-any.whl
Size 62.2 kB
Tags Python 3
SHA-256 checksum
How to use checksums
3bc709ddb0fca8dc0fa4dfa9a351f0f90336b61796dbe7f5bbce432041c08ea8
BLAKE2b-256 checksum
How to use checksums
15f6a4cb6c7ae807e791b0143697359c474bc29948fe9a7e43177c3ece6c8798
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.14.4

Release history Release notifications | RSS feed

121

2 release files

This release

120 This release

2 release files

119

2 release files

118

2 release files

116

2 release files

115

2 release files

114

2 release files

113

2 release files

111

2 release files

110

2 release files

109

2 release files

108

2 release files

106

2 release files

105

2 release files

104

2 release files

103

2 release files

102

2 release files

101

2 release files

100

2 release files

99

2 release files

98

2 release files

97

2 release files

96

2 release files

95

2 release files

94

2 release files

93

2 release files

92

2 release files

91

2 release files

90

2 release files

89

2 release files

88

2 release files

87

2 release files

86

2 release files

85

2 release files

84

2 release files

83

2 release files

82

2 release files

81

2 release files

80

2 release files

79

2 release files

78

2 release files

76

2 release files

75

2 release files

74

2 release files

73

2 release files

72

2 release files

71

2 release files

70

2 release files

69

2 release files

68

2 release files

67

2 release files

66

2 release files

65

2 release files

64

2 release files

63

2 release files

62

2 release files

61

2 release files

60

2 release files

59

2 release files

58

2 release files

57

2 release files

56

2 release files

55

2 release files

54

2 release files

53

2 release files

52

2 release files

51

2 release files

50

2 release files

49

2 release files

47

2 release files

46

2 release files

45

2 release files

44

2 release files

43

2 release files

42

2 release files

41

2 release files

40

2 release files

39

2 release files

38

2 release files

37

2 release files

36

2 release files

35

2 release files

34

2 release files

33

2 release files

32

2 release files

31

2 release files

30

2 release files

29

2 release files

28

2 release files

27

2 release files

26

2 release files

25

2 release files

24

2 release files

23

2 release files

22

2 release files

21

2 release files

20

2 release files

19

2 release files

18

2 release files

17

2 release files

16

2 release files

15

2 release files

14

2 release files

13

2 release files

12

2 release files

11

2 release files

10

2 release files

9

2 release files

8

2 release files

7

2 release files

5

2 release files

0.0.4

2 release files

0.0.3

2 release files

0.0.2

2 release files

0.0.1

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page