99The function translate(PATTERN) returns a regular expression
1010corresponding to PATTERN. (It does not compile it.)
1111"""
12-
12+ import os
13+ import posixpath
1314import re
1415
1516__all__ = ["filter" , "fnmatch" , "fnmatchcase" , "purge" , "translate" ]
1617
1718_cache = {} # Maps text patterns to compiled regexen.
1819_cacheb = {} # Ditto for bytes patterns.
19- _MAXCACHE = 100 # Maximum size of caches
20+ _MAXCACHE = 100 # Maximum size of caches.
21+
2022
2123def purge ():
22- """Clear the pattern cache"""
24+ """Clear the pattern cache. """
2325 _cache .clear ()
2426 _cacheb .clear ()
2527
28+
2629def fnmatch (name , pat ):
2730 """Test whether FILENAME matches PATTERN.
2831
@@ -38,12 +41,11 @@ def fnmatch(name, pat):
3841 if the operating system requires it.
3942 If you don't want this, use fnmatchcase(FILENAME, PATTERN).
4043 """
41-
42- import os
4344 name = os .path .normcase (name )
4445 pat = os .path .normcase (pat )
4546 return fnmatchcase (name , pat )
4647
48+
4749def _compile_pattern (pat ):
4850 cache = _cacheb if isinstance (pat , bytes ) else _cache
4951 regex = cache .get (pat )
@@ -59,9 +61,9 @@ def _compile_pattern(pat):
5961 cache [pat ] = regex = re .compile (res )
6062 return regex .match
6163
64+
6265def filter (names , pat ):
63- """Return the subset of the list NAMES that match PAT"""
64- import os ,posixpath
66+ """Return the subset of the list NAMES that match PAT."""
6567 result = []
6668 pat = os .path .normcase (pat )
6769 match = _compile_pattern (pat )
@@ -76,16 +78,17 @@ def filter(names, pat):
7678 result .append (name )
7779 return result
7880
81+
7982def fnmatchcase (name , pat ):
8083 """Test whether FILENAME matches PATTERN, including case.
8184
8285 This is a version of fnmatch() which doesn't case-normalize
8386 its arguments.
8487 """
85-
8688 match = _compile_pattern (pat )
8789 return match (name ) is not None
8890
91+
8992def translate (pat ):
9093 """Translate a shell PATTERN to a regular expression.
9194
0 commit comments