contextvars-registry
is an extension for the Python's contextvars module.
In case you are not familiar with Context Variables, they work like Thread-Local storage, but better: they are both thread-safe and async task-safe, have snapshots (all existing vars copied in O(1) time), allowing to run functions/threads/asynctasks in the copied context snapshot.
The contextvars is a powerful module, but its API seems too low-level.
So this contextvars_registry
package provides some higher-level additions on top of the standard API, like, for example, grouping ContextVar objects in a registry class, with nice @property
-like access:
from contextvars_registry import ContextVarsRegistry
class CurrentVars(ContextVarsRegistry):
locale: str = 'en'
timezone: str = 'UTC'
current = CurrentVars()
# calls ContextVar.get() under the hood
current.timezone # => 'UTC'
# calls ContextVar.set() under the hood
current.timezone = 'GMT'
# ContextVar() objects can be reached as class members
CurrentVars.timezone.get() # => 'GMT'
That makes your code more readable (no more noisy .get()
calls), and it is naturally firendly to typing, so static code analysis features (like type checkers and auto-completion in your IDE) work nicely.
Check out the full documentation
- Read the Docs: https://contextvars-registry.readthedocs.io
- GitHub repository: https://github.com/vdmit11/contextvars-registry
- Python package: https://pypi.org/project/contextvars-registry/