Skip to main content

Run the function only in a specific context

Project description

logo

Downloads Downloads codecov Hits-of-Code Test-Package Python versions PyPI version Checked with mypy Ruff

contextif: run the function only in a specific context

Sometimes we may need to run a function only if it happens in a strictly defined context. In this case, we can use this mini library.

Install it:

pip install contextif

And use:

from contextif import state

with state:
    state(print, 'hello,', 'world!')  # It will be printed.

state(print, "it's me, Mario!")  # It won't.

Using state as a function, you can pass another function and arguments to it there. It will be called only if it happens in a context created also using state. The function will not be called out of context.

For convenience, after the first import of state, this variable becomes builtin and you can access it in other modules of your program without importing.

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

contextif-0.0.5.tar.gz (3.9 kB view hashes)

Uploaded Source

Built Distribution

contextif-0.0.5-py3-none-any.whl (4.2 kB view hashes)

Uploaded Python 3

Supported by

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