跳转到主要内容

运行时上下文

项目描述

***************
runtime-context
***************

.. code-block:: shell

pip install runtime-context


--------------------------
``RuntimeContextWrapper`` 示例
--------------------------

.. code-block:: python

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`` 示例
-------------------------------

.. code-block:: python

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)
断言 env.dry_run 为 True

项目详情


下载文件

下载适用于您平台的文件。如果您不确定选择哪个,请了解更多关于 安装包 的信息。

源代码发行版

runtime-context-3.0.0.tar.gz (7.9 kB 查看哈希)

上传时间 源代码

由...