An API to pass data in the sub-function inspired by React Context
Project description
react_context
An API to pass data down in the sub-function inspired by React Context
pip install react-context
Inspired by this reddit, here a React Context-like API for Python to pass data down in thesub-function without polluting all the function parameters.
Under the hood it uses inspect.stack().frame to register the element in the context manager. Frames may be available only in CPython.
Define context values
To define a context value use use_context(**kargws) function
with use_context(something=123):
...
Retrieve the value somewhere in the code inside of the context
To retrieve the value, use get_context(key) function
Complete example
def nested_function():
return get_context('something', 'default_value')
with use_context(something=123):
value = nested_function()
print(value) # 123
print(nested_function()) # 'default_value'
How to debug the context
To print in the console where a context has been defined in the stacktrace, replace get_context with debug_context(key, logger=print)
For the complete implementation and behaviour, please check the test file.
Project details
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
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file react_context-0.5.0.tar.gz.
File metadata
- Download URL: react_context-0.5.0.tar.gz
- Upload date:
- Size: 3.0 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.2 CPython/3.10.8
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
993d36a5b9b48e51261e621a7945d47867815c6414fb2c5f7baea54e453e5030
|
|
| MD5 |
c03ed78cf91c2c52acbc4f766e20d208
|
|
| BLAKE2b-256 |
95863499fc04e06525df4580dd0a01bfc1c795860727610b8c81d102ff7b3871
|
File details
Details for the file react_context-0.5.0-py3-none-any.whl.
File metadata
- Download URL: react_context-0.5.0-py3-none-any.whl
- Upload date:
- Size: 3.2 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.2 CPython/3.10.8
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
b2b6e517523dab120be07fcf90bfd8c0afd40476e9d136bf1d47e3292f3d8ae9
|
|
| MD5 |
d727583c6267e96f47fa2972e5f40e3c
|
|
| BLAKE2b-256 |
5df459a329bcf54b68a2e225760922ed6fd54d0688fcf4bf5ba98709b902d33a
|