Thanks to visit codestin.com
Credit goes to github.com

Skip to content

.leave_whitespace() may make unnecessary copies #525

Open
@InSyncWithFoo

Description

@InSyncWithFoo

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

def leave_whitespace(self, recursive: bool = True) -> ParserElement:
"""
Extends ``leave_whitespace`` defined in base class, and also invokes ``leave_whitespace`` on
all contained expressions.
"""
super().leave_whitespace(recursive)
if recursive:
self.exprs = [e.copy() for e in self.exprs]
for e in self.exprs:
e.leave_whitespace(recursive)
return self

pyparsing/pyparsing/core.py

Lines 4524 to 4531 in fad68f4

def leave_whitespace(self, recursive: bool = True) -> ParserElement:
super().leave_whitespace(recursive)
if recursive:
if self.expr is not None:
self.expr = self.expr.copy()
self.expr.leave_whitespace(recursive)
return self

In the same spirit with recursive (#219), I think another parameter should also be introduced to configure the behavior.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions