例外工厂

python-should-check的Python项目详细描述


#python应该检查

检查参数并提出可理解的异常。

>>> from should_check import (
...    Check,
...    be_callable,
...    be_py_enum_str,
...    be_instance_of,
...    be_subclass_of,
...    not_be_none,
...    not_contain,
...    contain,
...    not_be_empty,
...    be_shorter_than,
...    be_length_of,
...    not_be_negative,
...    be_greater_than,
...    be_less_than,
...    be_equal_with,
...    be,
...    be_in)

##检查函数是否可调用,而不是无

>>> function = Check(function=print).should(not_be_none, be_callable)
>>> function("Hello world")
Hello world
>>> function = Check(function=None).should(not_be_none, be_callable)
Traceback (most recent call last):
...
ValueError: 'function' should not be None
>>> function = Check(function="boo").should(not_be_none, be_callable)
Traceback (most recent call last):
...
ValueError: 'function' should be callable

如果“无”不能通过这些检查,那么 检查函数的可选参数。例如,此示例起作用:

>>> function = Check(function=None).should(be_callable)

但这些习惯:

>>> checked = Check(value=None).should(be(1))
Traceback (most recent call last):
...
ValueError: value 'None' should (reference equally) be '1'
>>> checked = Check(value=None).should(be_in([1,2]))
Traceback (most recent call last):
...
ValueError: value 'None' should be in '[1, 2]'
>>> checked = Check(value=None).should(be_equal_with(1))
Traceback (most recent call last):
...
ValueError: value 'None' should be equal with '1'

所以小心点,不要把支票放在任何地方。

##检查字符串是否是枚举定义的成员

>>> import enum
>>> class MyEnum(enum.IntEnum):
...   GOOD_ENUM = 0
...   MEH_ENUM = 1
...
>>> enum_str = Check(enum_str="GOOD_ENUM").should(not_be_none, be_py_enum_str(MyEnum))
>>> enum_str = Check(enum_str="BAD_ENUM").should(not_be_none, be_py_enum_str(MyEnum))
Traceback (most recent call last):
...
TypeError: enum_str 'BAD_ENUM' should be one of '['GOOD_ENUM', 'MEH_ENUM']'

##检查对象是否是类的实例

>>> my_enum = MyEnum(0)
>>> second_enum = Check(my_enum=my_enum).should(not_be_none, be_instance_of(MyEnum))
>>> second_enum =  Check(my_enum=my_enum).should(not_be_none, be_instance_of(int))
>>> second_enum =  Check(my_enum=my_enum).should(not_be_none, be_instance_of(str))
Traceback (most recent call last):
...
TypeError: my_enum '0' should be instance of '<class 'str'>'

##检查一个类是否是另一个类的子类

>>> CheckedClass = Check(subclass=MyEnum).should(
...    not_be_none,
...    be_subclass_of(enum.IntEnum),
...    be_subclass_of(int),
...    be_subclass_of(str))
Traceback (most recent call last):
...
TypeError: subclass '<enum 'MyEnum'>' should be subclass of '<class 'str'>'

##检查物品是否不在容器中

>>> checked_item = Check(item=[1,2,3]).should(not_be_none, not_contain(51))
>>> checked_item = Check(item=[1,2,3]).should(not_be_none, not_contain(1))
Traceback (most recent call last):
...
ValueError: 'item' should not contain '1'

##检查物品是否在容器中

>>> checked_item = Check(item=[1,2,3]).should(not_be_none, contain(1))
>>> checked_item = Check(item=[1,2,3]).should(not_be_none, contain(51))
Traceback (most recent call last):
...
ValueError: 'item' should contain '51'

##检查集合是否不为空

>>> not_empty = Check(collection=[1,2,3]).should(not_be_none, not_be_empty)
>>> not_empty = Check(collection=set()).should(not_be_none, not_be_empty)
Traceback (most recent call last):
...
ValueError: 'collection' should not be empty

##容器(字符串)的长度检查

>>> capped = Check(number="a").should(not_be_none, be_shorter_than(2))
>>> capped = Check(number="aaa").should(not_be_none, be_shorter_than(2))
Traceback (most recent call last):
...
ValueError: number length '3' should be equal or less than '2'
>>> fixed = Check(number="aa").should(not_be_none, be_length_of(2))
>>> fixed = Check(number="aaa").should(not_be_none, be_length_of(2))
Traceback (most recent call last):
...
ValueError: number length '3' should be equal with '2'

##数字范围检查:

>>> positive = Check(number=-1).should(not_be_none, not_be_negative)
Traceback (most recent call last):
...
ValueError: 'number' should not be negative
>>> positive = Check(number=-1).should(not_be_none, be_greater_than(0))
Traceback (most recent call last):
...
ValueError: number '-1' should be greater than '0'
>>> positive = Check(number=4).should(not_be_none, be_less_than(1))
Traceback (most recent call last):
...
ValueError: number '4' should be less than '1'

##相等检查

>>> equals_one = Check(number="1").should(not_be_none, be_equal_with("1"))
>>> equals_one = Check(number="2").should(not_be_none, be_equal_with("1"))
Traceback (most recent call last):
...
ValueError: number '2' should be equal with '1'
>>> reference = object()
>>> equals_one = Check(object=reference).should(not_be_none, be(reference))
>>> equals_one = Check(object="another").should(be(None))
Traceback (most recent call last):
...
ValueError: object 'another' should (reference equally) be 'None'

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

推荐PyPI第三方库


热门话题
java如何使用“Wed,01 Jul 2015 17:32:41 EDT”解析字符串   java Storm apache升级(1.0.0到2.0.0)   java类驻留在不同的目录中,而不是包指定的目录。为什么?   将Java中的图像缩放到非常小的维度   java如何通过子文档从自定义方面访问ElasticSearch parentdoc字段   java如何在RationalSoftwareArchitect中使用findbugs?   Java中的事件提升处理   java值被添加到arrayList的所有索引中,而不是在“”时添加到最后一个索引中。正在使用arraylist的add()方法   JFrame中的java JPanel派生类   java如何用循环和异步方法模拟类   java Android阻止可绘制背景超出视图范围   为客户排序Java阵列   java Apache poi如何将工作表设置为枚举位置值属性?   java Rhino在使用自定义类参数调用javascript函数时出错   java格式化日期从年月日到年月日   spring如何修复java。lang.illegalargumentexception在此特定场景中是否尝试创建具有null实体的合并事件?   java如何创建更好的对象