数据序列化和验证库
lollipop的Python项目详细描述
数据序列化和验证库
功能
- 具有强大类型组合器 的灵活模式定义API
- 数据验证
- 序列化/反序列化
- 就地反序列化
示例
fromlollipop.typesimportObject,String,Datefromlollipop.validatorsimportLengthfromcollectionsimportnamedtuplefromdatetimeimportdatePerson=namedtuple('Person',['name'])Book=namedtuple('Book',['title','publish_date','author'])PersonType=Object({'name':String(validate=Length(min=1)),},constructor=Person)BookType=Object({'title':String(),'publish_date':Date(),'author':PersonType,},constructor=Book)harryPotter1=Book(title='Harry Potter and the Philosopher\'s Stone',publish_date=date(1997,6,26),author=Person(name='J. K. Rowling'))# DumpingBookType.dump(harryPotter1)# => {'title': 'Harry Potter and the Philosopher\'s Stone',# 'publish_date': '1997-06-26',# 'author': {'name': 'J. K. Rowling'}}# LoadingBookType.load({'title':'Harry Potter and the Philosopher\'s Stone','publish_date':'1997-06-26','author':{'name':'J. K. Rowling'}})# => Book(title='Harry Potter and the Philosopher\'s Stone',# publish_date=date(1997, 06, 26),# author=User(name='J. K. Rowling'))# Partial inplace loadingBookType.load_into(harryPotter1,{'publish_date':'1997-06-27'})# => Book(title='Harry Potter and the Philosopher\'s Stone',# publish_date=date(1997, 06, 27),# author=User(name='J. K. Rowling'))# Loading list of objectsList(BookType).load([{'title':'Harry Potter and the Philosopher\'s Stone','publish_date':'1997-06-26','author':{'name':'J. K. Rowling'}},{'title':'Harry Potter and the Chamber of Secrets','publish_date':'1998-07-02','author':{'name':'J. K. Rowling'}},])# => [Book(...), Book(...)]# ValidationBookType.validate({'title':'Harry Potter and the Philosopher\'s Stone','author':{'name':''},})# => {'author': {'name': 'Length should be at least 1'},# 'publish_date': 'Value is required'}
安装
$ pip install lollipop
文件
要求
- python>;=2.6或<;=3.6
项目链接
许可证
麻省理工学院许可有关详细信息,请参见捆绑的LICENSE文件