Open efd54b2f-2106-478f-aa8c-13cab7280b31 opened 15 years ago
As I mentioned on python-ideas, I my project needs to extend fnmatch to support zsh-style globbing, where you can use brackets to designate subexpressions. Say you had a directory structure like this:
foo/ foo.ext1 foo.ext2 bar/ foo.ext1 foo.ext2
The subexpressions will let you do patterns like this:
>>> glob.glob('foo/foo.{ext1,ext2}')
['foo/foo.ext1', 'foo/foo.ext2']
>>> glob.glob('foo/foo.ext{1,2}')
['foo/foo.ext1', 'foo/foo.ext2']
>>> glob.glob('{foo,bar}')
['bar', 'foo']
>>> glob.glob('{foo,bar}/foo*')
['bar/foo.ext1', 'bar/foo.ext2', 'foo/foo.ext1', 'foo/foo.ext2']
>>> glob.glob('{foo,bar}/foo.{ext*}')
['bar/foo.ext1', 'bar/foo.ext2', 'foo/foo.ext1', 'foo/foo.ext2']
>>> glob.glob('{f?o,b?r}/foo.{ext*}')
['bar/foo.ext1', 'bar/foo.ext2', 'foo/foo.ext1', 'foo/foo.ext2']
Would this be interesting to anyone else? It would unfortunately break fnmatch since it currently treats {} as ordinary characters. It'd be easy to work around that by adding a flag or using a different function name though. Anyway, here's the patch against the head of py3k.
This should be applicable to 2.7, at least, as well. Here is a backport of the patch against trunk.
This can't go in like this, since suddenly accepting braces is a subtle change of semantics. I could imagine adding another function though, that has extended zsh-like globbing abilities.
I completely agree, Georg. I tried to get this in before 3.0, but it didn't work out which is a shame. I assume you also feel that we couldn't make a backwards compatible change in 3.1, right? I thought I heard that 3.1 may break some things in 3.0, but I'm not positive.
Got any suggestions for a function name? Here are some I have in mind, but none of them are particularly good:
fnmatch.fnmatch2 fnmatch.efnmatch fnmatch.extended_fnmatch
glob.glob2 glob.eglob glob.extended_glob
I like glob_ext() and fnmatch_ext().
Is there mileage for glob.glob to grow a dialect param, with a default value to keep it backwards compatible? Otherwise, presumably, proponents of some other xsh variant will come forward with their scheme of matching, and regex-followers with theirs and so on.
Raymond, is this zsh addition still relevant or out of date?
Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.
Show more details
GitHub fields: ```python assignee = None closed_at = None created_at =
labels = ['type-feature', 'library', '3.10']
title = 'zsh-style subpattern matching for fnmatch/glob'
updated_at =
user = 'https://bugs.python.org/erickt'
```
bugs.python.org fields:
```python
activity =
actor = 'terry.reedy'
assignee = 'none'
closed = False
closed_date = None
closer = None
components = ['Library (Lib)']
creation =
creator = 'erickt'
dependencies = []
files = ['12259', '12273']
hgrepos = []
issue_num = 4573
keywords = ['patch']
message_count = 7.0
messages = ['77216', '77278', '85073', '85130', '85131', '85180', '379369']
nosy_count = 8.0
nosy_names = ['georg.brandl', 'rhettinger', 'terry.reedy', 'ironfroggy', 'giampaolo.rodola', 'tim.golden', 'erickt', 'kveretennicov']
pr_nums = []
priority = 'normal'
resolution = None
stage = None
status = 'open'
superseder = None
type = 'enhancement'
url = 'https://bugs.python.org/issue4573'
versions = ['Python 3.10']
```