2012-01-30 80 views
1

手冊ParseResults在http://pyparsing.wikispaces.com/message/view/home/49765026創建於Pyparsing

Crossposted我正在解析項目,我需要一些手動創建parseresults注入一個解析令牌工作。我已將一個parseaction附加到我的代碼中的適當位置,並且我似乎成功創建了一個自定義的parseresult,以將其添加回到我的更大的語法分析器中。 dump()和asxml()似乎輸出正確,但我的代碼的其他部分(試圖通過名稱訪問創建的結果)有問題。我可以通過列表位置訪問,但不能分配名稱。我有限的python知識完全可能將我搞亂了,但由於我沒有找到一個這樣創建parseresults的例子,我以爲我會從這裏開始。這是我的parseresults創建代碼。 tripHeaderCustomFields作爲parseaction附加。如果解析了特定的值(即「TripCode」),則會創建一些自定義parseresults並將其添加回最終結果中。

如果有人試圖創建像這樣的手動parseresults,請你看看我的代碼,並告訴我,如果你看到任何明顯的問題?花了數小時的試驗和錯誤才能使這個版本起作用,如果有更好或更正確的方法,我不會感到驚訝。

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> 

是進入一個更大的結果:

<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> 
+0

PLZ乍得 閱讀我的問題 http://stackoverflow.com/questions/11696338/adding-external-information-to-paeseresults-before-return – 2012-07-28 22:51:57

回答

1

我已經返工我的自定義代碼ParseResults,現在按預期工作。我希望我第一次想到這樣做,因爲這很容易理解。 :)我傾向於重新發明輪子... tripHeaderCustomFields作爲ParseAction附加,並且新的ParseResults被添加到父ParseResults。

這解決了我通過名稱訪問customField項目時遇到的神祕問題。我相信這是由於我並不真正瞭解如何在幕後創建解析結果,所以這對我來說會更好。

def tripHeaderCustomFields(self, tokens): 
    parseSegment = tokens 
    if "TripCode" in parseSegment: 
     customField = self.addCustomField("USAir", "PairingCode", "String", parseSegment["TripCode"], parseSegment) 
    if "Charter" in parseSegment[self.NAME.EFFECTIVEDOWS]: 
     customField = self.addCustomField("USAir", "Charter", "Boolean", "True", parseSegment) 

def buildCustomFieldString(self, group, name, datatype, value): 
    #TODO: replace any stray "|" that might be in input strings 

    text = group + "|" + name + "|" + datatype + "|" + value 
    return text 

def addCustomField(self, group, name, datatype, value, token): 
    """ 
    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" 
     <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> 
    """ 
    pGroup = Word(alphanums)(self.NAME.CFGROUP) 
    pName = Word(alphanums)(self.NAME.CFNAME) 
    pDatatype = Word(alphanums)(self.NAME.CFDATATYPE) 
    pValue = Word(alphanums)(self.NAME.CFVAULE) 
    delim = Suppress("|") 
    customField = Group(pGroup + delim + pName + delim + pDatatype + delim + pValue)(self.NAME.CUSTOMFIELD) 
    text = self.buildCustomFieldString(group, name, datatype, value) 
    if self.NAME.CUSTOMFIELDS in token: 
     token[self.NAME.CUSTOMFIELDS] += customField.parseString(text) 
    else : 
     token += Group(customField)(self.NAME.CUSTOMFIELDS).parseString(text)