Re: [Pyparsing] pyparsing svg - noob
Brought to you by:
ptmcg
From: Paul M. <pa...@al...> - 2007-11-08 15:12:59
|
You are right, parseString really returns an object, ParseResults. ParseResults tries to behave much like a list, but adds some dict-like and object-like behavior too. I see I overlooked pop() in ParseResults' methods, I'll try to remember to include that in the next version. (You may have to resort to reading some documentation soon...) If you really, really, really want to pop your way through the tokens, you can convert the results to a list by using asList(), and then pop() away! I forgot to mention in the other e-mail: Another recommended technique is to do conversions like string-to-float during the parsing process itself, by adding a parse action. Let's assume you changed the variable name float to floatNum: floatNum = Combine(Optional("-") + Word(nums) + dot + Word(nums)) Now add: def convertToFloat(tokens): return float(tokens[0]) floatNum.setParseAction(convertToFloat) Or if you want to make this a one-liner (more compact, but a lot to take in at once when first learning): floatNum.setParseAction(lambda toks:float(toks[0])) Now when you process the resulting tokens, the floats will already be floating point numbers instead of strings, no need to convert them after-the-fact. -- Paul -----Original Message----- From: pyp...@li... [mailto:pyp...@li...] On Behalf Of Donn Sent: Thursday, November 08, 2007 5:39 AM To: Paul McGuire Cc: pyp...@li... Subject: Re: [Pyparsing] pyparsing svg - noob Paul, Sorry to bug you. Two things: 1. I am having a conceptual issue with the "list" returned by parseString. I can't seem to use it as a normal list. Mainly I like to while len(l)>0: and then l.pop(0) to process it but I can't seem to pop this list. 2. It seems the optional closing "Z" is being ignored. dot = Literal(".") float = Combine(Optional("-") + Word(nums) + dot + Word(nums)) command = oneOf("M L C Z") comma = Literal(",").suppress() couple = Group(float + comma + float) phrase = OneOrMore(command + Group(OneOrMore(couple)) ) #No C commands in this one... d1="""M 209.12237,172.2415 L 286.76739,153.51369 L 286.76739,275.88534 L 209.12237,294.45058 L 209.12237,172.2415 z """ paths = [] pl = phrase.parseString(d1.upper()) while len(pl) > 0: something = pl.pop(0) print something (I get : TypeError: 'str' object is not callable) I could use <for something in pl:> but since some of the coord lists are longer than others and not grouped ideally, it gets hairy. The ideal grouping would be something like this (I mix C and L commands) ['M', [float,float], 'C',[float,float,float,float,float,float], 'L',[float,float], 'Z'] So I can just pop them off one by one. \d ------------------------------------------------------------------------- This SF.net email is sponsored by: Splunk Inc. Still grepping through log files to find problems? Stop. Now Search log events and configuration files using AJAX and a browser. Download your FREE copy of Splunk now >> http://get.splunk.com/ _______________________________________________ Pyparsing-users mailing list Pyp...@li... https://lists.sourceforge.net/lists/listinfo/pyparsing-users |