我正在使用InType
字段扩展Python ORM,这是一个验证器。
事实上,我正在创建一大堆验证器...在我正在模拟的web2py DAL术语中:
Field(type=String, validators=[IS_EMAIL(), IS_IN(['foo@bar.com'])])
然而,在自定义字段级别完成所有操作。我试图在这里创建一个测试用例:
from types import StringType, IntType
class StringField(object):
def validate(self, value):
assert type(value) is StringType
self.value = value
class IntField(object):
def validate(self, value):
assert type(value) is IntType
self.value = value
class InField(object):
"""
Confirms `value in enumerable` before putting
in `StringField` or `IntField`
"""
def validate(self, value, enumerable):
assert value not in enumerable
obj = (StringField if type(value) is StringType else IntField)()
obj.validate(value)
return obj
我没有像上面那样返回运行时发现的类的实例对象,而是认为使用元类或其他技术实现它更好。