Skip to main content

Various Python utility functions

Project description

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.

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

  • dict_get() - nested dict get()
  • pluck() - Extract values from a dict, if they are present

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

  • 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

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.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.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 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                       Run code formatters and linter
fix-code-style             Fix code formatting
tox-listenvs               List all tox test environments
tox                        Run tests via tox 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
safety                     Run https://github.com/pyupio/safety
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

Project details


Release history Release notifications | RSS feed

This version

94

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

bx_py_utils-94.tar.gz (180.0 kB view details)

Uploaded Source

Built Distribution

bx_py_utils-94-py3-none-any.whl (160.0 kB view details)

Uploaded Python 3

File details

Details for the file bx_py_utils-94.tar.gz.

File metadata

  • Download URL: bx_py_utils-94.tar.gz
  • Upload date:
  • Size: 180.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.1 CPython/3.12.3

File hashes

Hashes for bx_py_utils-94.tar.gz
Algorithm Hash digest
SHA256 cb8c1f32d67d152fa5fd355503a3f69ddc5a6b5d79fee1acc16f9d55a5605130
MD5 e749d64ea1cc7bdb1ff050d20c8427a0
BLAKE2b-256 30e94f4615efcc0107aefb7b73a16cb8057d982d08f221c0455da946ccca4f61

See more details on using hashes here.

File details

Details for the file bx_py_utils-94-py3-none-any.whl.

File metadata

  • Download URL: bx_py_utils-94-py3-none-any.whl
  • Upload date:
  • Size: 160.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.1 CPython/3.12.3

File hashes

Hashes for bx_py_utils-94-py3-none-any.whl
Algorithm Hash digest
SHA256 24b26873e66a1ef61e0f2b709f9a1a2b90a44bf29e86ad72be193c1dd49b17fe
MD5 46f5b6f3b48c64e94e1333012d80df22
BLAKE2b-256 bac0c4d82b1fd66eca67fe48fa49ba932d0809ca120bfb8a01c54844f59aeb29

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page