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")

timeout

This context manager limits the execution time of a block of code. If the block exceeds the specified time limit, a TimeoutError is raised. This is useful for preventing long-running operations from hanging your program.

Here is an example of how to use it:

>>> import bits_and_bobs as bnb
>>> import time
>>> try:
...  with bnb.timeout(seconds=2):
...   time.sleep(3)  # This will take longer than 2 seconds
... except TimeoutError:
...  print("The operation timed out!")
The operation timed out!

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.2.0.tar.gz (45.5 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.2.0-py3-none-any.whl (8.9 kB view details)

Uploaded Python 3

File details

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

File metadata

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

File hashes

Hashes for bits_and_bobs-0.2.0.tar.gz
Algorithm Hash digest
SHA256 5fe5957f08686eaff11419de780160de577d137fc82566dc1ebe0d63b0a93066
MD5 d7655bceaa088f31fbd77d3842203a1b
BLAKE2b-256 ac5c1d7a5fa08b406f18e504bd2a60d373b8d46fd864e7b838cb8259f4a8f82a

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for bits_and_bobs-0.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 2f2cb023f2503077a61176b8ed4fec919b3d2da3c0fdd792c56dd1ccce252213
MD5 2daaca3f050d5b7a8e9ec93bdb5c7456
BLAKE2b-256 a69796fb9bfffe89014a21fa6c0881e569c249ac579cbd6619cef4eb21bf715d

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