A collection of Python utility functions by Tenhil GmbH & Co. KG
Project description
baumbelt
Curated collection of handy utility functions for Python by the Tenhil GmbH.
Installation
Run pip install baumbelt
Utilities
EnumContainsMeta
baumbelt.enum.EnumContainsMeta offers a metaclass, that adds the syntactic sugar of member checks. The default Enum only allows checks for values:
from enum import Enum
from baumbelt.enum import EnumContainsMeta
class AtomEnum(Enum, metaclass=EnumContainsMeta):
hydrogen = 1
helium = 2
"hydrogen" in AtomEnum # True
2 in AtomEnum # True
"water" in AtomEnum # False
MeasureTime
The baumbelt.time.MeasureTime class can be used as a context manager to have a syntactically nice way to measure the time a block of code takes.
The following two snippets produce the same result.
Vanilla:
from datetime import datetime
t0 = datetime.now()
this_call_takes_a_while()
tend = datetime.now() - t0
print(f"{tend} ({tend.total_seconds()}s)")
and with MeasureTime:
from baumbelt.time import MeasureTime
with MeasureTime() as mt:
this_call_takes_a_while()
print(mt)
HuggingLog
baumbelt.logging.HuggingLog offers a convenient way to print the duration a specific code block took. It utilizes MeasureTime and adds a bit of printing around it. You can also pass
a different logging function, for instance logger.debug. This comes especially comes in handy, if you run in detached environment (eg: cronjob).
from baumbelt.logging import HuggingLog
import logging
logger = logging.getLogger(__name__)
with HuggingLog("cross-compile doom", logging_fn=logger.debug, prefix="[ARM]"):
# compile hard
...
This outputs something like:
(2629) [DEBUG] 2024-05-28 14:49:51,616 - logging#32 - [ARM]: Start 'cross-compile doom'...
(2629) [DEBUG] 2024-05-28 14:49:53,616 - logging#41 - [ARM]: Finish 'cross-compile doom' in 0:00:02.000204 (2.000204s total)
Vigilant readers may notice the log-origin "logging#32" and "logging#41". These origins are from inside the utility and probably not very useful. You can circumvent this by passing a lambda:
with HuggingLog(..., logging_fn=lambda s: logger.debug(s)):
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file baumbelt-0.1.1.tar.gz.
File metadata
- Download URL: baumbelt-0.1.1.tar.gz
- Upload date:
- Size: 8.1 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.1.0 CPython/3.12.3
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
5864f527c29757314712effd9748146a877caf9d88d379bd67694606b318d8b6
|
|
| MD5 |
48614fe771ce0d8b89acd0a81059e6bc
|
|
| BLAKE2b-256 |
d1915a2c58eebd2a492a5eeb2c18793bc962054a48f1d2db9c598b52f13cab66
|
File details
Details for the file baumbelt-0.1.1-py3-none-any.whl.
File metadata
- Download URL: baumbelt-0.1.1-py3-none-any.whl
- Upload date:
- Size: 7.8 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.1.0 CPython/3.12.3
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
81be45485f5775de287cfc38df0668d82c1f4fb3a05e50a39ec1c882751a70c2
|
|
| MD5 |
e08f605d2bbd9479df51ed320669afe7
|
|
| BLAKE2b-256 |
9a88b65a2611681a89f714a9bd3d41737903d08ed02ea91005e4aa03540e8492
|