Python / PyParsing: Complexity with setResultsName

I think I'm wrong in what I call setResultsName():

from pyparsing import *

DEPT_CODE = Regex(r'[A-Z]{2,}').setResultsName("Dept Code")
COURSE_NUMBER = Regex(r'[0-9]{4}').setResultsName("Course Number")

COURSE_NUMBER.setParseAction(lambda s, l, toks : int(toks[0]))

course = DEPT_CODE + COURSE_NUMBER

course.setResultsName("course")

statement = course

From IDLE:

>>> myparser import *
>>> statement.parseString("CS 2110")
(['CS', 2110], {'Dept Code': [('CS', 0)], 'Course Number': [(2110, 1)]})

The result I hope for:

>>> myparser import *
>>> statement.parseString("CS 2110")
(['CS', 2110], {'Course': ['CS', 2110], 'Dept Code': [('CS', 0)], 'Course Number': [(2110, 1)]})

Does it work setResultsName()only for terminals?

+3
source share
1 answer

If you change the definition courseto

course = (DEPT_CODE + COURSE_NUMBER).setResultsName("Course")

you will get the following behavior:

x=statement.parseString("CS 2110")
print(repr(x))
# (['CS', 2110], {'Course': [((['CS', 2110], {'Dept Code': [('CS', 0)], 'Course Number': [(2110, 1)]}), 0)], 'Dept Code': [('CS', 0)], 'Course Number': [(2110, 1)]})
print(x['Dept Code'])
# CS
print(x['Course Number'])
# 2110
print(x['Course'])
# ['CS', 2110]

This is not exactly what you need, but is it enough?

Please note from the documentation :

[setResultsName] returns a copy of the original ParserElement object; this is so that the client can identify a basic element, such as an integer, and refer to it in several places with different names.

course.setResultsName("Course") , course. course=course.setResultsName("Course"). , .

+5

Source: https://habr.com/ru/post/1747781/


All Articles