另一种形式的解析器。
pilo的Python项目详细描述
又一个表单分析器。
开发
$ git clone git@github.com:bninja/pilo.git $ cd pilo $ mkvirtualenv pilo (pilo)$ pip install -e .[tests](pilo)$ py.test tests/ --cov=pilo --cov-report term-missing
释放
一切正常:
(pilo)$ py.test tests/ --cov=pilo --cov-report term-missing
所以更新__version__in:
- pilo/__init__.py
$ git commit -am "release v{version}" $ git tag -a v{version} -m "release v{version}" $ git push --tags
用法
验证
在此,我们验证消息是否具有可接受的头和正文。
frompiloimportFormfrompilo.fieldsimportDict,StringclassMessage(Form):headers=Dict(String(choices=['to','from','content-type']),String())body=String(max_length=20)>>>printMessage(headers={'to':'William III'},body='ha'*10){'body':'hahahahahahahahahaha','headers':{'to':'William III'}}>>>printMessage(headers={'send-to':'William III'},body='ha'*10)Invalid:headers-"send-to"isnotoneof"to","from","content-type">>>printMessage(headers={'to':'William III'},body='ha'*11)Invalid:body-"hahahahahahahahahahaha"musthavelength<=20
挂钩
允许用户插入函数以自定义这些函数的可重写机制 行为:
- 计算
- 解决
- 解析
- 默认值
- 咀嚼
- 过滤器
- 验证
例如:
importpiloclassExtraCurricular(pilo.Form):category=pilo.fields.String(choices=['athletics','academics','social','service'])name=pilo.fields.String(max_length=40)description=pilo.fields.String(max_length=140)role=pilo.fields.String(choices=['member','leader'])classCollegeApplication(pilo.Form):high_school_name=pilo.fields.String()sat_score=pilo.fields.Integer()gpa=pilo.fields.Float()extra_curriculars=pilo.fields.List(pilo.fields.SubForm(ExtraCurricular))score=pilo.fields.Float(default=pilo.NONE)accepted=pilo.fields.Bool(default=False)@score.computedefscore(self):leadership_roles=[ecforecinself.extra_curricularsifec.role=='leader']relevant_extra_curriculars=[ecforecinself.extra_curricularsifec.categoryin['academics','service']]score=(10*(self.sat_score/1600.0)+10*(self.gpa/4.0)+5*len(leadership_roles)+5*len(relevant_extra_curriculars))returnscore@accepted.computedefaccepted(self):ifself.score>30:returnTruereturnFalse@high_school_name.mungedefhigh_school_name(self,value):returnvalue.upper()application=CollegeApplication(high_school_name='Bodega High',sat_score=1400,gpa=4.0,extra_curriculars=[dict(category='athletics',role='leader',name='hockey',description=''),dict(category='academics',role='member',name='chess club',description=''),])>>>printapplication.high_school_nameBODEGAHIGH>>>printapplication.score28.75>>>printapplication.acceptedFalse
形态多态性与类型下投射
在下面的示例中,用户有一个地址,但是地址模式是 国家缩写的多态性。
importpiloimportuuidclassAddress(pilo.Form):guid=pilo.fields.UUID(default=uuid.uuid4)country=pilo.fields.Type.abstract()classUnitedKingdomAddress(Address):country=pilo.fields.Type.constant('UK')name=pilo.fields.String()street=pilo.fields.String()locality=pilo.fields.String()post_town=pilo.fields.String()postcode=pilo.fields.String()classUnitedStatesAddress(Address):country=pilo.fields.Type.constant('USA')name=pilo.fields.String()street=pilo.fields.String()unit=pilo.fields.String(default=None)city=pilo.fields.String()state=pilo.fields.String()zip=pilo.fields.String(length=5)classUser(pilo.Form):guid=pilo.fields.UUID(default=uuid.uuid4)name=pilo.fields.String()address=pilo.fields.PolymorphicSubForm(Address.country)mikey_representation=dict(name='Michael Pikey',address=dict(country='UK',name='Mikey Pikey',street='351 Meagre Lane',locality='Hedge End',post_town='Southampton',postcode='SO31 4NG',))bart_representation=dict(name='Bartholomew Simpson',address=dict(country='USA',name='Bite Me',street='742 Evergreen Terrace',city='Springfield',state='???',zip='12345',))mikey=User(**mikey_representation)bart=User(**bart_representation)>>>printdict(mikey){'address':{'country':'UK','guid':UUID('8c73752c-69a2-4832-99f8-c5354cbeec59'),'locality':'Hedge End','name':'Mikey Pikey','post_town':'Southampton','postcode':'SO31 4NG','street':'351 Meagre Lane'},'guid':UUID('eee0953c-1b5a-4bd0-893d-f513b1cf24f4'),'name':'Michael Pikey'}>>>printdict(bart){'address':{'city':'Springfield','country':'USA','guid':UUID('a321bedd-8b94-46b8-830e-ea137b08a608'),'name':'Bite Me','state':'???','street':'742 Evergreen Terrace','unit':None,'zip':'12345'},'guid':UUID('3155a3dd-4b5a-4990-aaea-439359bb36a9'),'name':'Bartholomew Simpson'}>>>printmikey.address.postcodeSO314NG>>>printbart.address.zip12345>>>printtype(mikey.address).__name__UnitedKingdomAddress>>>printtype(bart.address).__name__UnitedStatesAddress