例外工厂
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'