Contextvars made easy (WARNING: unstable alpha version. Things may break).
Project description
Warning!
The code is at the early development stage, and may be unstable. Use with caution.
contextvars-extras is a set of extensions for the Python’s contextvars module.
In case you are not familiar with the contextvars module, its ContextVar objects work like Thread-Local storage, but better: they are both thread-safe and async task-safe, and they can be copied (all existing vars copied in O(1) time), and then you can run a function in the copied and isolated context.
The contextvars is a powerful module, but its API seems too low-level.
So this contextvars_extras package provides some higher-level additions on top of the standard API, like, for example, organizing ContextVar objects into registry classes, with nice @property-like access:
from contextvars_extras 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 lass 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
Links
Read the Docs: https://contextvars-extras.readthedocs.io
GitHub repository: https://github.com/vdmit11/contextvars-extras
Python package: https://pypi.org/project/contextvars-extras/
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
Hashes for contextvars-extras-0.0.10.tar.gz
Algorithm | Hash digest | |
---|---|---|
SHA256 | 4a8ab80d0eec9265f59b034d381607c75d0861abca46fb02d39cca6ef52cc183 |
|
MD5 | 27b57a176cadc9bbbc7f767ed6c7863b |
|
BLAKE2b-256 | 6662703ef6af6da2b9ec37347b1215c0f6c9bcb24e1dd6fa4cc0fec4e5f6e16f |
Hashes for contextvars_extras-0.0.10-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | bc00d23a2b71919ac4ca21dc059c946d6c0dc8d889b7b33921c4db61b5e39e9e |
|
MD5 | 8773fd9e46346f116b52716c28f7eb0e |
|
BLAKE2b-256 | 71cac5b4a7ca8243a8b33e868693be266f7ea171877cb5b19096c62d464fc0f3 |