Package: pilo
Yet another form parser.dev$gitclonegit@github.com:bninja/pilo.git$cdpilo$mkvirtualenvpilo(pilo)$pipinstall-e.[tests](pilo)$py.testtests/--cov=pilo--cov-reportterm-missingreleaseAll is well:(pilo)$py.testtests/--cov=pilo--cov-reportterm-missingso update__version__in:pilo/__init__.pythen commit and tag it (traviswill publish it topypi):$gitcommit-am"release v{version}"$gittag-av{version}-m"release v{version}"$gitpush--tagsusageValidationHere we validate that a message has acceptable headers and body.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<=20HooksOverride-able mechanism allowing users to inject functions to customize these
behaviors:computeresolveparsedefaultmungefiltervalidatee.g.: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.acceptedFalseForm polymorphism and type downcastingIn the following example, a user has an address, but the address schema is
polymorphic on the country abbreviation.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