Вопрос

I'd like to make the 'pyparsing' parsing result come out as a dictionary without neeing to post-process. For this, I need to define my own key strings. The following the best I could come up with that produces the desired results.

Line to parse:

%ADD22C,0.35X*%

Code:

import pyparsing as pyp

floatnum = pyp.Regex(r'([\d\.]+)')
comma = pyp.Literal(',').suppress()

cmd_app_def = pyp.Literal('AD').setParseAction(pyp.replaceWith('aperture-definition'))

cmd_app_def_opt_circ = pyp.Group(pyp.Literal('C') +
comma).setParseAction(pyp.replaceWith('circle'))

circular_apperture = pyp.Group(cmd_app_def_opt_circ +
pyp.Group(pyp.Empty().setParseAction(pyp.replaceWith('diameter')) + floatnum) +
pyp.Literal('X').suppress())

<the grammar for the entire line>

The result is:

['aperture-definition', '20', ['circle', ['diameter', '0.35']]]

What I consider a hack here is

pyp.Empty().setParseAction(pyp.replaceWith('diameter'))

which always matches and is empty, but then I assign my desired key name to it.

Is this the best way to do this? Am I abusing pyparsing to do something it's not meant to do?

Это было полезно?

Решение 2

See comments in the posted code.

import pyparsing as pyp

comma = pyp.Literal(',').suppress()
# use parse actions to do type conversion at parse time, so that results fields
# can immediately be used as ints or floats, without additional int() or float()
# calls
floatnum = pyp.Regex(r'([\d\.]+)').setParseAction(lambda t: float(t[0]))
integer = pyp.Word(pyp.nums).setParseAction(lambda t: int(t[0]))

# define the command keyword - I assume there will be other commands too, they
# should follow this general pattern (define the command keyword, then all the
# options, then define the overall command)
aperture_defn_command_keyword = pyp.Literal('AD')

# define a results name for the matched integer - I don't know what this
# option is, wasn't in your original post
d_option = 'D' + integer.setResultsName('D')

# shortcut for defining a results name is to use the expression as a 
# callable, and pass the results name as the argument (I find this much
# cleaner and keeps the grammar definition from getting messy with lots
# of calls to setResultsName)
circular_aperture_defn = 'C' + comma + floatnum('diameter') + 'X'

# define the overall command
aperture_defn_command = aperture_defn_command_keyword("command") + d_option + pyp.Optional(circular_aperture_defn)

# use searchString to skip over '%'s and '*'s, gives us a ParseResults object
test = "%ADD22C,0.35X*%"
appData = aperture_defn_command.searchString(test)[0]

# ParseResults can be accessed directly just like a dict
print appData['command']
print appData['D']
print appData['diameter']

# or if you prefer attribute-style access to results names
print appData.command
print appData.D
print appData.diameter

# convert ParseResults to an actual Python dict, removes all unnamed tokens
print appData.asDict()

# dump() prints out the parsed tokens as a list, then all named results
print appData.dump()

Prints:

AD
22
0.35
AD
22
0.35
{'diameter': 0.34999999999999998, 'command': 'AD', 'D': 22}
['AD', 'D', 22, 'C', 0.34999999999999998, 'X']
- D: 22
- command: AD
- diameter: 0.35

Другие советы

If you want to name your floatnum as "diameter", you can use named results:

cmd_app_def_opt_circ = pyp.Group(pyp.Literal('C') +
comma)("circle")


circular_apperture = pyp.Group(cmd_app_def_opt_circ +
pyp.Group(floatnum)("diameter") +
pyp.Literal('X').suppress())

In this way, every time the parses encounters floatnum in the circular_appertur context, this result is named diameter. Also, as described above, you can name circle in the same fashion. Does this work for you?

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top