Skip to main content

Runtime context

Project description

pip install runtime-context

RuntimeContext Example

from runtime_context import RuntimeContext

runtime_context = RuntimeContext()


def do_something(i):
    if runtime_context.dry_run:
        print('{} - dry run'.format(i))
    else:
        print('{} - for real'.format(i))


with runtime_context(dry_run=False):
    do_something(1)  # for real

    with runtime_context(dry_run=True):
        do_something(2)  # dry run

        runtime_context.dry_run = False
        do_something(3)  # for real

    with runtime_context():
        do_something(4)  # for real

        runtime_context.dry_run = True
        do_something(5)  # dry run

    do_something(6)  # for real

runtime_context_env Example

import json
from typing import Union  # noqa

from runtime_context import EnvBase, runtime_context_env  # noqa


@runtime_context_env
class YourApp:
    dry_run = False
    db_name = None
    config_file = None


env = YourApp()  # type: Union[YourApp, EnvBase]


@env.context_var_updated.listener(predicate=lambda name: name == 'config_file')
def reload_config():
    if not env.config_file:
        return

    print('Reloading config from {}'.format(env.config_file))
    with open(env.config_file) as f:
        config = json.load(f)
        for k, v in config.items():
            env.set(k, v)


with env():
    assert env.dry_run is False
    assert env.db_name is None

    env.config_file = 'config.json'  # prints 'Reloading config from config.json'

    assert env.db_name == 'products'  # read from config.json file

    with env(dry_run=True):
        assert env.dry_run is True

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

runtime-context-1.3.0.tar.gz (6.0 kB view details)

Uploaded Source

File details

Details for the file runtime-context-1.3.0.tar.gz.

File metadata

File hashes

Hashes for runtime-context-1.3.0.tar.gz
Algorithm Hash digest
SHA256 b6c7042729d4d702ff7900817f57de471af4af65a0dabc8678e7a28b4d00ad39
MD5 36423f996596f8c532de0f39edc4f6d0
BLAKE2b-256 4966828ad55324d10639f5a6c5d3c36c3d9ac14af94d943a9b9ef956cd581709

See more details on using hashes here.

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