relib 0.2.1__py3-none-any.whl → 0.2.3__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 CHANGED
@@ -1,4 +1,5 @@
1
- from .utils import distinct, find, transpose_dict, make_combinations_by_dict, merge_dicts, intersect, ensure_tuple, omit, pick, flatten, transpose, deepen_dict, group
1
+ from .utils import list_split, drop_none, distinct, find, transpose_dict, make_combinations_by_dict, \
2
+ merge_dicts, intersect, ensure_tuple, omit, dict_by, tuple_by, flatten, transpose, deepen_dict, group
2
3
  from .raypipe import raypipe
3
4
  from .hashing import hash
4
5
  from .measure_duration import measure_duration
relib/utils.py CHANGED
@@ -1,20 +1,43 @@
1
- def distinct(items):
1
+ from typing import TypeVar, Union, Sequence, Generator, Any
2
+
3
+ T = TypeVar('T')
4
+ U = TypeVar('U')
5
+
6
+ def list_split(l: list[T], sep: T) -> list[list[T]]:
7
+ l = [sep, *l, sep]
8
+ split_at = [i for i, x in enumerate(l) if x is sep]
9
+ ranges = list(zip(split_at[0:-1], split_at[1:]))
10
+ result: Any = [
11
+ l[start + 1:end]
12
+ for start, end in ranges
13
+ ]
14
+ return result
15
+
16
+ def drop_none(l: list[Union[T, None]]) -> list[T]:
17
+ return [x for x in l if x is not None]
18
+
19
+ def distinct(items: list[T]) -> list[T]:
2
20
  return list(set(items))
3
21
 
4
- def find(iterable):
22
+ def find(iterable: Union[Sequence[T], Generator[T, None, None]]) -> Union[T, None]:
5
23
  return next(iterable, None)
6
24
 
7
25
  def transpose_dict(des):
8
- keys = list(des.keys())
9
- length = len(des[keys[0]])
10
-
11
- return [
12
- {
13
- key: des[key][i]
26
+ if isinstance(des, list):
27
+ keys = list(des[0].keys()) if des else []
28
+ length = len(des)
29
+ return {
30
+ key: [des[i][key] for i in range(length)]
14
31
  for key in keys
15
32
  }
16
- for i in range(length)
17
- ]
33
+ elif isinstance(des, dict):
34
+ keys = list(des.keys())
35
+ length = len(des[keys[0]]) if keys else 0
36
+ return [
37
+ {key: des[key][i] for key in keys}
38
+ for i in range(length)
39
+ ]
40
+ raise ValueError('transpose_dict only accepts dict or list')
18
41
 
19
42
  def make_combinations_by_dict(des, keys=None, pairs=[]):
20
43
  keys = sorted(des.keys()) if keys == None else keys
@@ -23,38 +46,40 @@ def make_combinations_by_dict(des, keys=None, pairs=[]):
23
46
  key = keys[0]
24
47
  remaining_keys = keys[1:]
25
48
  new_pairs = [(key, val) for val in des[key]]
26
- return flatten(
27
- [make_combinations_by_dict(des, remaining_keys, [pair] + pairs) for pair in new_pairs]
28
- )
49
+ return flatten([
50
+ make_combinations_by_dict(des, remaining_keys, [pair] + pairs)
51
+ for pair in new_pairs
52
+ ])
29
53
 
30
- def merge_dicts(*dicts):
54
+ def merge_dicts(*dicts: dict[T, U]) -> dict[T, U]:
31
55
  result = {}
32
56
  for dictionary in dicts:
33
57
  result.update(dictionary)
34
58
  return result
35
59
 
36
- def intersect(*lists):
60
+ def intersect(*lists: list[T]) -> list[T]:
37
61
  return set.intersection(*map(set, lists))
38
62
 
39
- def ensure_tuple(value):
63
+ def ensure_tuple(value: Union[T, tuple[T, ...]]) -> tuple[T, ...]:
40
64
  if isinstance(value, tuple):
41
65
  return value
42
66
  return (value,)
43
67
 
44
- def omit(d, keys):
68
+ def omit(d: dict[T, U], keys: Sequence[T]) -> dict[T, U]:
45
69
  if keys:
46
70
  d = dict(d)
47
71
  for key in keys:
48
72
  del d[key]
49
73
  return d
50
74
 
51
- def pick(d, keys):
75
+ def dict_by(keys: Sequence[T], values: Sequence[U]) -> dict[T, U]:
76
+ return dict(zip(keys, values))
77
+
78
+ def tuple_by(d: dict[T, U], keys: Sequence[T]) -> tuple[U, ...]:
52
79
  return tuple(d[key] for key in keys)
53
80
 
54
- def flatten(l, iterations=1):
55
- if iterations == 0:
56
- return l
57
- return flatten([value for inner_list in l for value in inner_list], iterations - 1)
81
+ def flatten(l: list[list[T]]) -> list[T]:
82
+ return [value for inner_list in l for value in inner_list]
58
83
 
59
84
  def transpose(tuples, default_num_returns=0):
60
85
  result = tuple(zip(*tuples))
@@ -73,7 +98,7 @@ def deepen_dict(d):
73
98
  curr[head] = value
74
99
  return result
75
100
 
76
- def group(pairs):
101
+ def group(pairs: Union[Sequence[tuple[T, U]], Generator[tuple[T, U], None, None]]) -> dict[T, list[U]]:
77
102
  values_by_key = {}
78
103
  for key, value in pairs:
79
104
  if key not in values_by_key:
@@ -1,4 +1,4 @@
1
- Copyright 2018 Hampus Hallman
1
+ Copyright 2023 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
 
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: relib
3
- Version: 0.2.1
3
+ Version: 0.2.3
4
4
  Home-page: https://github.com/Reddan/relib
5
5
  Author: Hampus Hallman
6
6
  Author-email: me@hampushallman.com
@@ -0,0 +1,10 @@
1
+ relib/__init__.py,sha256=OmN7DRgA2rYN0XMamE4P3rJPGJIFpnkFQoDXn-GK6sU,309
2
+ relib/hashing.py,sha256=6iAPRiJI_4jaSooZRFJnqK2limXqTmErzcwpd050LAA,8943
3
+ relib/measure_duration.py,sha256=jJa5Kh5FxaBysS__nkwqcnTt8Uc2niLucXfTzFE0j-E,555
4
+ relib/raypipe.py,sha256=ynEoXs1dnD-360_uQC8v89xjiilt3knpocXpFaQ3plA,1905
5
+ relib/utils.py,sha256=hJ0A3N-H3PaPmT06wWa003FGMlzHMoUOs-Wja49369o,3073
6
+ relib-0.2.3.dist-info/LICENSE.txt,sha256=t9LfkVbmcvZjP0x3Sq-jR38UfTNbNtRQvc0Q8HWmLak,1054
7
+ relib-0.2.3.dist-info/METADATA,sha256=9Q6IJAOtehmp-dQR6l3ppMl8MA1-XFJqE5LFSn84Xk4,260
8
+ relib-0.2.3.dist-info/WHEEL,sha256=2wepM1nk4DS4eFpYrW1TTqPcoGNfHhhO_i5m4cOimbo,92
9
+ relib-0.2.3.dist-info/top_level.txt,sha256=Yc96FwkbRYj4AQVatga8uK4hH9ATKI9XIyEH_1ba6KQ,6
10
+ relib-0.2.3.dist-info/RECORD,,
@@ -1,5 +1,5 @@
1
1
  Wheel-Version: 1.0
2
- Generator: bdist_wheel (0.37.1)
2
+ Generator: bdist_wheel (0.38.4)
3
3
  Root-Is-Purelib: true
4
4
  Tag: py3-none-any
5
5
 
@@ -1,10 +0,0 @@
1
- relib/__init__.py,sha256=pzWwFP1HnqS-gPaER62RZuDyQuUOlbH0XiYQV87jsqU,269
2
- relib/hashing.py,sha256=6iAPRiJI_4jaSooZRFJnqK2limXqTmErzcwpd050LAA,8943
3
- relib/measure_duration.py,sha256=jJa5Kh5FxaBysS__nkwqcnTt8Uc2niLucXfTzFE0j-E,555
4
- relib/raypipe.py,sha256=ynEoXs1dnD-360_uQC8v89xjiilt3knpocXpFaQ3plA,1905
5
- relib/utils.py,sha256=CiYE2QU1ybmY2pk9AoNOuhcpMkc0y-PAGihFWzFDqyQ,1930
6
- relib-0.2.1.dist-info/LICENSE.txt,sha256=2c7g4mni-RUemFGkk6GnoFwknh-leF04BF_J_3gp4sg,1054
7
- relib-0.2.1.dist-info/METADATA,sha256=jEimdiPgyg6p219YAmhH6e3IpMU8SvNHeaDV_VJKDsE,260
8
- relib-0.2.1.dist-info/WHEEL,sha256=G16H4A3IeoQmnOrYV4ueZGKSjhipXx8zc8nu9FGlvMA,92
9
- relib-0.2.1.dist-info/top_level.txt,sha256=Yc96FwkbRYj4AQVatga8uK4hH9ATKI9XIyEH_1ba6KQ,6
10
- relib-0.2.1.dist-info/RECORD,,