Re: [Pyparsing] Fwd: Returning an actual `list`
Brought to you by:
ptmcg
|
From: <pt...@au...> - 2017-10-20 12:41:04
|
I looked at this briefly last week, I thought I had a working example of this in the parsePythonValue.py example, but it seems to have the same problem.
I thought I had worked out a working version at one time, using ungroup, but have not succeeded with that.
Here is an example of an expression and parse action that creates a list. Bear in mind that the return value from parseString is *always* a ParseResults, even if it is just `pp.pyparsing_common.integer.parseString("123")`.
Without the parse action, the parsed list is a ParseResults returned as the 0'th element of a ParseResults:
import pyparsing as pp
LBRACK, RBRACK = map(pp.Suppress, "[]")
item = pp.Word(pp.alphas)
list_expr = LBRACK + pp.delimitedList(item) + RBRACK
ret = pp.Group(list_expr).parseString("[a,b,c,d]")
print(ret)
print(type(ret))
print(ret[0])
print(type(ret[0]))
Gives:
[['a', 'b', 'c', 'd']]
<class 'pyparsing.ParseResults'>
['a', 'b', 'c', 'd']
<class 'pyparsing.ParseResults'>
Adding this parse action, we get a list returned as the 0'th element of a ParseResults. Note *no* Group around the list expression:
def make_list(tokens):
contents = tokens.asList()
tokens[:] = (contents,)
list_expr.addParseAction(make_list)
ret = list_expr.parseString("[a,b,c,d]")
print(ret)
print(type(ret))
print(ret[0])
print(type(ret[0]))
Gives:
[['a', 'b', 'c', 'd']]
<class 'pyparsing.ParseResults'>
['a', 'b', 'c', 'd']
<class 'list'>
Does that get you closer?
-- Paul
---- Athanasios Anastasiou <ath...@gm...> wrote:
> Hello everyone
>
> Any ideas on the attached?
>
> All the best
> AA
>
>
> ---------- Forwarded message ----------
> From: Athanasios Anastasiou <ath...@gm...>
> Date: Wed, Oct 4, 2017 at 11:44 AM
> Subject: Returning an actual `list`
> To: pyp...@li...
>
>
> Hello
>
> I have set up a very simple "primitive data type" parsing system to parse
> numbers, quoted strings, lists and dictionaries. The last two are following
> Python's convention.
>
> While this is working, I am having trouble returning specific data types
> from the "action" of the LIST definition.
>
> Here is a snippet:
>
> LIST = pyparsing.Forward()
> DICT = pyparsing.Forward()
>
> VALUE = (NUMBER|IDENTIFIER|DICT|pyparsing.Group(LIST))
>
> KEY_VALUE_PAIR = pyparsing.Group(IDENTIFIER("key") +
> pyparsing.Suppress(":") + VALUE("value"))
>
> LIST << pyparsing.Suppress("[") + pyparsing.delimitedList(VALUE) +
> pyparsing.Suppress("]")
> DICT << pyparsing.Suppress("{") + pyparsing.delimitedList(KEY_VALUE_PAIR) +
> pyparsing.Suppress("}")
>
> So, when you are trying to parse something like: [1,2,3,4,[5,6]], this is
> returned as a pyparsing.ParseResults type of object, rather than a `list`.
> I have tried to set a simple parseAction with `lambda s,l,t: list(t)` or
> `lambda s,l,t:list(t[0])` but I am still getting back a ParseResults object.
>
> Ideally, I would like the rule to return a list, just like the INT rule
> returns a proper Python integer.
>
> Any ideas about what am I missing?
>
> All the best
> AA
> ------------------------------------------------------------------------------
> Check out the vibrant tech community on one of the world's most
> engaging tech sites, Slashdot.org! http://sdm.link/slashdot
> _______________________________________________
> Pyparsing-users mailing list
> Pyp...@li...
> https://lists.sourceforge.net/lists/listinfo/pyparsing-users
|