tinystream 0.1.15__tar.gz → 0.1.16__tar.gz

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.
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: tinystream
3
- Version: 0.1.15
3
+ Version: 0.1.16
4
4
  Summary: Yet another python streams library
5
5
  Home-page: https://github.com/mreiche/python-streams
6
6
  Author: Mike Reiche
@@ -137,6 +137,11 @@ stream = Stream(children)
137
137
  items_with_name = Stream([child]).filter_key("name")
138
138
  ```
139
139
 
140
+ ### Filter by key value
141
+ ```python
142
+ items_with_name = Stream([child]).filter_key_value("name", "Child")
143
+ ```
144
+
140
145
  ### Filter by type
141
146
  ```python
142
147
  nodes_only = Stream([child]).filter_type(Node)
@@ -128,6 +128,11 @@ stream = Stream(children)
128
128
  items_with_name = Stream([child]).filter_key("name")
129
129
  ```
130
130
 
131
+ ### Filter by key value
132
+ ```python
133
+ items_with_name = Stream([child]).filter_key_value("name", "Child")
134
+ ```
135
+
131
136
  ### Filter by type
132
137
  ```python
133
138
  nodes_only = Stream([child]).filter_type(Node)
@@ -10,7 +10,7 @@ setup(
10
10
  description="Yet another python streams library",
11
11
  long_description=long_description,
12
12
  long_description_content_type="text/markdown",
13
- version="0.1.15",
13
+ version="0.1.16",
14
14
  url="https://github.com/mreiche/python-streams",
15
15
  author="Mike Reiche",
16
16
  py_modules=['tinystream'],
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: tinystream
3
- Version: 0.1.15
3
+ Version: 0.1.16
4
4
  Summary: Yet another python streams library
5
5
  Home-page: https://github.com/mreiche/python-streams
6
6
  Author: Mike Reiche
@@ -137,6 +137,11 @@ stream = Stream(children)
137
137
  items_with_name = Stream([child]).filter_key("name")
138
138
  ```
139
139
 
140
+ ### Filter by key value
141
+ ```python
142
+ items_with_name = Stream([child]).filter_key_value("name", "Child")
143
+ ```
144
+
140
145
  ### Filter by type
141
146
  ```python
142
147
  nodes_only = Stream([child]).filter_type(Node)
@@ -15,9 +15,10 @@ Comparator = Callable[[T, T], bool]
15
15
  Reducer = Callable[[T, T], R]
16
16
  Predicate = Callable[[T], bool]
17
17
  Supplier = Callable[[], T]
18
+ Key = int|str
18
19
 
19
20
 
20
- def _filter_key(x: any, key: any, invert: bool = False):
21
+ def _key_exists(x: any, key: Key, invert: bool = False):
21
22
  if isinstance(x, (list, tuple)):
22
23
  size = len(x)
23
24
  if invert:
@@ -36,7 +37,7 @@ def _filter_key(x: any, key: any, invert: bool = False):
36
37
  return hasattr(x, key)
37
38
 
38
39
 
39
- def _map_key(x: any, key: any):
40
+ def _get_key_value(x: any, key: Key) -> any:
40
41
  if isinstance(x, (list, dict, tuple)):
41
42
  return x[key]
42
43
  else:
@@ -106,15 +107,21 @@ class Opt(Generic[T]):
106
107
  def filter_type(self, typehint: Type[R]) -> "Opt[R]":
107
108
  return self.filter(lambda x: isinstance(x, typehint))
108
109
 
109
- def filter_key(self, key: str | int, invert: bool = False):
110
- if _filter_key(self.__val, key, invert):
110
+ def filter_key(self, key: Key, invert: bool = False):
111
+ if _key_exists(self.__val, key, invert):
111
112
  return self
112
113
  else:
113
114
  return EmptyOpt()
114
115
 
115
- def map_key(self, key: str | int):
116
- if _filter_key(self.__val, key):
117
- return Opt(_map_key(self.__val, key))
116
+ def filter_key_value(self, key: Key, value: any):
117
+ if _key_exists(self.__val, key, False) and _get_key_value(self.__val, key) == value:
118
+ return self
119
+ else:
120
+ return EmptyOpt()
121
+
122
+ def map_key(self, key: Key):
123
+ if _key_exists(self.__val, key):
124
+ return Opt(_get_key_value(self.__val, key))
118
125
  else:
119
126
  return EmptyOpt()
120
127
 
@@ -148,10 +155,13 @@ class EmptyOpt(Opt[None]):
148
155
  def filter(self, predicate: Predicate[T]):
149
156
  return self
150
157
 
151
- def map_key(self, key: str | int):
158
+ def map_key(self, key: Key):
152
159
  return self
153
160
 
154
- def filter_key(self, key: str | int, invert: bool = False):
161
+ def filter_key(self, key: Key, invert: bool = False):
162
+ return self
163
+
164
+ def filter_key_value(self, key: Key, value: any):
155
165
  return self
156
166
 
157
167
  @property
@@ -224,8 +234,8 @@ class Stream(Iterator[T]):
224
234
  def map_kwargs(self, mapper: Type[R]) -> "Stream[R]":
225
235
  return self.map(lambda x: mapper(**x))
226
236
 
227
- def map_key(self, key: str | int):
228
- return self.filter_key(key).map(lambda x: _map_key(x, key))
237
+ def map_key(self, key: Key):
238
+ return self.filter_key(key).map(lambda x: _get_key_value(x, key))
229
239
 
230
240
  def map_keys(self, *iterables):
231
241
  inst = self
@@ -242,8 +252,11 @@ class Stream(Iterator[T]):
242
252
  def filter(self, predicate: Predicate[T]):
243
253
  return Stream[T](filter(predicate, self.__iterable))
244
254
 
245
- def filter_key(self, key: str | int, invert: bool = False):
246
- return self.filter(lambda x: _filter_key(x, key, invert))
255
+ def filter_key(self, key: Key, invert: bool = False):
256
+ return self.filter(lambda x: _key_exists(x, key, invert))
257
+
258
+ def filter_key_value(self, key: Key, value: any):
259
+ return self.filter(lambda x: _key_exists(x, key, False) and _get_key_value(x, key) == value)
247
260
 
248
261
  def flatmap(self, mapper: FlatMapper[T, R] = None):
249
262
 
File without changes
File without changes