Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Taking Brian's pyparsing solution another step, you can create a quasi-deserializer for this format by using the Dict class:</p> <pre><code>import pyparsing string = pyparsing.CharsNotIn("{} \t\r\n") # use Word instead of CharsNotIn, to do whitespace skipping stringchars = pyparsing.printables.replace("{","").replace("}","") string = pyparsing.Word( stringchars ) # define a simple integer, plus auto-converting parse action integer = pyparsing.Word("0123456789").setParseAction(lambda t : int(t[0])) group = pyparsing.Forward() group &lt;&lt; ( pyparsing.Group(pyparsing.Literal("{").suppress() + pyparsing.ZeroOrMore(group) + pyparsing.Literal("}").suppress()) | integer | string ) toplevel = pyparsing.OneOrMore(group) sample = """ protocol sample_thread { { AUTOSTART 0 } { BITMAP thread.gif } { COORDS {0 0} } { DATAFORMAT { { TYPE hl7 } { PREPROCS { { ARGS {{}} } { PROCS sample_proc } } } } } } """ print toplevel.parseString(sample).asList() # Now define something a little more meaningful for a protocol structure, # and use Dict to auto-assign results names LBRACE,RBRACE = map(pyparsing.Suppress,"{}") protocol = ( pyparsing.Keyword("protocol") + string("name") + LBRACE + pyparsing.Dict(pyparsing.OneOrMore( pyparsing.Group(LBRACE + string + group + RBRACE) ) )("parameters") + RBRACE ) results = protocol.parseString(sample) print results.name print results.parameters.BITMAP print results.parameters.keys() print results.dump() </code></pre> <p>Prints</p> <pre><code>['protocol', 'sample_thread', [['AUTOSTART', 0], ['BITMAP', 'thread.gif'], ['COORDS', [0, 0]], ['DATAFORMAT', [['TYPE', 'hl7'], ['PREPROCS', [['ARGS', [[]]], ['PROCS', 'sample_proc']]]]]]] sample_thread thread.gif ['DATAFORMAT', 'COORDS', 'AUTOSTART', 'BITMAP'] ['protocol', 'sample_thread', [['AUTOSTART', 0], ['BITMAP', 'thread.gif'], ['COORDS', [0, 0]], ['DATAFORMAT', [['TYPE', 'hl7'], ['PREPROCS', [['ARGS', [[]]], ['PROCS', 'sample_proc']]]]]]] - name: sample_thread - parameters: [['AUTOSTART', 0], ['BITMAP', 'thread.gif'], ['COORDS', [0, 0]], ['DATAFORMAT', [['TYPE', 'hl7'], ['PREPROCS', [['ARGS', [[]]], ['PROCS', 'sample_proc']]]]]] - AUTOSTART: 0 - BITMAP: thread.gif - COORDS: [0, 0] - DATAFORMAT: [['TYPE', 'hl7'], ['PREPROCS', [['ARGS', [[]]], ['PROCS', 'sample_proc']]]] </code></pre> <p>I think you will get further faster with pyparsing.</p> <p>-- Paul</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload