Thread-safe context manager with nested inheritance and conflict resolution strategies for Python.
pip install threaded-contextfromthreaded_contextimport (
ThreadedContext, WeakThreadedContext,
get_current_context,
)
# Contexts nest and merge — inner values are visible alongside outer ones.# With ThreadedContext (strong), the outer value wins on conflict.withThreadedContext(knights='ni', eki='patang'):
print(get_current_context())
# {'eki': 'patang', 'knights': 'ni'}withThreadedContext(knights='round table', color='red'):
print(get_current_context())
# {'eki': 'patang', 'color': 'red', 'knights': 'ni'}print(get_current_context())
# {'eki': 'patang', 'knights': 'ni'}print(get_current_context())
# {}# With WeakThreadedContext, inner contexts can override the outer values.withWeakThreadedContext(knights='ni', eki='patang'):
print(get_current_context())
# {'eki': 'patang', 'knights': 'ni'}withThreadedContext(knights='round table', color='red'):
print(get_current_context())
# {'eki': 'patang', 'color': 'red', 'knights': 'round table'}print(get_current_context())
# {'eki': 'patang', 'knights': 'ni'}print(get_current_context())
# {}| Class | Behavior |
|---|---|
ThreadedContext | Strong — this level's values win over children's |
WeakThreadedContext | Weak — children can override this level's values |
BrutalThreadedContext | Strong + forcefully overrides parent values |
WeakBrutalThreadedContext | Weak + forcefully overrides parent values |
All context classes work as both context managers and decorators.
make test# run tests
make lint # pycodestyle, black, flake8, pylint, mypy
make build # clean + lint + test + build