I have been working on a DSL using pyparsing.
I have a couple of keywords. The doumentation on pyparsing keyword class is found at http://packages.python.org/pyparsing/pyparsing.pyparsing.Keyword-class.html
I defined them as
this = Keyword("this", caseless=cl)
that = Keyword("that", caseless=cl)
I have a dictionary where the above keywords translate into numbers:
helper_dict = {"this":-1, "that":1}
the problem I am facing is that I am not able to get a consistent string representation for them. When I try str(this), it comes with the quotes. So I can not really use the dictionary without getting a key error. I mean I get a KeyError when I try any of the following:
helper_dict[this]
helper_dict[this.__str__()]
helper_dict[str(this)]
How can I get a proper string representation
I looked at both the documentation for keyword and the super class of keyword and I can not figure out which function is actually supposed to do this.
thisandthatare not strings, they are pyparsing expressions defined using strings, but they are not strings. As forhelper_dict, you have defined the keys using the strings “this” and “that”. You will get back strings from thethisandthatexpressions after they have parsed some input containing their defining input strings.Here is some console experimenting to show you how this works:
Note that Keyword always returns a consistent token in terms of upper/lower case, regardless of the input string’s case. CaselessLiteral and caseless Keyword always return tokens in the case of their defining strings, not the case of the input string.
The parsed data is returned in a ParseResults object, which supports list, dict, and object-style addressing. Here the 0’th element is a string, the string “this”. You can use this string to match the key “this” defined in helper_dict:
Or define a parse action to do this substitution for you at parse time (replaceWith is a helper method defined in pyparsing:
— Paul