-
-
Notifications
You must be signed in to change notification settings - Fork 290
Parser element custom name isn't appropriate for __repr__
#416
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
This looks pretty cool! I think I looked at doing something like this a few years ago but, it got back-burnered in favor of other things. Would the repr also include some of the other qualifiers like "set_name" or "set_parse_action"? Or just reconstruct the bare expression? Yes, a PR to implement this would be very welcome. |
I wasn't planning to show too much more than the required constructor arguments, lest it become unreadable too quickly. (The I'll polish something up for a PR. |
PR is up, with a couple of questions:
|
With this change, what is the repr for Perhaps in the case of Word, we might use srange and _collapse_string_to_ranges when the initChars or bodyChars are very long strings. |
The repr for
so the repr for
Using |
With the latest commit in the PR, the repr() for elements which are based on sets of characters now determines whether the character string or an >>> Word(printables)
Word(srange('[!-~]'))
>>> Word("abcdefghijkl")
Word('abcdefghijkl')
>>> Word("abcdefghijklm")
Word('abcdefghijklm')
>>> Word("abcdefghijklmn")
Word(srange('[a-n]')) I forgot to check out |
The Python documentation on
__repr__
encourages developers to return something that "look[s] like a valid Python expression that could be used to recreate an object with the same value" or that is at least "information-rich and unambiguous".The custom name from
set_name
isn't really appropriate for this. Always using the shorthand "default name" for__repr__
would be more information-rich, although something that works like this would really be ideal:All of the above I have working so far for a handful of ParserElement types. Interested in a complete PR?
(And since I seem to tie everything into documentation: Sphinx uses
repr()
to show the values of documented variables.)The text was updated successfully, but these errors were encountered: