-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnarrow.py
More file actions
executable file
·41 lines (35 loc) · 1.11 KB
/
narrow.py
File metadata and controls
executable file
·41 lines (35 loc) · 1.11 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
import re
def reg(pat, flags=0):
return re.compile(pat, re.VERBOSE | flags)
def matches(s, pat, *, case='smart', verbose=False):
inverted = True
if len(pat) > 0 and pat[0] == "!":
inverted = False
pat = pat[1:]
flags = 0
if case == 'off':
if verbose:
print('case==of')
flags = re.IGNORECASE
elif case == 'on':
if verbose:
print('case==on')
elif case == 'smart': # smart case
if verbose:
print('case==smart')
if not any(x.isupper() for x in pat):
if verbose:
print('smartcase-ignore')
flags = re.IGNORECASE
else:
raise ValueError('unknown case specification %s' % case)
x = reg('(?:.*)' + pat, flags).match(s)
return inverted == (not (not x))
def narrow(strings, searches, **kws):
if isinstance(strings, str):
return len(narrow([strings], searches, **kws)) > 0
if isinstance(searches, str):
searches = [searches]
for search in searches:
strings = [s for s in strings if matches(s, search, **kws)]
return strings