Giter Club home page Giter Club logo

runtime-context's Introduction

runtime-context

pip install runtime-context

RuntimeContextWrapper Example

from runtime_context import RuntimeContextWrapper

runtime_context = RuntimeContextWrapper()


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_set.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

runtime-context's People

Contributors

jbasko avatar

Stargazers

 avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.