函数装饰器的集合,用于处理在入口和出口点上完成的公共过程。

terse的Python项目详细描述


这个模块提供了一个函数装饰器的集合来处理 进入和退出点的共同程序。

安装

可以使用pip和以下命令安装此库。

pip install terse

目录

目录中的

在执行函数之前,保存当前工作目录 并将目录更改为提供的path。之后 函数已完成,目录已更改为保存的工作目录 目录。注意:这是单线程使用的。螺纹 应该使用绝对路径。

fromterseimportin_dirfromglobimportglobimportos# Returns all files and directories at file system's root.@in_dir('/')defroot_files():returnglob('*')# Prints current working directory.print(os.getcwd())# Prints fils gathered from root.print(root_files())# Demonstrates current working directory is preserved.print(os.getcwd())

调用

main

将函数设置为文件的主函数。如果文件是 main,然后执行该函数。返回值将尝试 转换为可以传递给子进程的值;但是,如果没有 可以转换值,假设函数成功退出。

传统上,在python中,您可以执行以下操作来创建主条目 指向文件。

defmain():print("Hello World!")if__name__=='__main__':main()

这可以由以下内容代替。

fromterseimportmain@maindefmain_impl():print("Hello World!")
<H2>出口

不升高

decorator将捕获被调用方泄漏或生成的所有异常。如果 提供callback函数,则回调将仅为 发生异常时调用。如果与一起提供,则返回, 当发生异常时,将返回该值。

fromterseimportno_exceptdeflog(function,exception):print('LOG: %s'%exception)# If exception is given, raise given exception.# Otherwise, return True@no_raise(instead_return=False,callback=log)defexample(exception=None):ifexception:raiseexceptionreturnTrue# Example with no parameters raises no exceptions,# makes no calls to log, returns True.assertexample()==True# Example with parameter raises given exception,# makes a call to log, surpresses exception and# returns False.assertexample(False)==False

返回时

每当args中的值为 由函数返回。

fromterseimporton_returnedfromenumimportEnumclassStatus(Enum):SUCCESS=0FAILED=1CONNECTION_FAILED=2DISK_FAILED=3deflog(function,returned):print("LOG: %s"%returned)# The following are examples for two uses of on_returned.# First is for any return value and the second is for a# set of return values.# ANY RETURN VALUE# Function example_any returns any value passed in.# on_returned will invoke log for any return value.@on_returned(log)defexample_any(val):returnval# Log invoked: prints "LOG: Status.SUCCESS"assertexample_any(Status.SUCCESS)==Status.SUCCESS# Log invoked: prints "LOG: Status.FAILED"assertexample_any(Status.FAILED)==Status.FAILED# Log invoked: prints "LOG: None"assertexample_any(None)==None# Log invoked: prints "LOG:1"assertexample_any(1)==1# SET OF RETURN VALUES# Function example_set returns any value passed in.# on_returned will invoke log whenever values FAILED,# CONNECTION_FAILED or DISK_FAILED from enum Status are# returned by example.@on_returned(log,Status.FAILED,Status.CONNECTION_FAILED,Status.DISK_FAILED)defexample_set(val):returnval# log is not invoked.assertexample_set(Status.SUCCESS)==Status.SUCCESS# Log invoked: prints "LOG: Status.FAILED"assertexample_set(Status.FAILED)==Status.FAILED# Log invoked: prints "LOG: Status.CONNECTION_FAILURE"assertexample_set(Status.CONNECTION_FAILED)==Status.CONNECTION_FAILED# Log invoked: prints "LOG: Status.DISK_FAILURE"assertexample_set(Status.DISK_FAILED)==Status.DISK_FAILED

升高时

decorator将调用callback每当 args由函数引发。

fromterseimporton_exceptiondeflog(function,exception):print(returned)# The following are examples for two uses of on_raised.# First is for any exception and the second is a set of# exceptions.# ANY EXCEPTION EXAMPLE# If exception is given, raise given exception.# Otherwise, return True# on_raised will invoke log for all exceptions# raised by example_any.@on_raised(log)defexample_any(exception=None)ifexception:raiseexceptionreturnTrue# example_any is given no exceptions to throw.# Therefore, it will raise nothing and return True.assertexample_any()==True# example_any is given ZeroDivisionError exception.# It will raise ZeroDivisionError instance.# on_raised detects exception raised and invokes log.try:example_any(ZeroDivisionError())assertFalseexceptZeroDivisionError:pass# example_any is given ValueError exception.# It will raise ValueError instance.# on_raised detects exception raised and invokes log.try:example_any(ValueError())assertFalseexceptValueError:pass# example_any is given KeyError exception.# It will raise KeyError instance.# on_raised detects exception raised and invokes log.try:example_any(KeyError())assertFalseexceptValueError:pass# SET OF EXCEPTIONS EXAMPLE# If exception is given, raise given exception.# Otherwise, return True.# on_raised will only invoke log for ZeroDivisionError# and ValueError. All other exceptions are ignored by# on_raised.@on_raised(log,ZeroDivisionError,ValueError)defexample_set(exception=None)ifexception:raiseexceptionreturnTrue# example_set is given no exceptions to throw.# Therefore, it will raise nothing and return True.assertexample_set()==True# example_set is given ZeroDivisionError exception.# It will raise ZeroDivisionError instance.# on_raised detects exception raised and invokes log.try:example_set(ZeroDivisionError())assertFalseexceptZeroDivisionError:pass# example_set is given ValueError exception.# It will raise ValueError instance.# on_raised detects exception raised and invokes log.try:example_set(ValueError())assertFalseexceptValueError:pass# example_set is given KeyError exception.# It will raise KeyError instance.# example_set detects exception, but it will not# invoke log because KeyError is not in the set of# exceptions to be tracked.try:example_set(KeyError())assertFalseexceptValueError:pass

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

推荐PyPI第三方库


热门话题
java使用双一元运算符vs函数<Double,Double>   由于存在巨大的休眠,超出了java GC开销限制   java更改Tomcat中Apache文件上载的临时目录   当PDF位于本地驱动器时,javascript无法在IE中显示PDF   java hibernate如何加载瞬态对象?   java如何判断包属于哪个模块?   创建Word文档时java文件已损坏   java如何将Eclipse中的每个开放项目导出为自己的JAR?   java将带有getter和setter的变量添加到现有类中   java高效地发送多封电子邮件   java读/写。具有特殊字符的txt文件   java如何在导出到jar时包含opencv本机库   java Xstream在未完成时停止写入文件   if语句Java:无法检查布尔值是否为null   文本Java:读取txt文件并将其保存在字符串数组中,但不带反斜杠(空格)?   java如何使用正则表达式替换字符串的一部分   通过Java远程运行Powershell脚本   filenames带有xml文件空指针异常的Java文件uri