Skip to main content

General utility functions with no dependencies.

Project description

Alexandra Institute Logo

Bits And Bobs

General utility functions with no dependencies.


Code Coverage Documentation License LastCommit Contributor Covenant

Developer:

Installation

Install the package via uv or pip:

uv add bits_and_bobs

or

pip install bits_and_bobs

Overview of the utility functions

The package currently includes the following utility functions:

cache_arguments

An extension of the functools.cache/functools.lru_cache decorators, which can also cache specific arguments of a function, rather than always including all arguments. This is useful if you have a function with some arguments that do not affect the output, for instance a logging message or a verbosity flag.

Here is an example of how to use it:

>>> import bits_and_bobs as bnb
>>>
>>> @bnb.cache_arguments(["x", "y"])
>>> def add(x: int, y: int, logging_message: str = "Computing...") -> int:
... print(logging_message)
... return x + y
>>>
>>> # This will compute and cache the result
>>> print(add(1, 2))
Computing...
3
>>>
>>> # This will use the cached result, since x and y are the same
>>> print(add(1, 2, logging_message="This will not be printed"))
3

no_terminal_output

A context manager that suppresses all terminal output. This blocks all Python output, but also all output from underlying C/C++/Fortran libraries. This is useful if you want to silence noisy libraries, for instance in Terminal User Interfaces (TUIs).

Use it like this:

>>> import bits_and_bobs as bnb
>>>
>>> print("This will be printed")
This will be printed
>>>
>>> with bnb.no_terminal_output():
...  print("This will NOT be printed")
>>>
>>> # We can specify a condition that disables the suppression
>>> def print_if_debug():
...  with bnb.no_terminal_output(disable_condition=lambda: os.getenv("DEBUG") == "1"):
...   print("This will be printed only if DEBUG=1")
>>> print_if_debug()
>>> os.environ["DEBUG"] = "1"
>>> print_if_debug()
This will be printed only if DEBUG=1

only_allow_specific_loggers

This function restricts logging output to only the specified loggers, suppressing all others. This is useful if you care about log messages from only a few specific loggers and want to ignore the rest, usually coming from third-party libraries.

Here is an example of how to use it:

>>> import bits_and_bobs as bnb
>>> import logging
>>>
>>> logger1 = logging.getLogger("logger1")
>>> logger2 = logging.getLogger("logger2")
>>>
>>> bnb.only_allow_specific_loggers(["logger1"])
>>>
>>> logger1.warning("This will be printed")
WARNING:logger1:This will be printed
>>>
>>> logger2.warning("This will NOT be printed")

Project details


Download files

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

Source Distribution

bits_and_bobs-0.1.0.tar.gz (44.8 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

bits_and_bobs-0.1.0-py3-none-any.whl (7.8 kB view details)

Uploaded Python 3

File details

Details for the file bits_and_bobs-0.1.0.tar.gz.

File metadata

  • Download URL: bits_and_bobs-0.1.0.tar.gz
  • Upload date:
  • Size: 44.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.9.8

File hashes

Hashes for bits_and_bobs-0.1.0.tar.gz
Algorithm Hash digest
SHA256 8ebf4e5c18814b4e2f6938ce0a406f2dae1e65a0d855cd7124192a79acf0baee
MD5 dd45ddb4cbc98f4a21612d0578383f13
BLAKE2b-256 80d977d7453070e3ea728a401ca173d712472814d6c659178e84b604781d6a9a

See more details on using hashes here.

File details

Details for the file bits_and_bobs-0.1.0-py3-none-any.whl.

File metadata

File hashes

Hashes for bits_and_bobs-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 79d58d5ddee986c1bc88c5ba8c7a129aafe645320de778af4b95b9f430073b65
MD5 fcbc17903e8ada1ea32f9baee1b48b1e
BLAKE2b-256 54725095f06d36f3deeed75d993e71f9b0e83b807d7d4af72b094062bf8440ef

See more details on using hashes here.

Supported by

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