Package: bx-py-utils
Boxine - bx_py_utilsVarious Python utility functionsQuickstartpipinstallbx_py_utilsExisting stuffHere only a simple list about existing utilities.
Please take a look into the sources and tests for deeper informations.bx_py_utils.anonymizeanonymize()- Anonymize the given string with special handling for eMail addresses.bx_py_utils.auto_docFnmatchExclude()- Helper for auto docexclude_functhat exclude files via fnmatch pattern.assert_readme()- Check and update README file with generate_modules_doc()assert_readme_block()- Check and update README file: Asset that "text_block" is present between the markers.generate_modules_doc()- Generate a list of function/class information via pdoc.get_code_location()- Return start and end line number for an object via inspect.bx_py_utils.aws.client_side_cert_managerClientSideCertManager()- Helper to manage client-side TLS certificate via AWS Secrets Manager bybx_py_utils.aws.secret_managerSecretsManager()- Access AWS Secrets Manager valuesbx_py_utils.compatremoveprefix()- Backport ofremoveprefixfrom PEP-616 (Python 3.9+)removesuffix()- Backport ofremovesuffixfrom PEP-616 (Python 3.9+)bx_py_utils.dict_utilsdict_get()- nested dictget()pluck()- Extract values from a dict, if they are presentbx_py_utils.doc_writeDoc-Write, see:https://github.com/boxine/bx_py_utils/blob/master/bx_py_utils/doc_write/README.mdbx_py_utils.environOverrideEnviron()- Context manager to change 'os.environ' temporarily.cgroup_memory_usage()- Returns the memory usage of the cgroup the Python interpreter is running in.bx_py_utils.error_handlingprint_exc_plus()- Print traceback information with a listing of all the local variables in each frame.bx_py_utils.file_utilsEmptyFileError()- 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 filenamewithoutany 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 sizesafe_filename()- Makes an arbitrary input suitable to be used as a filename.bx_py_utils.filename_matcherfilename_matcher()- Enhance fnmatch that accept a list of patterns.bx_py_utils.graphql_introspectionintrospection_query()- Generate GraphQL introspection query with variable nested depth.bx_py_utils.hash_utilsurl_safe_encode()- Encode bytes into a URL safe string.url_safe_hash()- Generate a URL safe hash withmax_sizefrom given string/bytes.bx_py_utils.html_utilsElementsNotFoundError()- Happens if requested HTML elements cannot be foundInvalidHtml()- XMLSyntaxError with better error messages: used in validate_html()get_html_elements()- Returns the selected HTML elements as stringpretty_format_html()- Pretty format given HTML document via BeautifulSoup (Needs 'beautifulsoup4' package)validate_html()- Validate a HTML document via XMLParser (Needs 'lxml' package)bx_py_utils.humanize.pformatpformat()- Format given object: Try JSON fist and fallback to pformat()bx_py_utils.humanize.timehuman_timedelta()- Converts a time duration into a friendly text representation.bx_py_utils.iterationchunk_iterable()- Returns a generator that yields slices of iterable of the givenchunk_size.bx_py_utils.pathChangeCurrentWorkDir()- Context Manager change the "CWD" to an other directory.MockCurrentWorkDir()- Context Manager to move the "CWD" to a temp directory.assert_is_dir()- Check if given path is a directoryassert_is_file()- Check if given path is a filebx_py_utils.processifyprocessify()- Decorator to run a function as a process.bx_py_utils.pyproject_tomlget_pyproject_config()- Get a config section from "pyproject.toml". The path can be optional specify.bx_py_utils.risonrison_dumps()- Encode as RISON, a URL-safe encoding format.bx_py_utils.stack_infoFrameNotFound()- Base class for lookup errors.last_frame_outside_path()- Returns the stack frame that is the direct successor of given "file_path".bx_py_utils.string_utilscompare_sentences()- Calculates the Levenshtein distance between text1 and text2. With filter functionality.ensure_lf()- Replace line endings to unix-style.get_words()- Extract words from a text. With filter functionality.levenshtein_distance()- Calculates the Levenshtein distance between two strings.startswith_prefixes()- >>> startswith_prefixes('foobar', prefixes=('foo','bar'))uuid_from_text()- Generate a UUID instance from the given text in a determinism may via SHA224 hash.bx_py_utils.test_utils.assertionassert_equal()- Check if the two objects are the same. Display a nice diff, usingpformat()assert_text_equal()- Check if the two text strings are the same. Display an error message with a diff.pformat_ndiff()- Generate andifffrom two objects, usingpformat()pformat_unified_diff()- Generate a unified diff from two objects, usingpformat()text_ndiff()- Generate andiffbetween two text strings.text_unified_diff()- Generate a unified diff between two text strings.bx_py_utils.test_utils.context_managersMassContextManager()- A context manager that enter/exit a list of mocks.bx_py_utils.test_utils.datetimeparse_dt()- Helper for easy generate adatetimeinstance via string.bx_py_utils.test_utils.deny_requestsDenyAnyRealRequestContextManager()- Context manager that denies any request via docket/urllib3. Will raise DenyCallError.deny_any_real_request()- Deny any request via docket/urllib3. Useful for tests, because they should mock all requests.bx_py_utils.test_utils.filesystem_utilsFileWatcher()- Helper to record which new files have been created.bx_py_utils.test_utils.log_utilsNoLogs()- Context manager to Suppress all logger outputsRaiseLogUsage()- A log handler, that raise an error on every log output.bx_py_utils.test_utils.mock_aws_secret_managerSecretsManagerMock()- Mock forbx_py_utils.aws.secret_manager.SecretsManager()bx_py_utils.test_utils.mock_boto3sessionMockedBoto3Session()- Mock forboto3.session.Session()bx_py_utils.test_utils.mock_uuidMockUUIDGenerator()- Helper to mockuuid.uuid4()with reproducible results (e.g. for snapshot tests)bx_py_utils.test_utils.mocks3A simple mock for Boto3's S3 modules.PseudoS3Client()- Simulates a boto3 S3 client object in testsbx_py_utils.test_utils.redirectRedirectOut()- Redirect stdout + stderr into a buffer (with optional strip the output)bx_py_utils.test_utils.requests_mock_assertionassert_json_requests_mock()- Check the requests mock history. In this case all requests must be JSON.assert_json_requests_mock_snapshot()- Check requests mock history via snapshot. Accepts only JSON requests.assert_requests_mock()- Check the requests mock history. Accept mixed "text" and "JSON".assert_requests_mock_snapshot()- Check requests mock history via snapshot. Accept mixed "text" and "JSON".bx_py_utils.test_utils.snapshotAssert complex output via auto updated snapshot files with nice diff error messages.SnapshotChanged()- Assertion failed.assert_binary_snapshot()- Assert binary data via snapshot fileassert_html_snapshot()- Assert "html" string via snapshot file with validate and pretty formatassert_py_snapshot()- Assert complex python objects vio PrettyPrinter() snapshot file.assert_snapshot()- Assert given data serialized to JSON snapshot file.assert_text_snapshot()- Assert "text" string via snapshot fileget_snapshot_file()- Generate a file path use stack information to fill not provided path components.bx_py_utils.test_utils.timeMockTimeMonotonicGenerator()- Helper to mocktime.monotonic()in tests.bx_py_utils.test_utils.unittest_utilsBaseDocTests()- 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_toolscutout()- Mark a point in a long text by line no + column with context lines around.Notes about snapshotQuick 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 setRAISE_SNAPSHOT_ERRORS=0in your environment.e.g.:RAISE_SNAPSHOT_ERRORS=0python3-munittestRenew all snapshot files with:makeupdate-test-snapshot-filesBackwards-incompatible changesv36 -> v37 - Outsourcing Django stuffWe splitbx_py_utilsand moved all Django related utilities into the separated project:https://github.com/boxine/bx_django_utilsSo,bx_py_utilsis better usable in non-Django projects, because Django will not installed as decency of "bx_py_utils"developingTo start developing e.g.:~$gitclonehttps://github.com/boxine/bx_py_utils.git
~$cdbx_py_utils
~/bx_py_utils$make
install-poetryinstallorupdatepoetry
installinstallprojectviapoetry
updateupdatethesourcesandinstallationandgenerate"conf/requirements.txt"lintRuncodeformattersandlinter
fix-code-styleFixcodeformatting
tox-listenvsListalltoxtestenvironments
toxRuntestsviatoxwithallenvironmentstestRuntests
coverageRuntestswithcoverage
update-test-snapshot-filesUpdateallsnapshotfiles(byremoveandrecreateallsnapshotfiles)mypyRunmypy
safetyRunhttps://github.com/pyupio/safety
publishReleasenewversiontoPyPi
cleanRemovecreatedfilesfromthetestprojectLicenseMIT. Patches welcome!About usWe’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/Linkshttps://pypi.org/project/bx-py-utils/