11# ` wcmatch.fnmatch `
22
3- ``` py3
3+ ``` py
44from wcmatch import fnmatch
55```
66
@@ -13,7 +13,7 @@ default. See [flags](#flags) for more information.
1313
1414/// tip | Backslashes
1515When using backslashes, it is helpful to use raw strings. In a raw string, a single backslash is used to escape a
16- character ` #!py3 r'\?' ` . If you want to represent a literal backslash, you must use two: ` #!py3 r'some\\path' ` .
16+ character ` #!py r'\?' ` . If you want to represent a literal backslash, you must use two: ` #!py r'some\\path' ` .
1717///
1818
1919Pattern | Meaning
@@ -51,11 +51,30 @@ be no limit.
5151The imposed pattern limit and corresponding ` limit ` option was introduced in 6.0.
5252///
5353
54+ ## Precompiling
55+
56+ While patterns are often cached, auto expanding patterns, such as ` 'file{a, b, c}' ` will have each individual
57+ permutation cached (up to the cache limit), but not the entire pattern. This is to prevent the cache from exploding with
58+ really large patterns such as ` {1..100} ` . Essentially, individual patterns are cached, but not the expansion of a
59+ pattern into many patterns.
60+
61+ If it is planned to reuse a pattern and the performance hit of recompiling is not desired, you can precompile a matcher
62+ object via [ ` fnmatch.compile ` ] ( #compile ) which returns a [ ` WcMatcher ` ] ( #wcmatcher ) object.
63+
64+ ``` py
65+ >> > import wcmatch.fnmatch as fnmatch
66+ >> > m = fnmatch.compile(' *.md' )
67+ >> > m.match(' README.md' )
68+ True
69+ >> > m.filter([' test.txt' , ' file.md' , ' README.md' ])
70+ [' file.md' , ' README.md' ]
71+ ```
72+
5473## API
5574
5675#### ` fnmatch.fnmatch ` {: #fnmatch}
5776
58- ``` py3
77+ ``` py
5978def fnmatch (filename , patterns , * , flags = 0 , limit = 1000 , exclude = None )
6079```
6180
128147
129148#### ` fnmatch.filter ` {: #filter}
130149
131- ``` py3
150+ ``` py
132151def filter (filenames , patterns , * , flags = 0 , limit = 1000 , exclude = None ):
133152```
134153
@@ -151,9 +170,60 @@ pattern or a list of patterns.It returns a list of all files that matched the pa
151170` exclude ` parameter was added.
152171///
153172
173+ #### ` fnmatch.compile ` {: #compile}
174+
175+ ``` py
176+ def compile (patterns , * , flags = 0 , limit = 1000 , exclude = None ):
177+ ```
178+
179+ The ` compile ` function takes a file pattern (or list of patterns) and flags. It also allows configuring the [ max pattern
180+ limit] ( #multi-pattern-limits ) . Exclusion patterns can be specified via the ` exclude ` parameter which takes a pattern or
181+ a list of patterns. It returns a [ ` WcMatcher ` ] ( #wcmatcher ) object which can match or filter file paths depending on
182+ which method is called.
183+
184+ ``` pycon3
185+ >>> import wcmatch.fnmatch as fnmatch
186+ >>> m = fnmatch.compile('*.md')
187+ >>> m.match('README.md')
188+ True
189+ >>> m.filter(['test.txt', 'file.md', 'README.md'])
190+ ['file.md', 'README.md']
191+ ```
192+
193+ #### ` fnmatch.WcMatcher ` {: #wcmatcher}
194+
195+ The ` WcMatcher ` class is returned when a pattern is precompiled with [ ` compile ` ] ( #compile ) . It has two methods: ` match `
196+ and ` filter ` .
197+
198+ ``` py
199+ def match (self , filename ):
200+ ```
201+
202+ This ` match ` method allows for matching against a precompiled pattern.
203+
204+ ``` pycon3
205+ >>> import wcmatch.fnmatch as fnmatch
206+ >>> m = fnmatch.compile('*.md')
207+ >>> m.match('README.md')
208+ True
209+ ```
210+
211+ ``` py
212+ def filter (self , filenames ):
213+ ```
214+
215+ The ` filter ` method allows for filtering paths against a precompiled pattern.
216+
217+ ``` pycon3
218+ >>> import wcmatch.fnmatch as fnmatch
219+ >>> m = fnmatch.compile('*.md')
220+ >>> m.filter(['test.txt', 'file.md', 'README.md'])
221+ ['file.md', 'README.md']
222+ ```
223+
154224#### ` fnmatch.translate ` {: #translate}
155225
156- ``` py3
226+ ``` py
157227def translate (patterns , * , flags = 0 , limit = 1000 , exclude = None ):
158228```
159229
@@ -173,8 +243,8 @@ matches at least one inclusion pattern and matches **none** of the exclusion pat
173243
174244When using [ ` EXTMATCH ` ] ( #extmatch ) patterns, patterns will be returned with capturing groups around the groups:
175245
176- While in regex patterns like ` #!py3 r'(a)+' ` would capture only the last character, even though multiple where matched,
177- we wrap the entire group to be captured: ` #!py3 '+(a)' ` --> ` #!py3 r'((a)+)' ` .
246+ While in regex patterns like ` #!py r'(a)+' ` would capture only the last character, even though multiple where matched,
247+ we wrap the entire group to be captured: ` #!py '+(a)' ` --> ` #!py r'((a)+)' ` .
178248
179249``` pycon3
180250>>> from wcmatch import fnmatch
@@ -199,7 +269,7 @@ Translate patterns now provide capturing groups for [`EXTMATCH`](#extmatch) grou
199269
200270#### ` fnmatch.escape ` {: #escape}
201271
202- ``` py3
272+ ``` py
203273def escape (pattern ):
204274```
205275
@@ -220,7 +290,7 @@ An `escape` variant for `fnmatch` was made available in 8.1.
220290
221291### ` fnmatch.is_magic ` {: #is_magic}
222292
223- ``` py3
293+ ``` py
224294def is_magic (pattern , * , flags = 0 ):
225295 """ Check if the pattern is likely to be magic."""
226296```
@@ -267,8 +337,8 @@ Added `is_magic` in 8.1.
267337
268338#### ` fnmatch.RAWCHARS, fnmatch.R ` {: #rawchars}
269339
270- ` RAWCHARS ` causes string character syntax to be parsed in raw strings: ` #!py3 r'\u0040' ` --> ` #!py3 r'@' ` . This will
271- handle standard string escapes and Unicode including ` #!py3 r'\N{CHAR NAME}' ` .
340+ ` RAWCHARS ` causes string character syntax to be parsed in raw strings: ` #!py r'\u0040' ` --> ` #!py r'@' ` . This will
341+ handle standard string escapes and Unicode including ` #!py r'\N{CHAR NAME}' ` .
272342
273343#### ` fnmatch.NEGATE, fnmatch.N ` {: #negate}
274344
0 commit comments