Skip to main content

An API to pass data in the nested functions 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 create_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-1.0.0.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-1.0.0-py3-none-any.whl (3.3 kB view details)

Uploaded Python 3

File details

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

File metadata

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

Hashes for react_context-1.0.0.tar.gz
Algorithm Hash digest
SHA256 e101e74b19277cecef17490945bf3b0bf3ad39f8ea23b090120096ce40add2e5
MD5 cb3aaa4167b1a4c696670d126906d085
BLAKE2b-256 737602e219c6d84fdad7a07ade72677940d90c58a4ba7a88268210b23c727c4f

See more details on using hashes here.

File details

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

File metadata

  • Download URL: react_context-1.0.0-py3-none-any.whl
  • Upload date:
  • Size: 3.3 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-1.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 42af70da8c8e5c5d66018717dbf703330ae9a84d823b37f39fb01b49e2fa7411
MD5 d20f1ad29bc7e2a7e2a4673c0f21e622
BLAKE2b-256 1e5dc506f070a7f9d5cb0d06abd8ca8683aad9e25c3b7ca8d621b1cc0060157e

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