用于自动对象验证和序列化的python库。
entities的Python项目详细描述
Entities是一个用于自动对象验证和序列化的Python库。 在需要定义良好的实体模型但不需要 特别需要一个存储后端或表单验证程序。它支持递归 以及非递归集合类型(list、set和dict)、嵌套实体和 引用字段。它可以自动验证、序列化、反序列化和 为任意复杂度的实体生成可哈希密钥。
用法
典型用法如下:
from entities import * class Account(Entity): id = IntegerField(group=PRIMARY) # this field is in primary key group iban = IntegerField(group=SECONDARY) # this is in secondary key group balance = FloatField(default=0.0) class Name(Entity): first_name = StringField(group=SECONDARY) last_name = StringField(group=SECONDARY) class Customer(Entity): id = IntegerField(group=PRIMARY) name = EntityField(Name, group=SECONDARY) accounts = ListField(ReferenceField(Account), default=[]) # Create Account objects. a_1 = Account(1, 111, 10.0) # __init__() recognize positional arguments a_2 = Account(id=2, iban=222, balance=20.0) # as well as keyword arguments # Generate hashable key using primary key. print a_1.keyify() # prints '(1,)' # Generate hashable key using secondary key. print a_2.keyify(SECONDARY) # prints '(222,)' # Create Customer object. c = Customer(1, Name('eser', 'aygun')) # Generate hashable key using primary key. print c.keyify() # prints '(1,)' # Generate hashable key using secondary key. print c.keyify(SECONDARY) # prints '(('eser', 'aygun'),)' # Try validating an invalid object. c.accounts.append(123) try: c.validate() # fails except ValidationError: print 'accounts list is only for Account objects' # Try validating a valid object. c.accounts = [a_1, a_2] c.validate() # succeeds
待办事项列表
- json序列化模块(应该很简单)
- bson序列化模块(用于MongoDB兼容性)