forked from datafold/data-diff
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.py
311 lines (228 loc) · 8.03 KB
/
utils.py
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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
from typing import (
Iterable,
Iterator,
MutableMapping,
Type,
Union,
Any,
Sequence,
Dict,
TypeVar,
List,
)
from abc import abstractmethod
import math
import string
import re
from uuid import UUID
from urllib.parse import urlparse
from typing_extensions import Self
# -- Common --
def join_iter(joiner: Any, iterable: Iterable) -> Iterable:
it = iter(iterable)
try:
yield next(it)
except StopIteration:
return
for i in it:
yield joiner
yield i
def safezip(*args):
"zip but makes sure all sequences are the same length"
lens = list(map(len, args))
if len(set(lens)) != 1:
raise ValueError(f"Mismatching lengths in arguments to safezip: {lens}")
return zip(*args)
def is_uuid(u):
try:
UUID(u)
except ValueError:
return False
return True
def match_regexps(regexps: Dict[str, Any], s: str) -> Sequence[tuple]:
for regexp, v in regexps.items():
m = re.match(regexp + "$", s)
if m:
yield m, v
# -- Schema --
V = TypeVar("V")
class CaseAwareMapping(MutableMapping[str, V]):
@abstractmethod
def get_key(self, key: str) -> str:
...
def new(self, initial=()) -> Self:
return type(self)(initial)
class CaseInsensitiveDict(CaseAwareMapping):
def __init__(self, initial):
self._dict = {k.lower(): (k, v) for k, v in dict(initial).items()}
def __getitem__(self, key: str) -> V:
return self._dict[key.lower()][1]
def __iter__(self) -> Iterator[V]:
return iter(self._dict)
def __len__(self) -> int:
return len(self._dict)
def __setitem__(self, key: str, value):
k = key.lower()
if k in self._dict:
key = self._dict[k][0]
self._dict[k] = key, value
def __delitem__(self, key: str):
del self._dict[key.lower()]
def get_key(self, key: str) -> str:
return self._dict[key.lower()][0]
def __repr__(self) -> str:
return repr(dict(self.items()))
class CaseSensitiveDict(dict, CaseAwareMapping):
def get_key(self, key):
self[key] # Throw KeyError if key doesn't exist
return key
def as_insensitive(self):
return CaseInsensitiveDict(self)
# -- Alphanumerics --
alphanums = " -" + string.digits + string.ascii_uppercase + "_" + string.ascii_lowercase
class ArithString:
@classmethod
def new(cls, *args, **kw) -> Self:
return cls(*args, **kw)
def range(self, other: "ArithString", count: int) -> List[Self]:
assert isinstance(other, ArithString)
checkpoints = split_space(self.int, other.int, count)
return [self.new(int=i) for i in checkpoints]
class ArithUUID(UUID, ArithString):
"A UUID that supports basic arithmetic (add, sub)"
def __int__(self):
return self.int
def __add__(self, other: int) -> Self:
if isinstance(other, int):
return self.new(int=self.int + other)
return NotImplemented
def __sub__(self, other: Union[UUID, int]):
if isinstance(other, int):
return self.new(int=self.int - other)
elif isinstance(other, UUID):
return self.int - other.int
return NotImplemented
def numberToAlphanum(num: int, base: str = alphanums) -> str:
digits = []
while num > 0:
num, remainder = divmod(num, len(base))
digits.append(remainder)
return "".join(base[i] for i in digits[::-1])
def alphanumToNumber(alphanum: str, base: str = alphanums) -> int:
num = 0
for c in alphanum:
num = num * len(base) + base.index(c)
return num
def justify_alphanums(s1: str, s2: str):
max_len = max(len(s1), len(s2))
s1 = s1.ljust(max_len)
s2 = s2.ljust(max_len)
return s1, s2
def alphanums_to_numbers(s1: str, s2: str):
s1, s2 = justify_alphanums(s1, s2)
n1 = alphanumToNumber(s1)
n2 = alphanumToNumber(s2)
return n1, n2
class ArithAlphanumeric(ArithString):
def __init__(self, s: str, max_len=None):
if s is None:
raise ValueError("Alphanum string cannot be None")
if max_len and len(s) > max_len:
raise ValueError(f"Length of alphanum value '{str}' is longer than the expected {max_len}")
for ch in s:
if ch not in alphanums:
raise ValueError(f"Unexpected character {ch} in alphanum string")
self._str = s
self._max_len = max_len
# @property
# def int(self):
# return alphanumToNumber(self._str, alphanums)
def __str__(self):
s = self._str
if self._max_len:
s = s.rjust(self._max_len, alphanums[0])
return s
def __len__(self):
return len(self._str)
def __repr__(self):
return f'alphanum"{self._str}"'
def __add__(self, other: "Union[ArithAlphanumeric, int]") -> Self:
if isinstance(other, int):
if other != 1:
raise NotImplementedError("not implemented for arbitrary numbers")
num = alphanumToNumber(self._str)
return self.new(numberToAlphanum(num + 1))
return NotImplemented
def range(self, other: "ArithAlphanumeric", count: int) -> List[Self]:
assert isinstance(other, ArithAlphanumeric)
n1, n2 = alphanums_to_numbers(self._str, other._str)
split = split_space(n1, n2, count)
return [self.new(numberToAlphanum(s)) for s in split]
def __sub__(self, other: "Union[ArithAlphanumeric, int]") -> float:
if isinstance(other, ArithAlphanumeric):
n1, n2 = alphanums_to_numbers(self._str, other._str)
return n1 - n2
return NotImplemented
def __ge__(self, other):
if not isinstance(other, type(self)):
return NotImplemented
return self._str >= other._str
def __lt__(self, other):
if not isinstance(other, type(self)):
return NotImplemented
return self._str < other._str
def __eq__(self, other):
if not isinstance(other, type(self)):
return NotImplemented
return self._str == other._str
def new(self, *args, **kw) -> Self:
return type(self)(*args, **kw, max_len=self._max_len)
def number_to_human(n):
millnames = ["", "k", "m", "b"]
n = float(n)
millidx = max(
0,
min(len(millnames) - 1, int(math.floor(0 if n == 0 else math.log10(abs(n)) / 3))),
)
return "{:.0f}{}".format(n / 10 ** (3 * millidx), millnames[millidx])
def split_space(start, end, count) -> List[int]:
size = end - start
assert count <= size, (count, size)
return list(range(start, end, (size + 1) // (count + 1)))[1 : count + 1]
def remove_passwords_in_dict(d: dict, replace_with: str = "***"):
for k, v in d.items():
if k == "password":
d[k] = replace_with
elif isinstance(v, dict):
remove_passwords_in_dict(v, replace_with)
elif k.startswith("database"):
d[k] = remove_password_from_url(v, replace_with)
def _join_if_any(sym, args):
args = list(args)
if not args:
return ""
return sym.join(str(a) for a in args if a)
def remove_password_from_url(url: str, replace_with: str = "***") -> str:
parsed = urlparse(url)
account = parsed.username or ""
if parsed.password:
account += ":" + replace_with
host = _join_if_any(":", filter(None, [parsed.hostname, parsed.port]))
netloc = _join_if_any("@", filter(None, [account, host]))
replaced = parsed._replace(netloc=netloc)
return replaced.geturl()
def match_like(pattern: str, strs: Sequence[str]) -> Iterable[str]:
reo = re.compile(pattern.replace("%", ".*").replace("?", ".") + "$")
for s in strs:
if reo.match(s):
yield s
class UnknownMeta(type):
def __instancecheck__(self, instance):
return instance is Unknown
def __repr__(self):
return "Unknown"
class Unknown(metaclass=UnknownMeta):
def __nonzero__(self):
raise TypeError()
def __new__(class_, *args, **kwargs):
raise RuntimeError("Unknown is a singleton")