Crossposted at http://pyparsing.wikispaces.com/message/view/home/49765026
I am working on a parsing project where I need to inject some manually created parseresults into a parsed token. I have attached a parseaction to the appropriate place in my code, and I seem to have succeeded in creating a custom made parseresult to add back into my larger grammer. dump() and asxml() seem to output correctly, but other parts of my code (trying to access the created results by name) have issues. I can access by list position, but not assigned name. It is entirely possible that my limited python knowledge is messing me up somewhere, but since I have not been able to find an example of creating a parseresults quite this way I thought I would start here. Here is my parseresults creation code. tripHeaderCustomFields is attached as a parseaction. If a particular value is parsed (ie. "TripCode") then some custom parseresults are created and added back in to the final result.
If anyone has tried to create manual parseresults like this, could you please look over my code and tell me if you see any glaring problems? It took hours of trial and error to get this version to work, and I would not be surprised if there is a better or more correct way.
def addCustomField( self, group, name, datatype, value ):
"""
custom fields:
Group: ie, specific airline or category - "USAir, "general"
Name: name of field, ie "linecheck", "Medical", "Deadhead", "IV Pay"
DataType: string, int, date, time
Value: value of field, ie. "checked by joe shmo, #2345", or "1st class medical - bryman"
"""
#TODO: Need to ask for help, some logic problem somewhere. loosing string name somewhere, but xml prints ok!
prGroup = ParseResults( group, self.NAME.CFGROUP )
prName = ParseResults( name, self.NAME.CFNAME )
prDataType = ParseResults( datatype, self.NAME.CFDATATYPE )
prValue = ParseResults( value, self.NAME.CFVAULE )
prList = ParseResults( [] )
prList += prGroup
prList += prName
prList += prDataType
prList += prValue
customField = ParseResults( [prList], self.NAME.CUSTOMFIELD )
return customField
def tripHeaderCustomFields( self, tokens ):
parseSegment = tokens
if "TripCode" in parseSegment:
customField = self.addCustomField( "USAir", "PairingCode", "String", parseSegment["TripCode"] )
if self.NAME.CUSTOMFIELDS in parseSegment:
parseSegment[self.NAME.CUSTOMFIELDS] += customField
else :
parseSegment += ParseResults( [customField], self.NAME.CUSTOMFIELDS )
if "Charter" in parseSegment[self.NAME.EFFECTIVEDOWS]:
customField = self.addCustomField( "USAir", "Charter", "Boolean", "True" )
if self.NAME.CUSTOMFIELDS in parseSegment:
parseSegment[self.NAME.CUSTOMFIELDS] += customField
else :
parseSegment += ParseResults( [customField], self.NAME.CUSTOMFIELDS )
return tokens
returns a seemingly correct token,
<CustomFields>
<CustomField>
<Group>USAir</Group>
<Name>EquipmentChange</Name>
<DataType>Boolean</DataType>
<Value>True</Value>
</CustomField>
<CustomField>
<Group>USAir</Group>
<Name>EquipmentChange</Name>
<DataType>Boolean</DataType>
<Value>True</Value>
</CustomField>
</CustomFields>
that goes into a bigger result:
<Trip>
<TripNumber>8510</TripNumber>
<EffectiveDOWs>
<EXCPT>EXCPT</EXCPT>
<DayOfWeek>MO</DayOfWeek>
<DayOfWeek>TH</DayOfWeek>
<DayOfWeek>FR</DayOfWeek>
</EffectiveDOWs>
<ReportTime>
<Hours>21</Hours>
<Minutes>40</Minutes>
</ReportTime>
<TripCode>N</TripCode>
<EffectiveDateStart>
<Month>APR</Month>
<Day>02</Day>
</EffectiveDateStart>
<EffectiveDateEnd>
<Month>APR</Month>
<Day>27</Day>
</EffectiveDateEnd>
<CustomFields>
<CustomField>
<Group>USAir</Group>
<Name>PairingCode</Name>
<DataType>String</DataType>
<Value>N</Value>
</CustomField>
</CustomFields>
<RequiredCrew>
<Captain>1</Captain>
<FO>1</FO>
</RequiredCrew>
.....snip....
</Trip>