-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhelpers.py
More file actions
52 lines (38 loc) · 1.25 KB
/
helpers.py
File metadata and controls
52 lines (38 loc) · 1.25 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
42
43
44
45
46
47
48
49
50
51
52
import os
from copy import deepcopy
def blacklist_filter_dict(d: dict[str, str], fpl: list[str]) -> dict[str, str]:
"""
Remove all items from `d` prefixed with a string matching a str in `fpl`
Args:
d (dict[str, str]): dictionary to operate on
fpl (list[str]): list of forbidden prefixes used for blacklist
Returns:
dict[str, str]: return modified dictionary
"""
d = deepcopy(d)
for k in list(d.keys()):
for fp in fpl:
if k.startswith(fp):
d.pop(k)
break
return d
def next_path(path_pattern):
"""
Finds the next free path in an sequentially named list of files
e.g. path_pattern = 'file-%s.txt':
file-1.txt
file-2.txt
file-3.txt
Runs in log(n) time where n is the number of existing files in sequence
"""
i = 1
# First do an exponential search
while os.path.exists(path_pattern % i):
i = i * 2
# Result lies somewhere in the interval (i/2..i]
# We call this interval (a..b] and narrow it down until a + 1 = b
a, b = (i // 2, i)
while a + 1 < b:
c = (a + b) // 2 # interval midpoint
a, b = (c, b) if os.path.exists(path_pattern % c) else (a, c)
return path_pattern % b