通用异步服务加载程序和依赖注入程序
buvar的Python项目详细描述
插件机制
我想要一个类似于Pyramidaproach的插件机制。它应该 提供生成要运行的任意任务的方法,其中每个生命周期阶段 应该交给开发人员控制。
引导程序如下:
frombuvarimportplugin,componentsplugin.run("some.module.with.plugin.function")
# some.module.with.plugin.functionfrombuvarimportcontext# you may omit include in argumentsasyncdefplugin(include):awaitinclude('.another.plugin')# create some long lasting componentsmy_component=context.add("some value")asyncdeftask():asyncio.sleep(1)asyncdefserver():awaitasyncio.Future()# you may run simple tasksyieldtask()# you may run server tasksyieldserver()
组件和依赖注入解决方案
我想要一些有用的东西来储存一些长期的帮助我 商业问题。我想要一个不冗长的查找。
frombuvarimportdiclassBar:passclassFoo:def__init__(self,bar:Bar=None):self.bar=bar@di.adapter_classmethodasyncdefadapt(cls,baz:str)->Foo:returnFoo()@di.adapterasyncdefadapt(bar:Bar)->Foofoo=Foo(bar)returnfooasyncdeftask():foo=awaitdi.nject(Foo,baz="baz")assertfoo.barisNonebar=Bar()foo=awaitdi.nject(Foo,bar=bar)assertfoo.barisbar
配置源
我想要一个配置源,它自动应用环境 变量设置为默认值。
配置toml
log_level="DEBUG"show_warnings="yes"[foobar]some="value"
exportAPP_FOOBAR_SOME=thing
importattrimporttomlfrombuvarimportconfig@attr.s(auto_attribs=True)classGeneralConfig:log_level:str="INFO"show_warnings:bool=config.bool_var(False)@attr.s(auto_attribs=True)classFoobarConfig:some:strsource=config.ConfigSource(toml.load('config.toml'),env_prefix="APP")general_config=source.load(GeneralConfig)assertgeneral_config==GeneralConfig(log_level="DEBUG",show_warnings=True)foobar_config=source.load(FoobarConfig,'foobar')assertfoobar_config.some=="thing"
结构日志
我想在我的终端中有一个漂亮且可读的structlog和一个json登录 生产。
importsysfrombuvarimportloglog.setup_logging(sys.stdout.isatty(),general_config.log_level)