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.

origin/main

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.0.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.0-py3-none-any.whl (3.7 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: react_context-0.4.0.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.0.tar.gz
Algorithm Hash digest
SHA256 76feb12e9f1b2fc33018f1cb4e91c81ba4e1a77ba355723506d47f3e690e6b97
MD5 c66087d041fafcf06aca13c590c42e0f
BLAKE2b-256 bfedd971ce079615a22e5d1d067b4e1edeb2f91f0a44349335f9047c5234142b

See more details on using hashes here.

File details

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

File metadata

  • Download URL: react_context-0.4.0-py3-none-any.whl
  • Upload date:
  • Size: 3.7 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.0-py3-none-any.whl
Algorithm Hash digest
SHA256 786d086f3d5bc51a7d7f7608f89623ff67144d67b94d8a009eb2be4e00a2b3aa
MD5 5ee2dc050ff812cc4308fee391381548
BLAKE2b-256 b2826272d237d23e0fb9ce5354e60b4e612af0bce21cd573dbeb978630fca520

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