Skip to content

Repository files navigation

threaded-context

Thread-safe context manager with nested inheritance and conflict resolution strategies for Python.

Install

pip install threaded-context

Usage

fromthreaded_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())
# {}

Context types

ClassBehavior
ThreadedContextStrong — this level's values win over children's
WeakThreadedContextWeak — children can override this level's values
BrutalThreadedContextStrong + forcefully overrides parent values
WeakBrutalThreadedContextWeak + forcefully overrides parent values

All context classes work as both context managers and decorators.

Development

make test# run tests
make lint # pycodestyle, black, flake8, pylint, mypy
make build # clean + lint + test + build

About

Very simple context tracking decorator / context handler.

Topics

Resources

Stars

1 star

Watchers

6 watching

Forks

Used by

Contributors

Languages