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')

with use_context(something=123):
    value = nested_function()
    print(value) # 123

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.4.1.tar.gz (2.9 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.4.1-py3-none-any.whl (3.2 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: react_context-0.4.1.tar.gz
  • Upload date:
  • Size: 2.9 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.4.1.tar.gz
Algorithm Hash digest
SHA256 525891f9380861566357d973ce823407e766791df6ed2f1ef4804318c5112745
MD5 047dfbc45506aa2ba0db65484fc92dc7
BLAKE2b-256 1d60b8e3d5a0f363bc305c0bc7a3538440aad7defe005e228b44fcef7fe8c579

See more details on using hashes here.

File details

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

File metadata

  • Download URL: react_context-0.4.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.4.1-py3-none-any.whl
Algorithm Hash digest
SHA256 6d44a1d0e29f6742e16f157bcdfa07d77791c2f937d70ed2cd29c6853f345807
MD5 fc15302bd9abe340fb5d22607694e750
BLAKE2b-256 39fe617054c92a58a81aecca1d5c57a9d8dcad9a19de0fb9408518834bae2b30

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