utils.context_managers module¶
context_managers.py
上下文管理器
错误管理器
@Author pht @Date 2022-07-04
- class utils.context_managers.Checker(untrapped: Type[BaseException] | Tuple[Type[BaseException]] = (), output: str = '发生了意料之外的错误,请检查输入')[源代码]¶
基类:
object
错误管理器
记录当前状态发生错误应当给出的提示信息,发生错误时抛出AssertionError
示例代码¶
``` try:
- with Checker(AssertionError) as checker:
checker.assert_(1 == 1, '相等比较异常') value, div = 10, 0 checker.assert_(value > 0, '被除数应为正数', '除数为0') value /= div checker.set_output(f'不存在活跃度为{value}的同学') student = Person.objects.get(point=value)
- except AssertionError as e:
content = wrong(str(e))