Skip to main content

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

react_context-0.5.1.tar.gz (3.0 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

react_context-0.5.1-py3-none-any.whl (3.2 kB view details)

Uploaded Python 3

File details

Details for the file react_context-0.5.1.tar.gz.

File metadata

  • Download URL: react_context-0.5.1.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

Hashes for react_context-0.5.1.tar.gz
Algorithm Hash digest
SHA256 1633a8bb828f0aa1fe491acbb8c8b797674d4947118d0514ee86b4a3dbd7d8b9
MD5 51a568f18ccbb5c49e2c9ff13f4bfb11
BLAKE2b-256 4645bf980f5eb680e9fd2618219e7aa6a3efd2aed9187e1b63041b7c0cc367f7

See more details on using hashes here.

File details

Details for the file react_context-0.5.1-py3-none-any.whl.

File metadata

  • Download URL: react_context-0.5.1-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

Hashes for react_context-0.5.1-py3-none-any.whl
Algorithm Hash digest
SHA256 f7e20f84ea75e886ab1ca41cf67b8acd0cf4c864d628648b7f5947f7e90bcfa5
MD5 888a2332db5060accd7ee55f9772db53
BLAKE2b-256 8944fe7dc21ddb85be79a9225eabfb8d81ba1daee291b490fb7bb125eaf5c893

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page