Greetings. First of all I'd like to thank for this project. It saves a lot of my time.
But as I am new to pyparsing I'd like to clarify some aspects of the API.
Here's the code:
assert p[0] == p.first, "Accessing result by index and by name should return the same object"
The first assertion is ok, but the second fails, because p[0] is a string "qwe" (as expected), but p.first is an instance of ParseResults object. The question is: do I misunderstand something or is it a bug?
Regards, Ihor
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
Even though your second example parses only a single word 'qwe' as the first subscript, that word is defined as part of an And(). And() expressions always return an array of tokens, even if only one of the elements of the And() returns any text. If you print out p.dump(), you can see that p.first returns ['qwe']:
['qwe', 'asd']
- first: ['qwe']
- second: ['asd']
so to get the 'qwe' string, you'll have to refer to p.first[0].
If you are going to suppress the leading and trailing brackets, you can collapse this back to a single string value by wrapping the subscript expression in a Combine():
Greetings. First of all I'd like to thank for this project. It saves a lot of my time.
But as I am new to pyparsing I'd like to clarify some aspects of the API.
Here's the code:
# Test 1
subscript = Word(alphas)
item = Group(subscript.setResultsName("first") + subscript.setResultsName("second") + Literal(";").suppress())
grammar = OneOrMore(item)
s = r'qwe asd;qwe asd;'
p = grammar.parseString(s)[0]
assert p[0] == p.first, "Accessing result by index and by name should return the same object"
# Test 2
subscript = Literal("[").suppress() + Word(alphas) + Literal("]").suppress()
item = Group(subscript.setResultsName("first") + subscript.setResultsName("second") + Literal(";").suppress())
grammar = OneOrMore(item)
s = r'[qwe][asd];[qwe][asd];'
p = grammar.parseString(s)[0]
assert p[0] == p.first, "Accessing result by index and by name should return the same object"
The first assertion is ok, but the second fails, because p[0] is a string "qwe" (as expected), but p.first is an instance of ParseResults object. The question is: do I misunderstand something or is it a bug?
Regards, Ihor
Ihor -
Even though your second example parses only a single word 'qwe' as the first subscript, that word is defined as part of an And(). And() expressions always return an array of tokens, even if only one of the elements of the And() returns any text. If you print out p.dump(), you can see that p.first returns ['qwe']:
['qwe', 'asd']
- first: ['qwe']
- second: ['asd']
so to get the 'qwe' string, you'll have to refer to p.first[0].
If you are going to suppress the leading and trailing brackets, you can collapse this back to a single string value by wrapping the subscript expression in a Combine():
subscript = Combine(Literal("[").suppress() + Word(alphas) + Literal("]").suppress())
Using this expression for subscript, p.dump() now returns:
['qwe', 'asd']
- first: qwe
- second: asd
which is the same value returned from your first grammar.
Not really a bug, but a subtle difference in behavior for And()'ed results.
-- Paul