在运行时解决缺少的参数

argresolver的Python项目详细描述


argresolver v0.3.3

Build Status

解析器是一个简单的装饰器,用于在运行时解析(缺少)参数。 它执行各种任务,从查找环境变量作用域中的参数到简单的服务依赖注入。

一。Resolver
1.1条。Environment
1.2条。Map
1.3条。Chain

一。分解器

1.1条。环境

# We inject arguments from the environment variables scope to a simple function# We use a `prefix` to minimize clashes with other components# username will have a correponding DB_USERNAME, same for password and databasefromargresolverimportEnvironmentfromargresolver.utilsimportmodified_environ# We use it to alter the environment variables@Environment()defconnect(host,user,password):print("Connecting: {user}:{password}@{host}".format(**locals()))withmodified_environ(PASSWORD='my_pass'):connect('localhost','admin')# Prints: Connecting: admin:my_pass@localhost
# We inject arguments from the environment variables scope # to an instance __init__.# We use a `prefix` to minimize clashes with other components that have a username / password.# Argument username will have a correponding DB_USERNAME, same for password and databasefromargresolverimportEnvironmentfromargresolver.utilsimportmodified_environ# We use it to alter the environment variablesclassConnection:@Environment(prefix='DB')def__init__(self,username,password,database='default'):self.username=usernameself.password=passwordself.database=databasedef__str__(self):# Hint: In a real world example you won't put your password in here ;-)return"Connection(username='{self.username}', password='{self.password}'"\
        ", database='{self.database}')".format(self=self)withmodified_environ(DB_USERNAME='admin',DB_PASSWORD='secret'):conn=Connection()print(str(conn))# Connection(username='admin', password='secret', database='default')

1.2条。地图

# We use the Map resolver to override an argument's default value # that is better suited for our needs.fromargresolverimportMap# Let's assume we cannot touch this code...classFoo:def__init__(self,arg1,arg2='I_dont_like_this_default'):self.arg1=arg1self.arg2=arg2def__str__(self):return"Foo(arg1='{self.arg1}', arg2='{self.arg2}')".format(self=self)# But we can alter the class and wrap a resolver around the class __init__ Foo.__init__=Map(dict(arg2="better_default"),default_override=True)(Foo.__init__)foo=Foo("this is arg1")print(str(foo))# Foo(arg1='this is arg1', arg2='better_default')

1.3条。链条

# We do some automatic dependency injection with fallbackfromargresolverimportChain,Const,Mapinject=Chain(Map(dict(service1="Service1",service2="Service2")),Const("Fallback Service"))classOrchestration:@injectdefbusiness_process1(self,service1,service2):print("Calling service:",service1)print("Calling service:",service2)@injectdefbusiness_process2(self,service1,service3):print("Calling service:",service1)print("Calling service:",service3)orchester=Orchestration()orchester.business_process1()# Prints:# Calling service: Service1# Calling service: Service2orchester.business_process2()# Prints:# Calling service: Service1# Calling service: Fallback Service

欢迎加入QQ群-->: 979659372 Python中文网_新手群

推荐PyPI第三方库


热门话题
java从guiceModule访问dropwizard捆绑包实例   java无法调用控制器方法,使用SpringMVC调用ajax   编译器错误此代码如何工作是一个基本的Java问题   java我需要从另一个控制器类(javafx和SceneBuilder)访问并清除窗格   java编写mongoDB语法   java二维矩形数组中所有元素的和   在Java中,如果类型转换双精度值足够大,则返回一个错误   java从jcombo框中获取数据并将其转换为float   读取带有空行的int文件并保存到JAVA数组中   java自填充缓存现有缓存问题(Spring 4,EhCache 2.10.3)   java有没有更快的方法来计算子矩阵的和?   背景为java指南,请设计   Camel-Spring测试中的java生产者模板显示NullPointerException   爪哇常春藤。未添加xml依赖项   JAVA While loop last else if语句不起作用   java Apache Thrift:抛出由方法中执行的代码引发的异常   多线程JAVA销毁JavaVM线程高CPU   java ArrayList<class>swap方法