这个库允许用户使用一系列的装饰器轻松地包装函数。
execution-pipeline的Python项目详细描述
#python pipeline
此库允许用户使用一系列装饰器轻松包装函数,以形成执行管道。
这在需要清理输入和系统地处理输出的情况下非常有用。
允许您修改传递给修饰函数的任何输入参数。任何传递到"pre"段的函数都将首先执行。
@执行管道(pre=[do_thing_before])
def do_thing(arg1=5):
返回arg1*10
在修饰函数已经运行之后。
def do_thing_after(响应):
response['added']="yup"
return response
@execution_pipeline(post=[do_thing_after])
def do_thing(**kwargs):
return{**kwargs}#只要使用传递的关键字参数创建一个新字典就可以了你需要;它们将按传递的顺序执行。
def do_another_thing_after(response):
assert response['added']='yup'\the one that is first in the pipeline happens first.
response['also_added']='also yup'
return response
@执行管道(post=[执行后的操作,执行后的另一操作])
def执行后的操作(**kwargs):
返回{**kwargs}
{apples':2,'桔子':3,'香蕉':0,'添加':'是','还添加':'还可以'}
定义处理这个错误(e,response=none):
打印(f"哦,不,鲍勃!{e})
返回"别担心,我们处理了一个typeerror。"
def handle_that_error(e,response=none):
打印(f"哦,不,鲍勃!{e})
返回"别担心,我们处理了myexception。"
def处理了其他错误(e,response=none):
打印(f"?{e}")
返回"其他错误?"
错误处理程序=[
{"异常类":类型错误,"处理程序":处理这个错误},
{"异常类":MyException,"处理程序":处理那个错误},
{"异常类":(异常,BaseException),"处理程序":处理其他错误},
]
@execution_pipeline(pre=[do_thing_before],post=[do_thing_after],error=error_handlers)
def fun_boys(arg1,arg4,arg2,arg3,thing=none):
引发myException("出了问题!")
结果=有趣的男孩(1、2、3、4、5)
哦,不,鲍勃!出了问题!
print(result)
别担心,我们处理了myexception。
如果愿意,您还可以使用类实例而不是字典来定义错误处理程序。
`````
类错误处理程序:
def初始(self,exception,处理程序):
self.exception_class=exception_class
self.handler=handler
error_handlers=[
errorhandler(typeerror,handle_this_error),
errorhandler(myexception,handle_that_error),
]
```
cache
cache执行段将记录所有参数(pre段前后)并存储结果
(post段和error段之后)。
来自pipeline.cache.mock import mockcache
mockcache基本上只是一个dict(),带有一些方便过期的方法。
mockcache=mockcache()
更改值=0
@execution管道(cache=mockcache)
def fun(arg1,arg4,arg2,arg3,thing=none:
返回changing_value
fun_boys(1,2,3,4,5)
这是为了提高管道代码的可移植性。
端口=6379)默认值
```
pip install memcached
```
此库允许用户使用一系列装饰器轻松包装函数,以形成执行管道。
这在需要清理输入和系统地处理输出的情况下非常有用。
允许您修改传递给修饰函数的任何输入参数。任何传递到"pre"段的函数都将首先执行。
@执行管道(pre=[do_thing_before])
def do_thing(arg1=5):
返回arg1*10
def do_thing_after(响应):
response['added']="yup"
return response
@execution_pipeline(post=[do_thing_after])
def do_thing(**kwargs):
return{**kwargs}#只要使用传递的关键字参数创建一个新字典就可以了你需要;它们将按传递的顺序执行。
def do_another_thing_after(response):
assert response['added']='yup'\the one that is first in the pipeline happens first.
response['also_added']='also yup'
return response
@执行管道(post=[执行后的操作,执行后的另一操作])
def执行后的操作(**kwargs):
返回{**kwargs}
定义处理这个错误(e,response=none):
打印(f"哦,不,鲍勃!{e})
返回"别担心,我们处理了一个typeerror。"
def handle_that_error(e,response=none):
打印(f"哦,不,鲍勃!{e})
返回"别担心,我们处理了myexception。"
def处理了其他错误(e,response=none):
打印(f"?{e}")
返回"其他错误?"
错误处理程序=[
{"异常类":类型错误,"处理程序":处理这个错误},
{"异常类":MyException,"处理程序":处理那个错误},
{"异常类":(异常,BaseException),"处理程序":处理其他错误},
]
@execution_pipeline(pre=[do_thing_before],post=[do_thing_after],error=error_handlers)
def fun_boys(arg1,arg4,arg2,arg3,thing=none):
引发myException("出了问题!")
结果=有趣的男孩(1、2、3、4、5)
哦,不,鲍勃!出了问题!
print(result)
别担心,我们处理了myexception。
如果愿意,您还可以使用类实例而不是字典来定义错误处理程序。
`````
类错误处理程序:
def初始(self,exception,处理程序):
self.exception_class=exception_class
self.handler=handler
error_handlers=[
errorhandler(typeerror,handle_this_error),
errorhandler(myexception,handle_that_error),
]
```
cache
cache执行段将记录所有参数(pre段前后)并存储结果
(post段和error段之后)。
来自pipeline.cache.mock import mockcache
mockcache基本上只是一个dict(),带有一些方便过期的方法。
mockcache=mockcache()
更改值=0
@execution管道(cache=mockcache)
def fun(arg1,arg4,arg2,arg3,thing=none:
返回changing_value
fun_boys(1,2,3,4,5)
这是为了提高管道代码的可移植性。
端口=6379)默认值
```
pip install memcached
```