-
-
Notifications
You must be signed in to change notification settings - Fork 290
.leave_whitespace() may make unnecessary copies #525
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
Currently I have this as a workaround: # @cache won't work
_seen = set()
def _leave_whitespace(element: ParserElement) -> ParserElement:
if expression in _seen:
return element
_seen.add(element)
if hasattr(element, 'expr'):
_leave_whitespace(element.expr)
elif hasattr(element, 'exprs'):
for epxr in element.exprs:
_leave_whitespace(expr)
else:
element.leave_whitespace()
return element |
Here is an alternative implementation of
|
@ptmcg Thanks. It would be nice if you document methods like this one so that we users know which ones are implementation details and which are not. Also, the method call should have |
A module of mine has tens of (nested) element definitions, all have
.leave_whitespace()
called; it takes several seconds to run. It took me quite some time to realize that pyparsing were making copies of nested elements, which is unnecessary for my use case:pyparsing/pyparsing/core.py
Lines 3738 to 3749 in fad68f4
pyparsing/pyparsing/core.py
Lines 4524 to 4531 in fad68f4
In the same spirit with
recursive
(#219), I think another parameter should also be introduced to configure the behavior.The text was updated successfully, but these errors were encountered: