[Pyparsing] pattern type, result type, & naming
Brought to you by:
ptmcg
From: spir <den...@fr...> - 2008-11-09 19:25:54
|
Hello, New to the list. I'm trying to understand how things work with pattern types, result types, and naming through setResultsName() or (). Example: a = Literal('a') b = Literal('b') c1 = (a|b)('| ') c2 = MatchFirst(a,b)('first') c3 = (a^b)('^ ') c4 = Or(a,b)('or ') c5 = Combine(a|b)('combi') c0 = Group(a|b)('group') patterns = [c1,c2,c3,c4,c5,c0] t = "a" # === use for p in patterns: result = p.parseString(t) token = result[0] print p.resultsName, token.__class__.__name__, token, try: print token.getName() except AttributeError: print 'no_getName()' print 'full result: %s' %(result) # === output | str a no_getName() first str a no_getName() ^ str a no_getName() or str a no_getName() combi str a no_getName() group ParseResults ['a'] group full result: [['a']] This leads me to the following questions ;-) -1- Why aren't all results of type ParseResults? -2- Which patterns, actually, generate ParseResults results, and which don't? -3- Is there another way to access resultsName, from a result instance, than getName()? -4- Is there another way to guess a result's "kind" (i.e. which pattern has generated it)? -5- Do we need to systematically Group results to identify them? I wish to get rid of Group where useless, in order not to get results such as [['a']] (['a'] is enough overload). Denis |