Skip to main content
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!

Metadata

Release files for bits_and_bobs 0.2.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for bits_and_bobs 0.2.0
File Size Uploaded
bits_and_bobs-0.2.0.tar.gz 45.5 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for bits_and_bobs 0.2.0
File Interpreter ABI Platform
bits_and_bobs-0.2.0-py3-none-any.whl Python 3 none any Details

Total release size: 54.4 kB

Release files / bits_and_bobs-0.2.0.tar.gz

Download URL bits_and_bobs-0.2.0.tar.gz
Size 45.5 kB
Tags Source
SHA-256 checksum
How to use checksums
5fe5957f08686eaff11419de780160de577d137fc82566dc1ebe0d63b0a93066
BLAKE2b-256 checksum
How to use checksums
ac5c1d7a5fa08b406f18e504bd2a60d373b8d46fd864e7b838cb8259f4a8f82a
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via uv/0.9.8

Release files / bits_and_bobs-0.2.0-py3-none-any.whl

Download URL bits_and_bobs-0.2.0-py3-none-any.whl
Size 8.9 kB
Tags Python 3
SHA-256 checksum
How to use checksums
2f2cb023f2503077a61176b8ed4fec919b3d2da3c0fdd792c56dd1ccce252213
BLAKE2b-256 checksum
How to use checksums
a69796fb9bfffe89014a21fa6c0881e569c249ac579cbd6619cef4eb21bf715d
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via uv/0.9.8

Release history Release notifications | RSS feed

This release

0.2.0 This release

2 release files

0.1.0

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page