Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting a pure list out of 'pyparsing.ParseResults'

I'm currently trying to get a result from pyparsing as a pure list so I can flatten it. I read in the documentation that

ParseResults can also be converted to an ordinary list of strings by calling asList(). Note that this will strip the results of any field names that have been defined for any embedded parse elements. (The pprint module is especially good at printing out the nested contents given by asList().)

So I tried defining a setParseAction where I work on the ParseResult

what I get is:

>>> print type(tokens.args[0])
 <class 'pyparsing.ParseResults'>
>>> print type(tokens.args[0].asList)
 <type 'instancemethod'>

But I was expecting/needing the last one to be of type list. I must be missing something important when using asList() here.

Dietmar

PS: Here a MTC of what the tokens actually look like:

>>> print tokens.args[0]
['foo1', ['xxx'], ',', 'graphics={', 'bar1', ['xxx,yyy'], ',', 'bar2', 
['xxx,yyy'], ',', 'bar3', ['xxx,yyy,', 'zzz=baz', ['xxx,yyy']], '}']
like image 338
Dietmar Winkler Avatar asked May 10 '12 12:05

Dietmar Winkler


Video Answer


1 Answers

tokens.args[0].asList is a function. tokens.args[0].asList() is a call to that function (with no arguments beyond the self argument). It seems that you would like to know the type of that latter expression.

like image 54
Marcin Avatar answered Sep 20 '22 07:09

Marcin