relib 1.2.0__py3-none-any.whl → 1.2.2__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- relib/__init__.py +4 -2
- relib/hashing.py +44 -120
- relib/system.py +18 -0
- relib/utils.py +42 -50
- {relib-1.2.0.dist-info → relib-1.2.2.dist-info}/METADATA +4 -3
- relib-1.2.2.dist-info/RECORD +9 -0
- {relib-1.2.0.dist-info → relib-1.2.2.dist-info}/WHEEL +1 -1
- {relib-1.2.0.dist-info → relib-1.2.2.dist-info}/licenses/LICENSE +1 -1
- relib-1.2.0.dist-info/RECORD +0 -8
relib/__init__.py
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
from .utils import (
|
2
|
-
clear_console,
|
3
2
|
non_none,
|
3
|
+
as_any,
|
4
4
|
list_split,
|
5
5
|
drop_none,
|
6
6
|
distinct,
|
@@ -25,11 +25,13 @@ from .utils import (
|
|
25
25
|
group,
|
26
26
|
reversed_enumerate,
|
27
27
|
get_at,
|
28
|
+
for_each,
|
28
29
|
sized_partitions,
|
29
30
|
num_partitions,
|
30
31
|
df_from_array,
|
31
32
|
StrFilter,
|
32
33
|
str_filterer,
|
33
34
|
)
|
34
|
-
from .
|
35
|
+
from .system import read_json, write_json, clear_console, console_link
|
36
|
+
from .hashing import hash, hash_obj
|
35
37
|
from .measure_duration import measure_duration
|
relib/hashing.py
CHANGED
@@ -1,8 +1,3 @@
|
|
1
|
-
"""
|
2
|
-
Fast cryptographic hash of Python objects, with a special case for fast
|
3
|
-
hashing of numpy arrays.
|
4
|
-
"""
|
5
|
-
|
6
1
|
# Author: Gael Varoquaux <gael dot varoquaux at normalesup dot org>
|
7
2
|
# Copyright (c) 2009 Gael Varoquaux
|
8
3
|
# License: BSD Style, 3 clauses.
|
@@ -11,30 +6,25 @@ import pickle
|
|
11
6
|
import hashlib
|
12
7
|
import sys
|
13
8
|
import types
|
14
|
-
import struct
|
15
9
|
import io
|
16
10
|
import decimal
|
17
11
|
|
12
|
+
try:
|
13
|
+
import numpy
|
14
|
+
except:
|
15
|
+
has_numpy = False
|
16
|
+
else:
|
17
|
+
has_numpy = True
|
18
|
+
|
18
19
|
Pickler = pickle._Pickler
|
19
|
-
_bytes_or_unicode = (bytes, str)
|
20
20
|
|
21
21
|
|
22
22
|
class _ConsistentSet(object):
|
23
|
-
""" Class used to ensure the hash of Sets is preserved
|
24
|
-
whatever the order of its items.
|
25
|
-
"""
|
26
23
|
def __init__(self, set_sequence):
|
27
|
-
# Forces order of elements in set to ensure consistent hash.
|
28
24
|
try:
|
29
|
-
# Trying first to order the set assuming the type of elements is
|
30
|
-
# consistent and orderable.
|
31
|
-
# This fails on python 3 when elements are unorderable
|
32
|
-
# but we keep it in a try as it's faster.
|
33
25
|
self._sequence = sorted(set_sequence)
|
34
26
|
except (TypeError, decimal.InvalidOperation):
|
35
|
-
|
36
|
-
# This is slower but works in any case.
|
37
|
-
self._sequence = sorted((hash(e) for e in set_sequence))
|
27
|
+
self._sequence = sorted(map(hash_obj, set_sequence))
|
38
28
|
|
39
29
|
|
40
30
|
class _MyHash(object):
|
@@ -45,35 +35,30 @@ class _MyHash(object):
|
|
45
35
|
|
46
36
|
|
47
37
|
class Hasher(Pickler):
|
48
|
-
""" A subclass of pickler, to do cryptographic hashing, rather than
|
49
|
-
pickling.
|
50
|
-
"""
|
38
|
+
""" A subclass of pickler, to do cryptographic hashing, rather than pickling. """
|
51
39
|
|
52
|
-
def __init__(self, hash_name=
|
40
|
+
def __init__(self, hash_name="md5"):
|
53
41
|
self.stream = io.BytesIO()
|
54
|
-
#
|
55
|
-
# the major python version and not the minor one
|
42
|
+
# We want a pickle protocol that only changes with major Python versions
|
56
43
|
protocol = pickle.HIGHEST_PROTOCOL
|
57
44
|
Pickler.__init__(self, self.stream, protocol=protocol)
|
58
|
-
# Initialise the hash obj
|
59
45
|
self._hash = hashlib.new(hash_name)
|
60
46
|
|
61
|
-
def hash(self, obj
|
47
|
+
def hash(self, obj) -> str:
|
62
48
|
try:
|
63
49
|
self.dump(obj)
|
64
50
|
except pickle.PicklingError as e:
|
65
|
-
e.args += (
|
51
|
+
e.args += ("PicklingError while hashing %r: %r" % (obj, e),)
|
66
52
|
raise
|
67
53
|
dumps = self.stream.getvalue()
|
68
54
|
self._hash.update(dumps)
|
69
|
-
|
70
|
-
return self._hash.hexdigest()
|
55
|
+
return self._hash.hexdigest()
|
71
56
|
|
72
57
|
def save(self, obj):
|
73
58
|
if isinstance(obj, (types.MethodType, type({}.pop))):
|
74
59
|
# the Pickler cannot pickle instance methods; here we decompose
|
75
60
|
# them into components that make them uniquely identifiable
|
76
|
-
if hasattr(obj,
|
61
|
+
if hasattr(obj, "__func__"):
|
77
62
|
func_name = obj.__func__.__name__
|
78
63
|
else:
|
79
64
|
func_name = obj.__name__
|
@@ -90,28 +75,25 @@ class Hasher(Pickler):
|
|
90
75
|
|
91
76
|
def memoize(self, obj):
|
92
77
|
# We want hashing to be sensitive to value instead of reference.
|
93
|
-
# For example we want [
|
78
|
+
# For example we want ["aa", "aa"] and ["aa", "aaZ"[:2]]
|
94
79
|
# to hash to the same value and that's why we disable memoization
|
95
80
|
# for strings
|
96
|
-
if isinstance(obj,
|
81
|
+
if isinstance(obj, (bytes, str)):
|
97
82
|
return
|
98
83
|
Pickler.memoize(self, obj)
|
99
84
|
|
100
85
|
# The dispatch table of the pickler is not accessible in Python
|
101
86
|
# 3, as these lines are only bugware for IPython, we skip them.
|
102
|
-
def save_global(self, obj, name=None
|
87
|
+
def save_global(self, obj, name=None):
|
103
88
|
# We have to override this method in order to deal with objects
|
104
89
|
# defined interactively in IPython that are not injected in
|
105
90
|
# __main__
|
106
|
-
kwargs = dict(name=name, pack=pack)
|
107
|
-
if sys.version_info >= (3, 4):
|
108
|
-
del kwargs['pack']
|
109
91
|
try:
|
110
|
-
Pickler.save_global(self, obj,
|
92
|
+
Pickler.save_global(self, obj, name=name)
|
111
93
|
except pickle.PicklingError:
|
112
|
-
Pickler.save_global(self, obj,
|
94
|
+
Pickler.save_global(self, obj, name=name)
|
113
95
|
module = getattr(obj, "__module__", None)
|
114
|
-
if module ==
|
96
|
+
if module == "__main__":
|
115
97
|
my_name = name
|
116
98
|
if my_name is None:
|
117
99
|
my_name = obj.__name__
|
@@ -121,67 +103,35 @@ class Hasher(Pickler):
|
|
121
103
|
# interactively in __main__
|
122
104
|
setattr(mod, my_name, obj)
|
123
105
|
|
124
|
-
dispatch = Pickler.dispatch.copy()
|
125
|
-
# builtin
|
126
|
-
dispatch[type(len)] = save_global
|
127
|
-
# type
|
128
|
-
dispatch[type(object)] = save_global
|
129
|
-
# classobj
|
130
|
-
dispatch[type(Pickler)] = save_global
|
131
|
-
# function
|
132
|
-
dispatch[type(pickle.dump)] = save_global
|
133
|
-
|
134
106
|
def _batch_setitems(self, items):
|
135
|
-
# forces order of keys in dict to ensure consistent hash.
|
136
107
|
try:
|
137
|
-
# Trying first to compare dict assuming the type of keys is
|
138
|
-
# consistent and orderable.
|
139
|
-
# This fails on python 3 when keys are unorderable
|
140
|
-
# but we keep it in a try as it's faster.
|
141
108
|
Pickler._batch_setitems(self, iter(sorted(items)))
|
142
109
|
except TypeError:
|
143
|
-
|
144
|
-
# slower but works in any case.
|
145
|
-
Pickler._batch_setitems(self, iter(sorted((hash(k), v)
|
146
|
-
for k, v in items)))
|
110
|
+
Pickler._batch_setitems(self, iter(sorted((hash_obj(k), v) for k, v in items)))
|
147
111
|
|
148
112
|
def save_set(self, set_items):
|
149
|
-
# forces order of items in Set to ensure consistent hash
|
150
113
|
Pickler.save(self, _ConsistentSet(set_items))
|
151
114
|
|
115
|
+
dispatch = Pickler.dispatch.copy()
|
116
|
+
dispatch[type(len)] = save_global # builtin
|
117
|
+
dispatch[type(object)] = save_global # type
|
118
|
+
dispatch[type(Pickler)] = save_global # classobj
|
119
|
+
dispatch[type(pickle.dump)] = save_global # function
|
152
120
|
dispatch[type(set())] = save_set
|
153
121
|
|
154
122
|
|
155
123
|
class NumpyHasher(Hasher):
|
156
|
-
""
|
157
|
-
"""
|
158
|
-
|
159
|
-
def __init__(self, hash_name='md5', coerce_mmap=False):
|
160
|
-
"""
|
161
|
-
Parameters
|
162
|
-
----------
|
163
|
-
hash_name: string
|
164
|
-
The hash algorithm to be used
|
165
|
-
coerce_mmap: boolean
|
166
|
-
Make no difference between np.memmap and np.ndarray
|
167
|
-
objects.
|
168
|
-
"""
|
169
|
-
self.coerce_mmap = coerce_mmap
|
124
|
+
def __init__(self, hash_name="md5"):
|
170
125
|
Hasher.__init__(self, hash_name=hash_name)
|
171
|
-
# delayed import of numpy, to avoid tight coupling
|
172
|
-
import numpy as np
|
173
|
-
self.np = np
|
174
|
-
if hasattr(np, 'getbuffer'):
|
175
|
-
self._getbuffer = np.getbuffer
|
176
|
-
else:
|
177
|
-
self._getbuffer = memoryview
|
178
126
|
|
179
127
|
def save(self, obj):
|
180
128
|
""" Subclass the save method, to hash ndarray subclass, rather
|
181
129
|
than pickling them. Off course, this is a total abuse of
|
182
130
|
the Pickler class.
|
183
131
|
"""
|
184
|
-
|
132
|
+
import numpy as np
|
133
|
+
|
134
|
+
if isinstance(obj, np.ndarray) and not obj.dtype.hasobject:
|
185
135
|
# Compute a hash of the object
|
186
136
|
# The update function of the hash requires a c_contiguous buffer.
|
187
137
|
if obj.shape == ():
|
@@ -198,31 +148,14 @@ class NumpyHasher(Hasher):
|
|
198
148
|
# XXX: There might be a more efficient way of doing this
|
199
149
|
obj_c_contiguous = obj.flatten()
|
200
150
|
|
201
|
-
#
|
202
|
-
|
203
|
-
# workaround is to view the array as bytes before
|
204
|
-
# taking the memoryview.
|
205
|
-
self._hash.update(
|
206
|
-
self._getbuffer(obj_c_contiguous.view(self.np.uint8)))
|
207
|
-
|
208
|
-
# We store the class, to be able to distinguish between
|
209
|
-
# Objects with the same binary content, but different
|
210
|
-
# classes.
|
211
|
-
if self.coerce_mmap and isinstance(obj, self.np.memmap):
|
212
|
-
# We don't make the difference between memmap and
|
213
|
-
# normal ndarrays, to be able to reload previously
|
214
|
-
# computed results with memmap.
|
215
|
-
klass = self.np.ndarray
|
216
|
-
else:
|
217
|
-
klass = obj.__class__
|
218
|
-
# We also return the dtype and the shape, to distinguish
|
219
|
-
# different views on the same data with different dtypes.
|
151
|
+
# View the array as bytes to support dtypes like datetime64
|
152
|
+
self._hash.update(memoryview(obj_c_contiguous.view(np.uint8)))
|
220
153
|
|
221
154
|
# The object will be pickled by the pickler hashed at the end.
|
222
|
-
obj = (
|
223
|
-
elif isinstance(obj,
|
155
|
+
obj = (obj.__class__, ("HASHED", obj.dtype, obj.shape, obj.strides))
|
156
|
+
elif isinstance(obj, np.dtype):
|
224
157
|
# Atomic dtype objects are interned by their default constructor:
|
225
|
-
# np.dtype(
|
158
|
+
# np.dtype("f8") is np.dtype("f8")
|
226
159
|
# This interning is not maintained by a
|
227
160
|
# pickle.loads + pickle.dumps cycle, because __reduce__
|
228
161
|
# uses copy=True in the dtype constructor. This
|
@@ -232,24 +165,15 @@ class NumpyHasher(Hasher):
|
|
232
165
|
# To prevent the hash from being sensitive to this, we use
|
233
166
|
# .descr which is a full (and never interned) description of
|
234
167
|
# the array dtype according to the numpy doc.
|
235
|
-
|
236
|
-
|
168
|
+
obj = (obj.__class__, ("HASHED", obj.descr))
|
169
|
+
|
237
170
|
Hasher.save(self, obj)
|
238
171
|
|
239
172
|
|
240
|
-
def
|
241
|
-
|
242
|
-
|
243
|
-
Parameters
|
244
|
-
-----------
|
245
|
-
hash_name: 'md5' or 'sha1'
|
246
|
-
Hashing algorithm used. sha1 is supposedly safer, but md5 is
|
247
|
-
faster.
|
248
|
-
coerce_mmap: boolean
|
249
|
-
Make no difference between np.memmap and np.ndarray
|
250
|
-
"""
|
251
|
-
if 'numpy' in sys.modules:
|
252
|
-
hasher = NumpyHasher(hash_name=hash_name, coerce_mmap=coerce_mmap)
|
173
|
+
def hash_obj(obj, hash_name="md5") -> str:
|
174
|
+
if has_numpy:
|
175
|
+
return NumpyHasher(hash_name=hash_name).hash(obj)
|
253
176
|
else:
|
254
|
-
|
255
|
-
|
177
|
+
return Hasher(hash_name=hash_name).hash(obj)
|
178
|
+
|
179
|
+
hash = hash_obj
|
relib/system.py
ADDED
@@ -0,0 +1,18 @@
|
|
1
|
+
import json
|
2
|
+
import os
|
3
|
+
from pathlib import Path
|
4
|
+
from typing import Any
|
5
|
+
|
6
|
+
def read_json(path: Path) -> Any:
|
7
|
+
with path.open("r") as f:
|
8
|
+
return json.load(f)
|
9
|
+
|
10
|
+
def write_json(path: Path, obj: Any) -> None:
|
11
|
+
with path.open("w") as f:
|
12
|
+
return json.dump(obj, f)
|
13
|
+
|
14
|
+
def clear_console() -> None:
|
15
|
+
os.system("cls" if os.name == "nt" else "clear")
|
16
|
+
|
17
|
+
def console_link(text, url) -> str:
|
18
|
+
return f"\033]8;;{url}\033\\{text}\033]8;;\033\\"
|
relib/utils.py
CHANGED
@@ -1,21 +1,15 @@
|
|
1
|
-
import os
|
2
1
|
import re
|
3
|
-
from typing import
|
2
|
+
from typing import Iterable, Callable, Any, overload
|
4
3
|
from itertools import chain
|
5
4
|
|
6
|
-
T
|
7
|
-
U = TypeVar('U')
|
8
|
-
K = TypeVar('K')
|
9
|
-
K1, K2, K3, K4, K5, K6 = TypeVar('K1'), TypeVar('K2'), TypeVar('K3'), TypeVar('K4'), TypeVar('K5'), TypeVar('K6')
|
10
|
-
|
11
|
-
def clear_console():
|
12
|
-
os.system("cls" if os.name == "nt" else "clear")
|
13
|
-
|
14
|
-
def non_none(obj: T | None) -> T:
|
5
|
+
def non_none[T](obj: T | None) -> T:
|
15
6
|
assert obj is not None
|
16
7
|
return obj
|
17
8
|
|
18
|
-
def
|
9
|
+
def as_any(obj: Any) -> Any:
|
10
|
+
return obj
|
11
|
+
|
12
|
+
def list_split[T](l: list[T], sep: T) -> list[list[T]]:
|
19
13
|
l = [sep, *l, sep]
|
20
14
|
split_at = [i for i, x in enumerate(l) if x is sep]
|
21
15
|
ranges = list(zip(split_at[0:-1], split_at[1:]))
|
@@ -24,17 +18,17 @@ def list_split(l: list[T], sep: T) -> list[list[T]]:
|
|
24
18
|
for start, end in ranges
|
25
19
|
]
|
26
20
|
|
27
|
-
def drop_none(
|
28
|
-
return [x for x in
|
21
|
+
def drop_none[T](iterable: Iterable[T | None]) -> list[T]:
|
22
|
+
return [x for x in iterable if x is not None]
|
29
23
|
|
30
|
-
def distinct(items: Iterable[T]) -> list[T]:
|
31
|
-
return list(
|
24
|
+
def distinct[T](items: Iterable[T]) -> list[T]:
|
25
|
+
return list(dict.fromkeys(items))
|
32
26
|
|
33
|
-
def first(iterable: Iterable[T]) -> T | None:
|
27
|
+
def first[T](iterable: Iterable[T]) -> T | None:
|
34
28
|
return next(iter(iterable), None)
|
35
29
|
|
36
|
-
def move_value(
|
37
|
-
l = list(
|
30
|
+
def move_value[T](iterable: Iterable[T], from_i: int, to_i: int) -> list[T]:
|
31
|
+
l = list(iterable)
|
38
32
|
l.insert(to_i, l.pop(from_i))
|
39
33
|
return l
|
40
34
|
|
@@ -53,7 +47,7 @@ def transpose_dict(des):
|
|
53
47
|
{key: des[key][i] for key in keys}
|
54
48
|
for i in range(length)
|
55
49
|
]
|
56
|
-
raise ValueError(
|
50
|
+
raise ValueError("transpose_dict only accepts dict or list")
|
57
51
|
|
58
52
|
def make_combinations_by_dict(des, keys=None, pairs=[]):
|
59
53
|
keys = sorted(des.keys()) if keys == None else keys
|
@@ -67,7 +61,7 @@ def make_combinations_by_dict(des, keys=None, pairs=[]):
|
|
67
61
|
for pair in new_pairs
|
68
62
|
])
|
69
63
|
|
70
|
-
def merge_dicts(*dicts: dict[K, T]) -> dict[K, T]:
|
64
|
+
def merge_dicts[T, K](*dicts: dict[K, T]) -> dict[K, T]:
|
71
65
|
if len(dicts) == 1:
|
72
66
|
return dicts[0]
|
73
67
|
result = {}
|
@@ -75,33 +69,33 @@ def merge_dicts(*dicts: dict[K, T]) -> dict[K, T]:
|
|
75
69
|
result.update(d)
|
76
70
|
return result
|
77
71
|
|
78
|
-
def intersect(*
|
79
|
-
return list(set.intersection(*map(set,
|
72
|
+
def intersect[T](*iterables: Iterable[T]) -> list[T]:
|
73
|
+
return list(set.intersection(*map(set, iterables)))
|
80
74
|
|
81
|
-
def ensure_tuple(value: T | tuple[T, ...]) -> tuple[T, ...]:
|
75
|
+
def ensure_tuple[T](value: T | tuple[T, ...]) -> tuple[T, ...]:
|
82
76
|
return value if isinstance(value, tuple) else (value,)
|
83
77
|
|
84
|
-
def key_of(dicts: Iterable[dict[T, U]], key: T) -> list[U]:
|
78
|
+
def key_of[T, U](dicts: Iterable[dict[T, U]], key: T) -> list[U]:
|
85
79
|
return [d[key] for d in dicts]
|
86
80
|
|
87
|
-
def omit(d: dict[K, T], keys: Iterable[K]) -> dict[K, T]:
|
81
|
+
def omit[T, K](d: dict[K, T], keys: Iterable[K]) -> dict[K, T]:
|
88
82
|
if keys:
|
89
83
|
d = dict(d)
|
90
84
|
for key in keys:
|
91
85
|
del d[key]
|
92
86
|
return d
|
93
87
|
|
94
|
-
def pick(d: dict[K, T], keys: Iterable[K]) -> dict[K, T]:
|
88
|
+
def pick[T, K](d: dict[K, T], keys: Iterable[K]) -> dict[K, T]:
|
95
89
|
return {key: d[key] for key in keys}
|
96
90
|
|
97
|
-
def dict_by(keys: Iterable[K], values: Iterable[T]) -> dict[K, T]:
|
91
|
+
def dict_by[T, K](keys: Iterable[K], values: Iterable[T]) -> dict[K, T]:
|
98
92
|
return dict(zip(keys, values))
|
99
93
|
|
100
|
-
def tuple_by(d: dict[K, T], keys: Iterable[K]) -> tuple[T, ...]:
|
94
|
+
def tuple_by[T, K](d: dict[K, T], keys: Iterable[K]) -> tuple[T, ...]:
|
101
95
|
return tuple(d[key] for key in keys)
|
102
96
|
|
103
|
-
def flatten(
|
104
|
-
return list(chain.from_iterable(
|
97
|
+
def flatten[T](iterable: Iterable[Iterable[T]]) -> list[T]:
|
98
|
+
return list(chain.from_iterable(iterable))
|
105
99
|
|
106
100
|
def transpose(tuples, default_num_returns=0):
|
107
101
|
output = tuple(zip(*tuples))
|
@@ -109,27 +103,21 @@ def transpose(tuples, default_num_returns=0):
|
|
109
103
|
return ([],) * default_num_returns
|
110
104
|
return tuple(map(list, output))
|
111
105
|
|
112
|
-
def map_dict(fn: Callable[[T], U], d: dict[K, T]) -> dict[K, U]:
|
106
|
+
def map_dict[T, U, K](fn: Callable[[T], U], d: dict[K, T]) -> dict[K, U]:
|
113
107
|
return {key: fn(value) for key, value in d.items()}
|
114
108
|
|
115
109
|
@overload
|
116
|
-
def deepen_dict(d: dict[tuple[K1], U]) -> dict[K1, U]: ...
|
117
|
-
|
110
|
+
def deepen_dict[K1, U](d: dict[tuple[K1], U]) -> dict[K1, U]: ...
|
118
111
|
@overload
|
119
|
-
def deepen_dict(d: dict[tuple[K1, K2], U]) -> dict[K1, dict[K2, U]]: ...
|
120
|
-
|
112
|
+
def deepen_dict[K1, K2, U](d: dict[tuple[K1, K2], U]) -> dict[K1, dict[K2, U]]: ...
|
121
113
|
@overload
|
122
|
-
def deepen_dict(d: dict[tuple[K1, K2, K3], U]) -> dict[K1, dict[K2, dict[K3, U]]]: ...
|
123
|
-
|
114
|
+
def deepen_dict[K1, K2, K3, U](d: dict[tuple[K1, K2, K3], U]) -> dict[K1, dict[K2, dict[K3, U]]]: ...
|
124
115
|
@overload
|
125
|
-
def deepen_dict(d: dict[tuple[K1, K2, K3, K4], U]) -> dict[K1, dict[K2, dict[K3, dict[K4, U]]]]: ...
|
126
|
-
|
116
|
+
def deepen_dict[K1, K2, K3, K4, U](d: dict[tuple[K1, K2, K3, K4], U]) -> dict[K1, dict[K2, dict[K3, dict[K4, U]]]]: ...
|
127
117
|
@overload
|
128
|
-
def deepen_dict(d: dict[tuple[K1, K2, K3, K4, K5], U]) -> dict[K1, dict[K2, dict[K3, dict[K4, dict[K5, U]]]]]: ...
|
129
|
-
|
118
|
+
def deepen_dict[K1, K2, K3, K4, K5, U](d: dict[tuple[K1, K2, K3, K4, K5], U]) -> dict[K1, dict[K2, dict[K3, dict[K4, dict[K5, U]]]]]: ...
|
130
119
|
@overload
|
131
|
-
def deepen_dict(d: dict[tuple[K1, K2, K3, K4, K5, K6], U]) -> dict[K1, dict[K2, dict[K3, dict[K4, dict[K5, dict[K6, U]]]]]]: ...
|
132
|
-
|
120
|
+
def deepen_dict[K1, K2, K3, K4, K5, K6, U](d: dict[tuple[K1, K2, K3, K4, K5, K6], U]) -> dict[K1, dict[K2, dict[K3, dict[K4, dict[K5, dict[K6, U]]]]]]: ...
|
133
121
|
def deepen_dict(d: dict[tuple[Any, ...], Any]) -> dict:
|
134
122
|
output = {}
|
135
123
|
if () in d:
|
@@ -151,31 +139,35 @@ def flatten_dict_inner(d, prefix=()):
|
|
151
139
|
def flatten_dict(deep_dict: dict, prefix=()) -> dict:
|
152
140
|
return dict(flatten_dict_inner(deep_dict, prefix))
|
153
141
|
|
154
|
-
def group(pairs: Iterable[tuple[K, T]]) -> dict[K, list[T]]:
|
142
|
+
def group[T, K](pairs: Iterable[tuple[K, T]]) -> dict[K, list[T]]:
|
155
143
|
values_by_key = {}
|
156
144
|
for key, value in pairs:
|
157
145
|
values_by_key.setdefault(key, []).append(value)
|
158
146
|
return values_by_key
|
159
147
|
|
160
|
-
def reversed_enumerate(l: list[T] | tuple[T, ...]) -> Iterable[tuple[int, T]]:
|
148
|
+
def reversed_enumerate[T](l: list[T] | tuple[T, ...]) -> Iterable[tuple[int, T]]:
|
161
149
|
return zip(reversed(range(len(l))), reversed(l))
|
162
150
|
|
163
|
-
def get_at(d: dict, keys: Iterable[Any], default: T) -> T:
|
151
|
+
def get_at[T](d: dict, keys: Iterable[Any], default: T) -> T:
|
164
152
|
try:
|
165
153
|
for key in keys:
|
166
154
|
d = d[key]
|
167
155
|
except KeyError:
|
168
156
|
return default
|
169
|
-
return
|
157
|
+
return as_any(d)
|
158
|
+
|
159
|
+
def for_each[T](func: Callable[[T]], iterable: Iterable[T]) -> None:
|
160
|
+
for item in iterable:
|
161
|
+
func(item)
|
170
162
|
|
171
|
-
def sized_partitions(values: Iterable[T], part_size: int) -> list[list[T]]:
|
163
|
+
def sized_partitions[T](values: Iterable[T], part_size: int) -> list[list[T]]:
|
172
164
|
# "chunk"
|
173
165
|
if not isinstance(values, list):
|
174
166
|
values = list(values)
|
175
167
|
num_parts = (len(values) / part_size).__ceil__()
|
176
168
|
return [values[i * part_size:(i + 1) * part_size] for i in range(num_parts)]
|
177
169
|
|
178
|
-
def num_partitions(values: Iterable[T], num_parts: int) -> list[list[T]]:
|
170
|
+
def num_partitions[T](values: Iterable[T], num_parts: int) -> list[list[T]]:
|
179
171
|
if not isinstance(values, list):
|
180
172
|
values = list(values)
|
181
173
|
part_size = (len(values) / num_parts).__ceil__()
|
@@ -1,13 +1,14 @@
|
|
1
|
-
Metadata-Version: 2.
|
1
|
+
Metadata-Version: 2.4
|
2
2
|
Name: relib
|
3
|
-
Version: 1.2.
|
3
|
+
Version: 1.2.2
|
4
4
|
Project-URL: Repository, https://github.com/Reddan/relib.git
|
5
5
|
Author: Hampus Hallman
|
6
|
-
License: Copyright
|
6
|
+
License: Copyright 2018-2025 Hampus Hallman
|
7
7
|
|
8
8
|
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
|
9
9
|
|
10
10
|
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
|
11
11
|
|
12
12
|
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
13
|
+
License-File: LICENSE
|
13
14
|
Requires-Python: >=3.12
|
@@ -0,0 +1,9 @@
|
|
1
|
+
relib/__init__.py,sha256=dLFft8umAfLeZfTiecZ2Cx_-C-nKoBepUk-aWivI5ZE,627
|
2
|
+
relib/hashing.py,sha256=DB_fnkj0ls01FgZbf4nPFHl4EBU8X_0OrmDvty4HlRE,6020
|
3
|
+
relib/measure_duration.py,sha256=LCTo_D_qReNprD3fhtJ0daeWycS6xQE_cwxeg2_h0xo,456
|
4
|
+
relib/system.py,sha256=H-SJccCVLNTDhWTT5jo1NFUiQJzHv2Z1xvq0OVrnJcM,431
|
5
|
+
relib/utils.py,sha256=GXvz4BPvEXzOxqCg1hV2F2x1xmaftlSdfCobaF0UBK8,6900
|
6
|
+
relib-1.2.2.dist-info/METADATA,sha256=tl7KPaFKh8TTJ9XPpHbvwmpdjpbLWu_q4Nq3jfFsNkE,1295
|
7
|
+
relib-1.2.2.dist-info/WHEEL,sha256=qtCwoSJWgHk21S1Kb4ihdzI2rlJ1ZKaIurTj_ngOhyQ,87
|
8
|
+
relib-1.2.2.dist-info/licenses/LICENSE,sha256=9xVsdtv_-uSyY9Xl9yujwAPm4-mjcCLeVy-ljwXEWbo,1059
|
9
|
+
relib-1.2.2.dist-info/RECORD,,
|
@@ -1,4 +1,4 @@
|
|
1
|
-
Copyright
|
1
|
+
Copyright 2018-2025 Hampus Hallman
|
2
2
|
|
3
3
|
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
|
4
4
|
|
relib-1.2.0.dist-info/RECORD
DELETED
@@ -1,8 +0,0 @@
|
|
1
|
-
relib/__init__.py,sha256=BX3u0WkfNZhplVWOYvDwoAOWqpxSx2XZuJ1hw-x7Xy4,541
|
2
|
-
relib/hashing.py,sha256=A3uNivDp51WRfNgvDiYDeAyIutG9GracQIzDhQkykdI,8950
|
3
|
-
relib/measure_duration.py,sha256=LCTo_D_qReNprD3fhtJ0daeWycS6xQE_cwxeg2_h0xo,456
|
4
|
-
relib/utils.py,sha256=qr1NSOI6Q41JKC9_lqPnCyXdh8J-4z_nHs5u-F_-ULg,6769
|
5
|
-
relib-1.2.0.dist-info/METADATA,sha256=4WvNiywokzHmiv6SaQ_0hOMOlbxWfp1JFOV20xNCOfk,1268
|
6
|
-
relib-1.2.0.dist-info/WHEEL,sha256=C2FUgwZgiLbznR-k0b_5k3Ai_1aASOXDss3lzCUsUug,87
|
7
|
-
relib-1.2.0.dist-info/licenses/LICENSE,sha256=t9LfkVbmcvZjP0x3Sq-jR38UfTNbNtRQvc0Q8HWmLak,1054
|
8
|
-
relib-1.2.0.dist-info/RECORD,,
|