from runtime_context import RuntimeContextWrapper
runtime_context = RuntimeContextWrapper()
defdo_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 realwith runtime_context(dry_run=True):
do_something(2) # dry run
runtime_context.dry_run =False
do_something(3) # for realwith 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 # noqafrom runtime_context import EnvBase, runtime_context_env # noqa@runtime_context_envclassYourApp:
dry_run =False
db_name =None
config_file =None
env = YourApp() # type:Union[YourApp,EnvBase]@env.context_var_set.listener(predicate=lambdaname: name =='config_file')
defreload_config():
ifnot env.config_file:
returnprint('Reloading config from {}'.format(env.config_file))
withopen(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 isFalseassert env.db_name isNone
env.config_file ='config.json'# prints 'Reloading config from config.json'assert env.db_name =='products'# read from config.json filewith env(dry_run=True):
assert env.dry_run isTrue
The Tidelift Subscription provides access to a continuously curated stream of human-researched and maintainer-verified data on open source packages and their licenses, releases, vulnerabilities, and development practices.