qaviton处理程序

qaviton-handlers的Python项目详细描述


Qaviton处理程序

logo
versionlicenseopen issuesdownloadscode size

错误处理实用程序

安装

pip install --upgrade qaviton_handlers  

要求

  • Python 3.6+

特点

  • 重试装饰器✓
  • 使用上下文✓重试
  • 尝试函数✓
  • 捕捉错误✓
  • 简单异常包装器✓

使用

^{pr2}$

重试装饰器

fromqaviton_handlers.try_decoratorimportretry@retry()deffoo():n=int('1'+input('select number:'))print(n)foo()

使用上下文重试

fromqaviton_handlers.try_contextimportretrywithretry()astrying:whiletrying:withtrying:print("Attempt #%d of %d"%(trying.attempt,trying.attempts))raise

使用不同的try wrapper函数

fromqaviton_handlers.try_functionsimporttry_to,try_or_none,multi_try,multi_try_no_breakdeffoo(a=0):print(float(a+input("select number:")))# simply trytry_to(foo,1)try_to(foo,2)try_to(foo,3)
# get the errorerror=try_to(foo,4)iferror:print(error)# if error occurrediftry_to(foo,5):try_to(foo,5)
# try with key argumentsr=try_to(lambdaa,b,c:a*b*c,1,kwargs={'b':2,'c':3})print(r)
# try to get a numbernumber=try_or_none(lambdaa:float(a+input("select number:")),6)ifnumber:print(number)
# try many functions, return a list of results, or an error# if an error occurred, the multi try stopsmulti_try(lambda:foo(10),lambda:foo(11),)# specify errors to ignoreresponse=multi_try(lambda:foo(13),lambda:foo(14),exceptions=Exception,)# handle the errorresponse=multi_try(lambda:foo(13),lambda:foo(14),)ifisinstance(response,Exception):...
# try many functions, return a list of results, some may be errors# if an error occurred, the multi try continuesmulti_try_no_break(lambda:foo(8),lambda:foo(9),lambda:foo(0),)

现在忽略错误,以便以后处理它们

fromqaviton_handlers.catchimportCatchfromqaviton_handlers.utils.errorimportErrorcatch=Catch(store=True)# catch an errortry:1+'1'exceptExceptionase:catch(e)# a cleaner syntaxwithcatch:1+'1'2+'2'# ignore the errorwithCatch():5*'e'print(f"caught {catch.count} errors")print(f"caught first {catch.first}")print(f"caught last {catch.last}")# make your own CatchclassMyCatch(Catch):defhandler(self,e):self.stack.add(Error(e))ifself.log:self.log.warning(f"I caught {e}")returnself

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

推荐PyPI第三方库


热门话题
Intellij方法参数openjdk   JAI二层图像的java中值滤波   带3个数字的java打印3个不同的大小写   java dojo NumberTextBox将小数点显示为“,”而不是“.”仅在墨西哥语浏览器中?   java我希望用户在安卓中创建自己的课程   java知道超类函数中的子类类型   java如何确定我的GWT应用程序是否在frame/iframe中运行?   java在划分两个“int”数字时回答不正确的“double”   在给出内容类型标题和@consume注释后,postman上出现java不支持的媒体类型错误?   如何在java中过滤包含附件的邮件?   java将自定义JPanel添加到我的JFrame时,不会显示任何内容   java Hibernate Hibernate完成saveOrUpdate Vs saveOrUpdateAll   java在ActiveMQ中获取远程代理上的目标列表   java子类中的方法重载   java JPQL查询联接表中不存在项的多对多关系   java非嵌入式集成测试   Spring提供的java正确JSON REST控制器