-
-
Notifications
You must be signed in to change notification settings - Fork 30.3k
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
GH-73435: Implement recursive wildcards in pathlib.PurePath.match()
#101398
GH-73435: Implement recursive wildcards in pathlib.PurePath.match()
#101398
Conversation
…ch() Add a new *recursive* argument to `pathlib.PurePath.match()`, defaulting to `False`. If set to true, `match()` handles the `**` wildcard as in `Path.glob()`, i.e. it matches any number of path segments. We now compile a `re.Pattern` object for the entire pattern. This is made more difficult by `fnmatch` not treating directory separators as special when evaluating wildcards (`*`, `?`, etc), and so we arrange the path parts onto separate *lines* in a string, and ensure we don't set `re.DOTALL`.
|
pathlib.PurePath.match()
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Approved, but consider adding a couple of comments (as suggested) so that the next person who has to trace through this code is grateful rather than mad at you ;-)
Perfect! Ship it |
Co-authored-by: Alex Waygood <[email protected]>
Thank you for your help Alex, Hugo and Steve! |
Hey, if it interests anyone, I have a follow-up PR that simplifies a bunch of the code added in this PR. It does this by adding a new seps parameter to |
PurePath.match()
now handles the**
wildcard as inPath.glob()
, i.e. it matches any number of path segments.We now compile a
re.Pattern
object for the entire pattern. This is made more difficult byfnmatch
not treating directory separators as special when evaluating wildcards (*
,?
, etc), and so we arrange the path parts onto separate lines in a string, and ensure we don't setre.DOTALL
.This improves performance of
match()
around 2x-3x times for simple patterns, and more for complex patterns: