tinystream 0.1.15__tar.gz → 0.1.17__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.
- {tinystream-0.1.15/tinystream.egg-info → tinystream-0.1.17}/PKG-INFO +64 -64
- {tinystream-0.1.15 → tinystream-0.1.17}/README.md +56 -62
- {tinystream-0.1.15 → tinystream-0.1.17}/setup.py +4 -1
- tinystream-0.1.17/tests/test_opt.py +172 -0
- tinystream-0.1.17/tests/test_streams.py +412 -0
- {tinystream-0.1.15 → tinystream-0.1.17/tinystream.egg-info}/PKG-INFO +64 -64
- {tinystream-0.1.15 → tinystream-0.1.17}/tinystream.egg-info/SOURCES.txt +2 -0
- {tinystream-0.1.15 → tinystream-0.1.17}/tinystream.py +35 -14
- {tinystream-0.1.15 → tinystream-0.1.17}/LICENSE +0 -0
- {tinystream-0.1.15 → tinystream-0.1.17}/setup.cfg +0 -0
- {tinystream-0.1.15 → tinystream-0.1.17}/tinystream.egg-info/dependency_links.txt +0 -0
- {tinystream-0.1.15 → tinystream-0.1.17}/tinystream.egg-info/top_level.txt +0 -0
|
@@ -1,11 +1,17 @@
|
|
|
1
|
-
Metadata-Version: 2.
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
2
|
Name: tinystream
|
|
3
|
-
Version: 0.1.
|
|
3
|
+
Version: 0.1.17
|
|
4
4
|
Summary: Yet another python streams library
|
|
5
5
|
Home-page: https://github.com/mreiche/python-streams
|
|
6
6
|
Author: Mike Reiche
|
|
7
7
|
Description-Content-Type: text/markdown
|
|
8
8
|
License-File: LICENSE
|
|
9
|
+
Dynamic: author
|
|
10
|
+
Dynamic: description
|
|
11
|
+
Dynamic: description-content-type
|
|
12
|
+
Dynamic: home-page
|
|
13
|
+
Dynamic: license-file
|
|
14
|
+
Dynamic: summary
|
|
9
15
|
|
|
10
16
|

|
|
11
17
|
[](https://app.codecov.io/github/mreiche/python-streams)
|
|
@@ -39,50 +45,15 @@ Aggregators like `sum()`, `count()`, `max()` will `collect()` the data and end t
|
|
|
39
45
|
|
|
40
46
|
## Built-in Optional support
|
|
41
47
|
|
|
42
|
-
Some aggregators like `sum()`, `max()` are
|
|
48
|
+
Some aggregators like `sum()`, `max()` are `Opt`:
|
|
43
49
|
|
|
44
50
|
```python
|
|
45
|
-
assert Stream((1, 2, 3, 4, 5)).sum().
|
|
51
|
+
assert Stream((1, 2, 3, 4, 5)).sum().get() == 15
|
|
46
52
|
```
|
|
47
53
|
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
```python
|
|
51
|
-
assert Stream((1, 2, 3, 4, 5)).next().present
|
|
52
|
-
```
|
|
53
|
-
|
|
54
|
-
Create custom *optional*:
|
|
55
|
-
|
|
56
|
-
```python
|
|
57
|
-
from tinystream import Opt
|
|
58
|
-
|
|
59
|
-
assert Opt(None).absent
|
|
60
|
-
```
|
|
61
|
-
|
|
62
|
-
Map *optional*:
|
|
63
|
-
```python
|
|
64
|
-
assert Opt("String").map(str.lower).len == 6
|
|
65
|
-
```
|
|
66
|
-
|
|
67
|
-
Get default value:
|
|
68
|
-
```python
|
|
69
|
-
assert Opt(None).get(6) == 6
|
|
70
|
-
assert Opt(None).get(lambda: 6) == 6
|
|
71
|
-
assert Opt(None).if_absent(lambda: 3).present
|
|
72
|
-
```
|
|
73
|
-
|
|
74
|
-
Filter value:
|
|
75
|
-
|
|
76
|
-
```python
|
|
77
|
-
assert Opt(0).filter(lambda x: x > 0).absent
|
|
78
|
-
```
|
|
79
|
-
|
|
80
|
-
You can also access optional index elements of the stream, but this will `collect()` and end the stream.
|
|
81
|
-
```python
|
|
82
|
-
assert Stream([])[2].absent
|
|
83
|
-
```
|
|
54
|
+
## More features
|
|
84
55
|
|
|
85
|
-
|
|
56
|
+
### Type hinting
|
|
86
57
|
|
|
87
58
|
You can typehint datatypes like:
|
|
88
59
|
|
|
@@ -113,8 +84,6 @@ def map_parent(n: Node):
|
|
|
113
84
|
assert stream.map(map_parent).next().get().name == "B"
|
|
114
85
|
```
|
|
115
86
|
|
|
116
|
-
## More features
|
|
117
|
-
|
|
118
87
|
### Typed dictionaries
|
|
119
88
|
|
|
120
89
|
Dictionaries are streamed as `tuple(key, value)`
|
|
@@ -137,6 +106,11 @@ stream = Stream(children)
|
|
|
137
106
|
items_with_name = Stream([child]).filter_key("name")
|
|
138
107
|
```
|
|
139
108
|
|
|
109
|
+
### Filter by key value
|
|
110
|
+
```python
|
|
111
|
+
items_with_name = Stream([child]).filter_key_value("name", "Child")
|
|
112
|
+
```
|
|
113
|
+
|
|
140
114
|
### Filter by type
|
|
141
115
|
```python
|
|
142
116
|
nodes_only = Stream([child]).filter_type(Node)
|
|
@@ -189,9 +163,40 @@ if char == "a":
|
|
|
189
163
|
stream.end()
|
|
190
164
|
```
|
|
191
165
|
|
|
166
|
+
### Opt usage
|
|
167
|
+
|
|
168
|
+
Get next value as `Opt`:
|
|
169
|
+
|
|
170
|
+
```python
|
|
171
|
+
assert Stream((1, 2, 3, 4, 5)).next().present
|
|
172
|
+
```
|
|
173
|
+
|
|
174
|
+
Mapping:
|
|
175
|
+
```python
|
|
176
|
+
assert Opt("String").map(str.lower).len == 6
|
|
177
|
+
```
|
|
178
|
+
|
|
179
|
+
Get default value:
|
|
180
|
+
```python
|
|
181
|
+
assert Opt(None).get(6) == 6
|
|
182
|
+
assert Opt(None).get(lambda: 6) == 6
|
|
183
|
+
assert Opt(None).if_absent(lambda: 3).present
|
|
184
|
+
```
|
|
185
|
+
|
|
186
|
+
Filter value:
|
|
187
|
+
```python
|
|
188
|
+
assert Opt(0).filter(lambda x: x > 0).absent
|
|
189
|
+
```
|
|
190
|
+
|
|
191
|
+
You can also access optional index elements of the stream, but this will `collect()` and end the stream.
|
|
192
|
+
```python
|
|
193
|
+
assert Stream([])[2].absent
|
|
194
|
+
```
|
|
195
|
+
|
|
192
196
|
## Examples
|
|
193
197
|
|
|
194
|
-
|
|
198
|
+
### Write better code with `Stream`
|
|
199
|
+
|
|
195
200
|
```python
|
|
196
201
|
data = {
|
|
197
202
|
"ranges": [
|
|
@@ -199,22 +204,28 @@ data = {
|
|
|
199
204
|
{"weeks": 1},
|
|
200
205
|
]
|
|
201
206
|
}
|
|
202
|
-
```
|
|
203
207
|
|
|
204
|
-
|
|
205
|
-
|
|
208
|
+
# With tinystream Stream
|
|
209
|
+
for range_data in Opt(data).map_key("ranges").stream().map_kwargs(timedelta):
|
|
210
|
+
pass
|
|
211
|
+
|
|
212
|
+
# Vanilly Python
|
|
206
213
|
if "ranges" in data:
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
214
|
+
range_data: timedelta
|
|
215
|
+
for range_data in map(lambda x: timedelta(**x), data["ranges"]):
|
|
216
|
+
pass
|
|
210
217
|
```
|
|
211
218
|
|
|
212
|
-
|
|
219
|
+
### Write better code with `Opt`
|
|
213
220
|
```python
|
|
214
|
-
|
|
215
|
-
|
|
221
|
+
# tinystream Opt
|
|
222
|
+
var = Opt(my_dict).kmap("key").filter(not_empty).get("default")
|
|
223
|
+
|
|
224
|
+
# Vanilla Python
|
|
225
|
+
var = my_dict["key"] if "key" in my_dict and not_empty(my_dict["key"]) else "default"
|
|
216
226
|
```
|
|
217
227
|
|
|
228
|
+
|
|
218
229
|
## Comparison with other libraries
|
|
219
230
|
|
|
220
231
|
There are a couple of other implementation to fulfill similar requirements.
|
|
@@ -235,17 +246,6 @@ There are a couple of other implementation to fulfill similar requirements.
|
|
|
235
246
|
PYTHONPATH="." pytest --cov=tinystream -n 4 tests/
|
|
236
247
|
```
|
|
237
248
|
|
|
238
|
-
### Release update
|
|
239
|
-
1. Update version in `setup.py`
|
|
240
|
-
2. Package library
|
|
241
|
-
```shell
|
|
242
|
-
python setup.py sdist
|
|
243
|
-
```
|
|
244
|
-
3. Publish library
|
|
245
|
-
```shell
|
|
246
|
-
twine upload dist/tinystream-[version].tar.gz
|
|
247
|
-
```
|
|
248
|
-
|
|
249
249
|
## References
|
|
250
250
|
|
|
251
251
|
- https://github.com/MichaelKim0407/tutorial-pip-package
|
|
@@ -30,50 +30,15 @@ Aggregators like `sum()`, `count()`, `max()` will `collect()` the data and end t
|
|
|
30
30
|
|
|
31
31
|
## Built-in Optional support
|
|
32
32
|
|
|
33
|
-
Some aggregators like `sum()`, `max()` are
|
|
33
|
+
Some aggregators like `sum()`, `max()` are `Opt`:
|
|
34
34
|
|
|
35
35
|
```python
|
|
36
|
-
assert Stream((1, 2, 3, 4, 5)).sum().
|
|
36
|
+
assert Stream((1, 2, 3, 4, 5)).sum().get() == 15
|
|
37
37
|
```
|
|
38
38
|
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
```python
|
|
42
|
-
assert Stream((1, 2, 3, 4, 5)).next().present
|
|
43
|
-
```
|
|
44
|
-
|
|
45
|
-
Create custom *optional*:
|
|
46
|
-
|
|
47
|
-
```python
|
|
48
|
-
from tinystream import Opt
|
|
49
|
-
|
|
50
|
-
assert Opt(None).absent
|
|
51
|
-
```
|
|
52
|
-
|
|
53
|
-
Map *optional*:
|
|
54
|
-
```python
|
|
55
|
-
assert Opt("String").map(str.lower).len == 6
|
|
56
|
-
```
|
|
57
|
-
|
|
58
|
-
Get default value:
|
|
59
|
-
```python
|
|
60
|
-
assert Opt(None).get(6) == 6
|
|
61
|
-
assert Opt(None).get(lambda: 6) == 6
|
|
62
|
-
assert Opt(None).if_absent(lambda: 3).present
|
|
63
|
-
```
|
|
64
|
-
|
|
65
|
-
Filter value:
|
|
66
|
-
|
|
67
|
-
```python
|
|
68
|
-
assert Opt(0).filter(lambda x: x > 0).absent
|
|
69
|
-
```
|
|
70
|
-
|
|
71
|
-
You can also access optional index elements of the stream, but this will `collect()` and end the stream.
|
|
72
|
-
```python
|
|
73
|
-
assert Stream([])[2].absent
|
|
74
|
-
```
|
|
39
|
+
## More features
|
|
75
40
|
|
|
76
|
-
|
|
41
|
+
### Type hinting
|
|
77
42
|
|
|
78
43
|
You can typehint datatypes like:
|
|
79
44
|
|
|
@@ -104,8 +69,6 @@ def map_parent(n: Node):
|
|
|
104
69
|
assert stream.map(map_parent).next().get().name == "B"
|
|
105
70
|
```
|
|
106
71
|
|
|
107
|
-
## More features
|
|
108
|
-
|
|
109
72
|
### Typed dictionaries
|
|
110
73
|
|
|
111
74
|
Dictionaries are streamed as `tuple(key, value)`
|
|
@@ -128,6 +91,11 @@ stream = Stream(children)
|
|
|
128
91
|
items_with_name = Stream([child]).filter_key("name")
|
|
129
92
|
```
|
|
130
93
|
|
|
94
|
+
### Filter by key value
|
|
95
|
+
```python
|
|
96
|
+
items_with_name = Stream([child]).filter_key_value("name", "Child")
|
|
97
|
+
```
|
|
98
|
+
|
|
131
99
|
### Filter by type
|
|
132
100
|
```python
|
|
133
101
|
nodes_only = Stream([child]).filter_type(Node)
|
|
@@ -180,9 +148,40 @@ if char == "a":
|
|
|
180
148
|
stream.end()
|
|
181
149
|
```
|
|
182
150
|
|
|
151
|
+
### Opt usage
|
|
152
|
+
|
|
153
|
+
Get next value as `Opt`:
|
|
154
|
+
|
|
155
|
+
```python
|
|
156
|
+
assert Stream((1, 2, 3, 4, 5)).next().present
|
|
157
|
+
```
|
|
158
|
+
|
|
159
|
+
Mapping:
|
|
160
|
+
```python
|
|
161
|
+
assert Opt("String").map(str.lower).len == 6
|
|
162
|
+
```
|
|
163
|
+
|
|
164
|
+
Get default value:
|
|
165
|
+
```python
|
|
166
|
+
assert Opt(None).get(6) == 6
|
|
167
|
+
assert Opt(None).get(lambda: 6) == 6
|
|
168
|
+
assert Opt(None).if_absent(lambda: 3).present
|
|
169
|
+
```
|
|
170
|
+
|
|
171
|
+
Filter value:
|
|
172
|
+
```python
|
|
173
|
+
assert Opt(0).filter(lambda x: x > 0).absent
|
|
174
|
+
```
|
|
175
|
+
|
|
176
|
+
You can also access optional index elements of the stream, but this will `collect()` and end the stream.
|
|
177
|
+
```python
|
|
178
|
+
assert Stream([])[2].absent
|
|
179
|
+
```
|
|
180
|
+
|
|
183
181
|
## Examples
|
|
184
182
|
|
|
185
|
-
|
|
183
|
+
### Write better code with `Stream`
|
|
184
|
+
|
|
186
185
|
```python
|
|
187
186
|
data = {
|
|
188
187
|
"ranges": [
|
|
@@ -190,22 +189,28 @@ data = {
|
|
|
190
189
|
{"weeks": 1},
|
|
191
190
|
]
|
|
192
191
|
}
|
|
193
|
-
```
|
|
194
192
|
|
|
195
|
-
|
|
196
|
-
|
|
193
|
+
# With tinystream Stream
|
|
194
|
+
for range_data in Opt(data).map_key("ranges").stream().map_kwargs(timedelta):
|
|
195
|
+
pass
|
|
196
|
+
|
|
197
|
+
# Vanilly Python
|
|
197
198
|
if "ranges" in data:
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
199
|
+
range_data: timedelta
|
|
200
|
+
for range_data in map(lambda x: timedelta(**x), data["ranges"]):
|
|
201
|
+
pass
|
|
201
202
|
```
|
|
202
203
|
|
|
203
|
-
|
|
204
|
+
### Write better code with `Opt`
|
|
204
205
|
```python
|
|
205
|
-
|
|
206
|
-
|
|
206
|
+
# tinystream Opt
|
|
207
|
+
var = Opt(my_dict).kmap("key").filter(not_empty).get("default")
|
|
208
|
+
|
|
209
|
+
# Vanilla Python
|
|
210
|
+
var = my_dict["key"] if "key" in my_dict and not_empty(my_dict["key"]) else "default"
|
|
207
211
|
```
|
|
208
212
|
|
|
213
|
+
|
|
209
214
|
## Comparison with other libraries
|
|
210
215
|
|
|
211
216
|
There are a couple of other implementation to fulfill similar requirements.
|
|
@@ -226,17 +231,6 @@ There are a couple of other implementation to fulfill similar requirements.
|
|
|
226
231
|
PYTHONPATH="." pytest --cov=tinystream -n 4 tests/
|
|
227
232
|
```
|
|
228
233
|
|
|
229
|
-
### Release update
|
|
230
|
-
1. Update version in `setup.py`
|
|
231
|
-
2. Package library
|
|
232
|
-
```shell
|
|
233
|
-
python setup.py sdist
|
|
234
|
-
```
|
|
235
|
-
3. Publish library
|
|
236
|
-
```shell
|
|
237
|
-
twine upload dist/tinystream-[version].tar.gz
|
|
238
|
-
```
|
|
239
|
-
|
|
240
234
|
## References
|
|
241
235
|
|
|
242
236
|
- https://github.com/MichaelKim0407/tutorial-pip-package
|
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import os
|
|
1
2
|
from pathlib import Path
|
|
2
3
|
|
|
3
4
|
from setuptools import setup
|
|
@@ -5,12 +6,14 @@ from setuptools import setup
|
|
|
5
6
|
this_directory = Path(__file__).parent
|
|
6
7
|
long_description = (this_directory / "README.md").read_text()
|
|
7
8
|
|
|
9
|
+
VERSION = os.environ.get("PACKAGE_VERSION", "0.0.1")
|
|
10
|
+
|
|
8
11
|
setup(
|
|
9
12
|
name="tinystream",
|
|
10
13
|
description="Yet another python streams library",
|
|
11
14
|
long_description=long_description,
|
|
12
15
|
long_description_content_type="text/markdown",
|
|
13
|
-
version=
|
|
16
|
+
version=VERSION,
|
|
14
17
|
url="https://github.com/mreiche/python-streams",
|
|
15
18
|
author="Mike Reiche",
|
|
16
19
|
py_modules=['tinystream'],
|
|
@@ -0,0 +1,172 @@
|
|
|
1
|
+
import pytest
|
|
2
|
+
|
|
3
|
+
from test_streams import Node
|
|
4
|
+
from tinystream import Opt
|
|
5
|
+
|
|
6
|
+
|
|
7
|
+
def test_opt_absent():
|
|
8
|
+
assert Opt(None).empty
|
|
9
|
+
assert Opt(None).absent
|
|
10
|
+
|
|
11
|
+
|
|
12
|
+
def test_opt_value():
|
|
13
|
+
assert Opt("Hallo").get() == "Hallo"
|
|
14
|
+
|
|
15
|
+
|
|
16
|
+
def test_opt_empty_default():
|
|
17
|
+
assert Opt(None).get("Welt") == "Welt"
|
|
18
|
+
|
|
19
|
+
|
|
20
|
+
def test_opt_empty_default_callable():
|
|
21
|
+
assert Opt(None).get(lambda: "Welt") == "Welt"
|
|
22
|
+
|
|
23
|
+
|
|
24
|
+
def test_opt_empty_raise():
|
|
25
|
+
with pytest.raises(Exception) as e:
|
|
26
|
+
Opt(None).get()
|
|
27
|
+
|
|
28
|
+
assert "Cannot get value of empty Opt without default value" in e.value.args[0]
|
|
29
|
+
|
|
30
|
+
|
|
31
|
+
def test_opt_value_if_present():
|
|
32
|
+
if_present = None
|
|
33
|
+
|
|
34
|
+
def _present(given):
|
|
35
|
+
nonlocal if_present
|
|
36
|
+
if_present = given
|
|
37
|
+
|
|
38
|
+
Opt("Hallo").if_present(_present)
|
|
39
|
+
assert if_present == "Hallo"
|
|
40
|
+
|
|
41
|
+
|
|
42
|
+
def test_opt_empty_if_present():
|
|
43
|
+
if_present = None
|
|
44
|
+
|
|
45
|
+
def _present(given):
|
|
46
|
+
nonlocal if_present
|
|
47
|
+
if_present = given
|
|
48
|
+
|
|
49
|
+
Opt(None).if_present(_present)
|
|
50
|
+
assert if_present is None
|
|
51
|
+
|
|
52
|
+
|
|
53
|
+
def test_opt_value_if_absent_callable():
|
|
54
|
+
assert Opt("Hallo").if_absent(lambda: "Empty").get() == "Hallo"
|
|
55
|
+
|
|
56
|
+
|
|
57
|
+
def test_opt_empty_if_empty_callable():
|
|
58
|
+
assert Opt(None).if_absent(lambda: "Empty").get() == "Empty"
|
|
59
|
+
|
|
60
|
+
|
|
61
|
+
def test_opt_empty_if_empty_value():
|
|
62
|
+
assert Opt(None).if_absent("Empty").get() == "Empty"
|
|
63
|
+
assert Opt(None).if_empty("Empty").get() == "Empty"
|
|
64
|
+
|
|
65
|
+
|
|
66
|
+
def test_opt_value_map():
|
|
67
|
+
assert Opt("Hallo").map(len).get() == 5
|
|
68
|
+
|
|
69
|
+
|
|
70
|
+
def test_opt_value_map_type():
|
|
71
|
+
|
|
72
|
+
class SimpleType:
|
|
73
|
+
def __init__(self, value: int):
|
|
74
|
+
self.value = value
|
|
75
|
+
|
|
76
|
+
assert Opt(5).map(SimpleType).get().value == 5
|
|
77
|
+
|
|
78
|
+
|
|
79
|
+
def test_opt_value_filter_is_empty():
|
|
80
|
+
assert Opt(0).filter(lambda x: x > 0).absent
|
|
81
|
+
|
|
82
|
+
|
|
83
|
+
def test_opt_value_filter_not_empty():
|
|
84
|
+
assert Opt(1).filter(lambda x: x > 0).present
|
|
85
|
+
|
|
86
|
+
|
|
87
|
+
def test_none_opt_filter():
|
|
88
|
+
assert Opt(None).filter(lambda x: x is not None).absent
|
|
89
|
+
|
|
90
|
+
|
|
91
|
+
def test_none_opt_map():
|
|
92
|
+
none: str = None
|
|
93
|
+
opt = Opt(none).map(len)
|
|
94
|
+
assert opt.absent
|
|
95
|
+
assert opt.map(str.lower).absent
|
|
96
|
+
|
|
97
|
+
|
|
98
|
+
def test_opt_value_len():
|
|
99
|
+
assert Opt("len").len == 3
|
|
100
|
+
|
|
101
|
+
|
|
102
|
+
def test_none_opt_len():
|
|
103
|
+
opt = Opt(None)
|
|
104
|
+
assert opt.len == 0
|
|
105
|
+
assert opt.map(str.lower).len == 0
|
|
106
|
+
|
|
107
|
+
|
|
108
|
+
def test_none_opt_map_key():
|
|
109
|
+
assert Opt(None).map_key("inexistent").absent
|
|
110
|
+
|
|
111
|
+
|
|
112
|
+
def test_filter_type():
|
|
113
|
+
assert Opt("string").filter_type(int).absent
|
|
114
|
+
|
|
115
|
+
|
|
116
|
+
def test_type():
|
|
117
|
+
assert Opt("0").type(int).get() == "0"
|
|
118
|
+
|
|
119
|
+
|
|
120
|
+
def test_filter_key_exists():
|
|
121
|
+
assert Opt({"name": "Hallo"}).filter_key("name").present
|
|
122
|
+
|
|
123
|
+
|
|
124
|
+
def test_filter_key_inexistent():
|
|
125
|
+
assert Opt({"name": "Hallo"}).filter_key("inexistent").absent
|
|
126
|
+
|
|
127
|
+
|
|
128
|
+
def test_filter_key_value():
|
|
129
|
+
opt = Opt({"name": "Hallo"})
|
|
130
|
+
assert opt.filter_key_value("name", "Hallo").present
|
|
131
|
+
assert opt.filter_key_value("name", "Katze").absent
|
|
132
|
+
|
|
133
|
+
|
|
134
|
+
def test_map_key_value():
|
|
135
|
+
assert Opt({"name": "Hallo"}).map_key("name").get() == "Hallo"
|
|
136
|
+
|
|
137
|
+
|
|
138
|
+
def test_map_key_empty():
|
|
139
|
+
assert Opt({"name": "Hallo"}).map_key("inexistent").absent
|
|
140
|
+
|
|
141
|
+
|
|
142
|
+
def test_map_keys():
|
|
143
|
+
data = {
|
|
144
|
+
"address": {
|
|
145
|
+
"street": {
|
|
146
|
+
"name": "Grove"
|
|
147
|
+
}
|
|
148
|
+
}
|
|
149
|
+
}
|
|
150
|
+
opt = Opt(data)
|
|
151
|
+
assert opt.map_keys("address", "street", "name").get() == "Grove"
|
|
152
|
+
assert opt.map_keys("address", "street", "number").absent
|
|
153
|
+
assert opt.map_keys("person", "name", "first").absent
|
|
154
|
+
|
|
155
|
+
empty_opt = opt.map_keys("inexistent")
|
|
156
|
+
assert empty_opt.filter_key("inexistent") == empty_opt
|
|
157
|
+
assert empty_opt.filter(lambda x: x is not None) == empty_opt
|
|
158
|
+
|
|
159
|
+
|
|
160
|
+
def test_stream():
|
|
161
|
+
data = {
|
|
162
|
+
"list": [1, 2, 3]
|
|
163
|
+
}
|
|
164
|
+
|
|
165
|
+
opt = Opt(data)
|
|
166
|
+
assert opt.map_key("list").stream().sum().get() == 6
|
|
167
|
+
assert opt.map_key("inexistent").stream().sum().absent
|
|
168
|
+
|
|
169
|
+
|
|
170
|
+
def test_dict_map_kwargs():
|
|
171
|
+
opt = Opt({"name": "First"})
|
|
172
|
+
assert opt.map_kwargs(Node).get().name == "First"
|
|
@@ -0,0 +1,412 @@
|
|
|
1
|
+
from dataclasses import dataclass
|
|
2
|
+
from typing import List, Iterable
|
|
3
|
+
|
|
4
|
+
import pytest
|
|
5
|
+
|
|
6
|
+
from tinystream import Stream
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
def fibonacci(*kwargs):
|
|
10
|
+
a = 1
|
|
11
|
+
b = 1
|
|
12
|
+
while True:
|
|
13
|
+
yield b
|
|
14
|
+
a, b = b, a + b
|
|
15
|
+
|
|
16
|
+
|
|
17
|
+
@dataclass
|
|
18
|
+
class Node:
|
|
19
|
+
name: str
|
|
20
|
+
parent: "Node" = None
|
|
21
|
+
children: List["Node"] = None
|
|
22
|
+
|
|
23
|
+
|
|
24
|
+
def map_name(entity: Node):
|
|
25
|
+
return entity.name
|
|
26
|
+
|
|
27
|
+
|
|
28
|
+
def flat_map_children(parent: Node):
|
|
29
|
+
return parent.children
|
|
30
|
+
|
|
31
|
+
|
|
32
|
+
def compare_name(x: Node, y: Node):
|
|
33
|
+
if x.name < y.name:
|
|
34
|
+
return -1
|
|
35
|
+
elif x.name > y.name:
|
|
36
|
+
return 1
|
|
37
|
+
else:
|
|
38
|
+
return 0
|
|
39
|
+
|
|
40
|
+
|
|
41
|
+
def create_parents():
|
|
42
|
+
parent_a = Node(name="Parent A")
|
|
43
|
+
parent_b = Node(name="Parent B")
|
|
44
|
+
child_a = Node(name="Child A")
|
|
45
|
+
child_b1 = Node(name="Child B")
|
|
46
|
+
child_b2 = Node(name="Child B2")
|
|
47
|
+
|
|
48
|
+
parent_a.children = [child_a]
|
|
49
|
+
child_a.parent = parent_a
|
|
50
|
+
|
|
51
|
+
parent_b.children = [child_b1, child_b2]
|
|
52
|
+
child_b1.parent = parent_b
|
|
53
|
+
child_b2.parent = parent_b
|
|
54
|
+
return parent_a, parent_b
|
|
55
|
+
|
|
56
|
+
|
|
57
|
+
def create_list():
|
|
58
|
+
parent_a, parent_b = create_parents()
|
|
59
|
+
data: List[Node] = [
|
|
60
|
+
parent_a,
|
|
61
|
+
parent_b
|
|
62
|
+
]
|
|
63
|
+
return data
|
|
64
|
+
|
|
65
|
+
|
|
66
|
+
def create_numeric_list():
|
|
67
|
+
return [1, 3, 5, 6, 2]
|
|
68
|
+
|
|
69
|
+
|
|
70
|
+
def create_string_list():
|
|
71
|
+
return ["X", "Y", "A"]
|
|
72
|
+
|
|
73
|
+
|
|
74
|
+
def create_mixed_list():
|
|
75
|
+
return ["X", Node("Node A"), 1]
|
|
76
|
+
|
|
77
|
+
|
|
78
|
+
def create_dict():
|
|
79
|
+
parent_a, parent_b = create_parents()
|
|
80
|
+
return {
|
|
81
|
+
"parent_a": parent_a,
|
|
82
|
+
"parent_b": parent_b
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
|
|
86
|
+
def create_node_dict_list():
|
|
87
|
+
return [
|
|
88
|
+
{"node": Node(name="Node A")},
|
|
89
|
+
{"node": Node(name="Node B")},
|
|
90
|
+
{"node": Node(name="Node C")},
|
|
91
|
+
{"node": Node(name="Node D")},
|
|
92
|
+
]
|
|
93
|
+
|
|
94
|
+
|
|
95
|
+
def create_dict_list():
|
|
96
|
+
parent_a, parent_b = create_parents()
|
|
97
|
+
return [parent_a.__dict__, parent_b.__dict__]
|
|
98
|
+
|
|
99
|
+
|
|
100
|
+
def test_dict_is_iterable():
|
|
101
|
+
assert isinstance({}, Iterable)
|
|
102
|
+
|
|
103
|
+
|
|
104
|
+
def test_list_one():
|
|
105
|
+
stream = Stream(create_list())
|
|
106
|
+
assert next(stream).name == "Parent A"
|
|
107
|
+
|
|
108
|
+
|
|
109
|
+
def test_optional_index():
|
|
110
|
+
list = Stream(create_string_list())
|
|
111
|
+
assert list[0].present
|
|
112
|
+
assert list[4].absent
|
|
113
|
+
assert list[-1].absent
|
|
114
|
+
assert list[0].map(str.upper).filter(lambda x: x == 'X').present
|
|
115
|
+
|
|
116
|
+
|
|
117
|
+
def test_list_map():
|
|
118
|
+
data = create_list()
|
|
119
|
+
stream = Stream(data)
|
|
120
|
+
for name in stream.map(map_name):
|
|
121
|
+
assert name.startswith("Parent")
|
|
122
|
+
|
|
123
|
+
|
|
124
|
+
def test_dict_map_kwargs():
|
|
125
|
+
data = [{"name": "First"}, {"name": "Second"}]
|
|
126
|
+
stream = Stream(data)
|
|
127
|
+
assert stream.map_kwargs(Node).next().get().name == "First"
|
|
128
|
+
|
|
129
|
+
|
|
130
|
+
def test_list_map_type():
|
|
131
|
+
parent_a, parent_b = create_parents()
|
|
132
|
+
stream = Stream(parent_b.children)
|
|
133
|
+
assert stream.map(lambda x: x.parent).next().get().name == "Parent B"
|
|
134
|
+
|
|
135
|
+
|
|
136
|
+
def test_string_one():
|
|
137
|
+
stream = Stream("Hallo Welt")
|
|
138
|
+
assert stream.next().get() == "H"
|
|
139
|
+
|
|
140
|
+
|
|
141
|
+
def test_string_one_ends():
|
|
142
|
+
stream = Stream("H")
|
|
143
|
+
assert stream.next().get() == "H"
|
|
144
|
+
assert stream.next().empty is True
|
|
145
|
+
|
|
146
|
+
|
|
147
|
+
def test_list_flatmap_count():
|
|
148
|
+
stream = Stream(create_list())
|
|
149
|
+
assert stream.flatmap(lambda x: x.children).count() == 3
|
|
150
|
+
|
|
151
|
+
|
|
152
|
+
def test_list_filter_count():
|
|
153
|
+
stream = Stream(create_list())
|
|
154
|
+
assert stream.filter(lambda x: x.name.endswith("A")).count() == 1
|
|
155
|
+
|
|
156
|
+
|
|
157
|
+
def test_list_sort():
|
|
158
|
+
stream = Stream(create_list())
|
|
159
|
+
assert stream.sort(compare_name, reverse=True).next().get().name.endswith("B")
|
|
160
|
+
|
|
161
|
+
|
|
162
|
+
def test_list_reverse():
|
|
163
|
+
stream = Stream(create_list())
|
|
164
|
+
assert stream.reverse().next().get().name.endswith("B")
|
|
165
|
+
|
|
166
|
+
|
|
167
|
+
def test_list_peek():
|
|
168
|
+
stream = Stream(create_list())
|
|
169
|
+
|
|
170
|
+
def extend_name(x: Node):
|
|
171
|
+
x.name += " Extended"
|
|
172
|
+
|
|
173
|
+
assert stream.peek(extend_name).next().get().name.endswith("Extended")
|
|
174
|
+
|
|
175
|
+
|
|
176
|
+
def test_stream_of_dict():
|
|
177
|
+
stream = Stream.of_dict(create_dict())
|
|
178
|
+
|
|
179
|
+
for item in stream:
|
|
180
|
+
assert isinstance(item[0], str)
|
|
181
|
+
assert isinstance(item[1], Node)
|
|
182
|
+
|
|
183
|
+
|
|
184
|
+
def test_stream_dict():
|
|
185
|
+
stream = Stream(create_dict())
|
|
186
|
+
for item in stream:
|
|
187
|
+
assert isinstance(item[0], str)
|
|
188
|
+
assert isinstance(item[1], Node)
|
|
189
|
+
|
|
190
|
+
|
|
191
|
+
def assert_fibonacci(stream: Stream):
|
|
192
|
+
assert next(stream) == 1
|
|
193
|
+
assert next(stream) == 2
|
|
194
|
+
assert next(stream) == 3
|
|
195
|
+
assert next(stream) == 5
|
|
196
|
+
assert next(stream) == 8
|
|
197
|
+
assert next(stream) == 13
|
|
198
|
+
|
|
199
|
+
assert stream.map(lambda x: x - 10).next().get() == 11
|
|
200
|
+
|
|
201
|
+
|
|
202
|
+
def test_generator_one():
|
|
203
|
+
stream = Stream(fibonacci())
|
|
204
|
+
assert_fibonacci(stream)
|
|
205
|
+
|
|
206
|
+
|
|
207
|
+
def test_numeric_list_min():
|
|
208
|
+
stream = Stream(create_numeric_list())
|
|
209
|
+
assert stream.min().get() == 1
|
|
210
|
+
|
|
211
|
+
|
|
212
|
+
def test_numeric_list_max():
|
|
213
|
+
stream = Stream(create_numeric_list())
|
|
214
|
+
assert stream.max().get() == 6
|
|
215
|
+
|
|
216
|
+
|
|
217
|
+
def test_numeric_list_sum():
|
|
218
|
+
stream = Stream(create_numeric_list())
|
|
219
|
+
assert stream.sum().get() == 17
|
|
220
|
+
|
|
221
|
+
|
|
222
|
+
def test_numeric_list_limit():
|
|
223
|
+
stream = Stream(create_numeric_list())
|
|
224
|
+
assert stream.limit(2).sum().get() == 4
|
|
225
|
+
|
|
226
|
+
|
|
227
|
+
def test_string_list_min():
|
|
228
|
+
stream = Stream(create_string_list())
|
|
229
|
+
assert stream.min().get() == "A"
|
|
230
|
+
|
|
231
|
+
|
|
232
|
+
def test_string_list_max():
|
|
233
|
+
stream = Stream(create_string_list())
|
|
234
|
+
assert stream.max().get() == "Y"
|
|
235
|
+
|
|
236
|
+
|
|
237
|
+
def test_string_list_sum():
|
|
238
|
+
stream = Stream(create_string_list())
|
|
239
|
+
assert stream.map(str.lower).sum().get() == "xya"
|
|
240
|
+
|
|
241
|
+
|
|
242
|
+
def test_numeric_list_concat():
|
|
243
|
+
stream = Stream(create_numeric_list())
|
|
244
|
+
assert stream.concat(create_numeric_list()).sum().get() == 34
|
|
245
|
+
|
|
246
|
+
|
|
247
|
+
def test_of_many_numeric_list():
|
|
248
|
+
stream = Stream.of_many(
|
|
249
|
+
create_numeric_list(),
|
|
250
|
+
create_numeric_list(),
|
|
251
|
+
create_numeric_list()
|
|
252
|
+
)
|
|
253
|
+
assert stream.sum().get() == 51
|
|
254
|
+
|
|
255
|
+
|
|
256
|
+
def test_absent():
|
|
257
|
+
empty = ()
|
|
258
|
+
assert Stream(empty).sum().absent
|
|
259
|
+
assert Stream(empty).max().absent
|
|
260
|
+
assert Stream(empty).min().absent
|
|
261
|
+
|
|
262
|
+
|
|
263
|
+
def test_doc():
|
|
264
|
+
stream = Stream([1, 2, 3, 4, 5])
|
|
265
|
+
|
|
266
|
+
sum = stream \
|
|
267
|
+
.map(lambda x: x + 1) \
|
|
268
|
+
.filter(lambda x: x > 2) \
|
|
269
|
+
.sorted(reverse=True) \
|
|
270
|
+
.reverse() \
|
|
271
|
+
.limit(2) \
|
|
272
|
+
.concat([4]) \
|
|
273
|
+
.sum()
|
|
274
|
+
|
|
275
|
+
assert sum.get() == 11
|
|
276
|
+
|
|
277
|
+
|
|
278
|
+
def test_sorted_key():
|
|
279
|
+
stream = Stream((3, 5, 1))
|
|
280
|
+
assert stream.sorted(int).next().get() == 1
|
|
281
|
+
assert stream.sorted(int, True).next().get() == 5
|
|
282
|
+
|
|
283
|
+
|
|
284
|
+
def test_sorted_no_key():
|
|
285
|
+
stream = Stream((3, 5, 1))
|
|
286
|
+
assert stream.sorted().next().get() == 1
|
|
287
|
+
assert stream.sorted(reverse=True).next().get() == 5
|
|
288
|
+
|
|
289
|
+
|
|
290
|
+
def test_flatmap_list_of_list():
|
|
291
|
+
stream = Stream([create_numeric_list(), create_numeric_list(), create_numeric_list()])
|
|
292
|
+
flat = stream.flatmap()
|
|
293
|
+
assert flat.count() == 15
|
|
294
|
+
|
|
295
|
+
|
|
296
|
+
def test_flatmap_generator():
|
|
297
|
+
stream = Stream(create_numeric_list())\
|
|
298
|
+
.flatmap(fibonacci)
|
|
299
|
+
|
|
300
|
+
assert_fibonacci(stream)
|
|
301
|
+
|
|
302
|
+
|
|
303
|
+
def test_collect_joining():
|
|
304
|
+
stream = Stream(create_numeric_list())
|
|
305
|
+
assert stream.join(" -> ") == "1 -> 3 -> 5 -> 6 -> 2"
|
|
306
|
+
|
|
307
|
+
|
|
308
|
+
def test_object_list_filter_key():
|
|
309
|
+
stream = Stream(create_list())
|
|
310
|
+
assert stream.filter_key("name").count() == 2
|
|
311
|
+
|
|
312
|
+
|
|
313
|
+
def test_object_list_filter_key_invert():
|
|
314
|
+
stream = Stream(create_list())
|
|
315
|
+
assert stream.filter_key("name", invert=True).count() == 0
|
|
316
|
+
|
|
317
|
+
|
|
318
|
+
def test_object_list_map_key():
|
|
319
|
+
stream = Stream(create_list())
|
|
320
|
+
assert stream.map_key("name").join(":") == "Parent A:Parent B"
|
|
321
|
+
|
|
322
|
+
|
|
323
|
+
def test_object_dict_map_key():
|
|
324
|
+
stream = Stream(create_node_dict_list())
|
|
325
|
+
assert stream.map_keys("node", "name").filter(lambda name: name.endswith("C")).next().get() == "Node C"
|
|
326
|
+
assert stream.map_keys("node", "inexistent").next().absent
|
|
327
|
+
|
|
328
|
+
|
|
329
|
+
def test_find():
|
|
330
|
+
stream = Stream(create_string_list())
|
|
331
|
+
find = stream.find(lambda s: s == "Y")
|
|
332
|
+
assert find.present
|
|
333
|
+
assert find.get() == "Y"
|
|
334
|
+
|
|
335
|
+
|
|
336
|
+
def test_dict_list_filter_key():
|
|
337
|
+
stream = Stream(create_dict_list())
|
|
338
|
+
assert stream.filter_key("name").count() == 2
|
|
339
|
+
|
|
340
|
+
|
|
341
|
+
def test_dict_list_filter_key_value():
|
|
342
|
+
stream = Stream(create_dict_list())
|
|
343
|
+
assert stream.filter_key_value("name", "Parent B").count() == 1
|
|
344
|
+
assert stream.filter_key_value("name", "Maus").count() == 0
|
|
345
|
+
|
|
346
|
+
|
|
347
|
+
def test_dict_list_filter_key_invert():
|
|
348
|
+
stream = Stream(create_dict_list())
|
|
349
|
+
assert stream.filter_key("name", invert=True).count() == 0
|
|
350
|
+
|
|
351
|
+
|
|
352
|
+
def test_dict_list_map_key():
|
|
353
|
+
stream = Stream(create_dict_list())
|
|
354
|
+
assert stream.map_key("name").join(":") == "Parent A:Parent B"
|
|
355
|
+
|
|
356
|
+
|
|
357
|
+
def test_dict_map_key():
|
|
358
|
+
stream = Stream.of_dict(create_dict())
|
|
359
|
+
assert stream.map_key(0).next().get() == "parent_a"
|
|
360
|
+
|
|
361
|
+
|
|
362
|
+
def test_dict_filter_key_invert():
|
|
363
|
+
stream = Stream.of_dict(create_dict())
|
|
364
|
+
assert stream.filter_key(0, invert=True).next().empty is True
|
|
365
|
+
|
|
366
|
+
|
|
367
|
+
def test_dict_map_value():
|
|
368
|
+
stream = Stream.of_dict(create_dict())
|
|
369
|
+
assert stream.map_key(1).type(Node).next().get().name == "Parent A"
|
|
370
|
+
|
|
371
|
+
|
|
372
|
+
def test_mixed_list_filter_type():
|
|
373
|
+
stream = Stream(create_mixed_list())
|
|
374
|
+
assert stream.filter_type(Node).count() == 1
|
|
375
|
+
|
|
376
|
+
|
|
377
|
+
def test_on_end():
|
|
378
|
+
closed = False
|
|
379
|
+
|
|
380
|
+
def close():
|
|
381
|
+
nonlocal closed
|
|
382
|
+
closed = True
|
|
383
|
+
|
|
384
|
+
stream = Stream(["a", "b", "c"]).on_end(close)
|
|
385
|
+
for _ in stream:
|
|
386
|
+
assert closed is False
|
|
387
|
+
|
|
388
|
+
assert closed is True
|
|
389
|
+
|
|
390
|
+
|
|
391
|
+
def test_call_end():
|
|
392
|
+
closed = False
|
|
393
|
+
|
|
394
|
+
def close():
|
|
395
|
+
nonlocal closed
|
|
396
|
+
closed = True
|
|
397
|
+
|
|
398
|
+
called = 0
|
|
399
|
+
|
|
400
|
+
stream = Stream(["a", "b", "c"]).on_end(close)
|
|
401
|
+
for _ in stream:
|
|
402
|
+
called += 1
|
|
403
|
+
assert closed is False
|
|
404
|
+
stream.end()
|
|
405
|
+
|
|
406
|
+
assert closed is True
|
|
407
|
+
assert called == 1
|
|
408
|
+
|
|
409
|
+
|
|
410
|
+
def test_on_end_twice():
|
|
411
|
+
with pytest.raises(AttributeError, match="on_end is immutable") as e:
|
|
412
|
+
Stream(["a", "b", "c"]).on_end(lambda: None).on_end(lambda: None)
|
|
@@ -1,11 +1,17 @@
|
|
|
1
|
-
Metadata-Version: 2.
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
2
|
Name: tinystream
|
|
3
|
-
Version: 0.1.
|
|
3
|
+
Version: 0.1.17
|
|
4
4
|
Summary: Yet another python streams library
|
|
5
5
|
Home-page: https://github.com/mreiche/python-streams
|
|
6
6
|
Author: Mike Reiche
|
|
7
7
|
Description-Content-Type: text/markdown
|
|
8
8
|
License-File: LICENSE
|
|
9
|
+
Dynamic: author
|
|
10
|
+
Dynamic: description
|
|
11
|
+
Dynamic: description-content-type
|
|
12
|
+
Dynamic: home-page
|
|
13
|
+
Dynamic: license-file
|
|
14
|
+
Dynamic: summary
|
|
9
15
|
|
|
10
16
|

|
|
11
17
|
[](https://app.codecov.io/github/mreiche/python-streams)
|
|
@@ -39,50 +45,15 @@ Aggregators like `sum()`, `count()`, `max()` will `collect()` the data and end t
|
|
|
39
45
|
|
|
40
46
|
## Built-in Optional support
|
|
41
47
|
|
|
42
|
-
Some aggregators like `sum()`, `max()` are
|
|
48
|
+
Some aggregators like `sum()`, `max()` are `Opt`:
|
|
43
49
|
|
|
44
50
|
```python
|
|
45
|
-
assert Stream((1, 2, 3, 4, 5)).sum().
|
|
51
|
+
assert Stream((1, 2, 3, 4, 5)).sum().get() == 15
|
|
46
52
|
```
|
|
47
53
|
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
```python
|
|
51
|
-
assert Stream((1, 2, 3, 4, 5)).next().present
|
|
52
|
-
```
|
|
53
|
-
|
|
54
|
-
Create custom *optional*:
|
|
55
|
-
|
|
56
|
-
```python
|
|
57
|
-
from tinystream import Opt
|
|
58
|
-
|
|
59
|
-
assert Opt(None).absent
|
|
60
|
-
```
|
|
61
|
-
|
|
62
|
-
Map *optional*:
|
|
63
|
-
```python
|
|
64
|
-
assert Opt("String").map(str.lower).len == 6
|
|
65
|
-
```
|
|
66
|
-
|
|
67
|
-
Get default value:
|
|
68
|
-
```python
|
|
69
|
-
assert Opt(None).get(6) == 6
|
|
70
|
-
assert Opt(None).get(lambda: 6) == 6
|
|
71
|
-
assert Opt(None).if_absent(lambda: 3).present
|
|
72
|
-
```
|
|
73
|
-
|
|
74
|
-
Filter value:
|
|
75
|
-
|
|
76
|
-
```python
|
|
77
|
-
assert Opt(0).filter(lambda x: x > 0).absent
|
|
78
|
-
```
|
|
79
|
-
|
|
80
|
-
You can also access optional index elements of the stream, but this will `collect()` and end the stream.
|
|
81
|
-
```python
|
|
82
|
-
assert Stream([])[2].absent
|
|
83
|
-
```
|
|
54
|
+
## More features
|
|
84
55
|
|
|
85
|
-
|
|
56
|
+
### Type hinting
|
|
86
57
|
|
|
87
58
|
You can typehint datatypes like:
|
|
88
59
|
|
|
@@ -113,8 +84,6 @@ def map_parent(n: Node):
|
|
|
113
84
|
assert stream.map(map_parent).next().get().name == "B"
|
|
114
85
|
```
|
|
115
86
|
|
|
116
|
-
## More features
|
|
117
|
-
|
|
118
87
|
### Typed dictionaries
|
|
119
88
|
|
|
120
89
|
Dictionaries are streamed as `tuple(key, value)`
|
|
@@ -137,6 +106,11 @@ stream = Stream(children)
|
|
|
137
106
|
items_with_name = Stream([child]).filter_key("name")
|
|
138
107
|
```
|
|
139
108
|
|
|
109
|
+
### Filter by key value
|
|
110
|
+
```python
|
|
111
|
+
items_with_name = Stream([child]).filter_key_value("name", "Child")
|
|
112
|
+
```
|
|
113
|
+
|
|
140
114
|
### Filter by type
|
|
141
115
|
```python
|
|
142
116
|
nodes_only = Stream([child]).filter_type(Node)
|
|
@@ -189,9 +163,40 @@ if char == "a":
|
|
|
189
163
|
stream.end()
|
|
190
164
|
```
|
|
191
165
|
|
|
166
|
+
### Opt usage
|
|
167
|
+
|
|
168
|
+
Get next value as `Opt`:
|
|
169
|
+
|
|
170
|
+
```python
|
|
171
|
+
assert Stream((1, 2, 3, 4, 5)).next().present
|
|
172
|
+
```
|
|
173
|
+
|
|
174
|
+
Mapping:
|
|
175
|
+
```python
|
|
176
|
+
assert Opt("String").map(str.lower).len == 6
|
|
177
|
+
```
|
|
178
|
+
|
|
179
|
+
Get default value:
|
|
180
|
+
```python
|
|
181
|
+
assert Opt(None).get(6) == 6
|
|
182
|
+
assert Opt(None).get(lambda: 6) == 6
|
|
183
|
+
assert Opt(None).if_absent(lambda: 3).present
|
|
184
|
+
```
|
|
185
|
+
|
|
186
|
+
Filter value:
|
|
187
|
+
```python
|
|
188
|
+
assert Opt(0).filter(lambda x: x > 0).absent
|
|
189
|
+
```
|
|
190
|
+
|
|
191
|
+
You can also access optional index elements of the stream, but this will `collect()` and end the stream.
|
|
192
|
+
```python
|
|
193
|
+
assert Stream([])[2].absent
|
|
194
|
+
```
|
|
195
|
+
|
|
192
196
|
## Examples
|
|
193
197
|
|
|
194
|
-
|
|
198
|
+
### Write better code with `Stream`
|
|
199
|
+
|
|
195
200
|
```python
|
|
196
201
|
data = {
|
|
197
202
|
"ranges": [
|
|
@@ -199,22 +204,28 @@ data = {
|
|
|
199
204
|
{"weeks": 1},
|
|
200
205
|
]
|
|
201
206
|
}
|
|
202
|
-
```
|
|
203
207
|
|
|
204
|
-
|
|
205
|
-
|
|
208
|
+
# With tinystream Stream
|
|
209
|
+
for range_data in Opt(data).map_key("ranges").stream().map_kwargs(timedelta):
|
|
210
|
+
pass
|
|
211
|
+
|
|
212
|
+
# Vanilly Python
|
|
206
213
|
if "ranges" in data:
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
214
|
+
range_data: timedelta
|
|
215
|
+
for range_data in map(lambda x: timedelta(**x), data["ranges"]):
|
|
216
|
+
pass
|
|
210
217
|
```
|
|
211
218
|
|
|
212
|
-
|
|
219
|
+
### Write better code with `Opt`
|
|
213
220
|
```python
|
|
214
|
-
|
|
215
|
-
|
|
221
|
+
# tinystream Opt
|
|
222
|
+
var = Opt(my_dict).kmap("key").filter(not_empty).get("default")
|
|
223
|
+
|
|
224
|
+
# Vanilla Python
|
|
225
|
+
var = my_dict["key"] if "key" in my_dict and not_empty(my_dict["key"]) else "default"
|
|
216
226
|
```
|
|
217
227
|
|
|
228
|
+
|
|
218
229
|
## Comparison with other libraries
|
|
219
230
|
|
|
220
231
|
There are a couple of other implementation to fulfill similar requirements.
|
|
@@ -235,17 +246,6 @@ There are a couple of other implementation to fulfill similar requirements.
|
|
|
235
246
|
PYTHONPATH="." pytest --cov=tinystream -n 4 tests/
|
|
236
247
|
```
|
|
237
248
|
|
|
238
|
-
### Release update
|
|
239
|
-
1. Update version in `setup.py`
|
|
240
|
-
2. Package library
|
|
241
|
-
```shell
|
|
242
|
-
python setup.py sdist
|
|
243
|
-
```
|
|
244
|
-
3. Publish library
|
|
245
|
-
```shell
|
|
246
|
-
twine upload dist/tinystream-[version].tar.gz
|
|
247
|
-
```
|
|
248
|
-
|
|
249
249
|
## References
|
|
250
250
|
|
|
251
251
|
- https://github.com/MichaelKim0407/tutorial-pip-package
|
|
@@ -11,13 +11,14 @@ K = TypeVar("K")
|
|
|
11
11
|
Mapper = Callable[[T], R] | Type[R]
|
|
12
12
|
FlatMapper = Callable[[T], Iterable[R]]
|
|
13
13
|
Consumer = Callable[[T], None]
|
|
14
|
-
Comparator = Callable[[T, T],
|
|
14
|
+
Comparator = Callable[[T, T], int]
|
|
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
|
|
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
|
|
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,18 +107,28 @@ 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:
|
|
110
|
-
if
|
|
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
|
|
116
|
-
if
|
|
117
|
-
return
|
|
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
|
|
118
119
|
else:
|
|
119
120
|
return EmptyOpt()
|
|
120
121
|
|
|
122
|
+
def map_key(self, key: Key):
|
|
123
|
+
if _key_exists(self.__val, key):
|
|
124
|
+
return Opt(_get_key_value(self.__val, key))
|
|
125
|
+
else:
|
|
126
|
+
return EmptyOpt()
|
|
127
|
+
|
|
128
|
+
def kmap(self, key: Key):
|
|
129
|
+
"""This is an alias for map_key"""
|
|
130
|
+
return self.map_key(key)
|
|
131
|
+
|
|
121
132
|
def map_keys(self, *iterables):
|
|
122
133
|
inst = self
|
|
123
134
|
for key in iterables:
|
|
@@ -148,10 +159,13 @@ class EmptyOpt(Opt[None]):
|
|
|
148
159
|
def filter(self, predicate: Predicate[T]):
|
|
149
160
|
return self
|
|
150
161
|
|
|
151
|
-
def map_key(self, key:
|
|
162
|
+
def map_key(self, key: Key):
|
|
152
163
|
return self
|
|
153
164
|
|
|
154
|
-
def filter_key(self, key:
|
|
165
|
+
def filter_key(self, key: Key, invert: bool = False):
|
|
166
|
+
return self
|
|
167
|
+
|
|
168
|
+
def filter_key_value(self, key: Key, value: any):
|
|
155
169
|
return self
|
|
156
170
|
|
|
157
171
|
@property
|
|
@@ -224,8 +238,12 @@ class Stream(Iterator[T]):
|
|
|
224
238
|
def map_kwargs(self, mapper: Type[R]) -> "Stream[R]":
|
|
225
239
|
return self.map(lambda x: mapper(**x))
|
|
226
240
|
|
|
227
|
-
def map_key(self, key:
|
|
228
|
-
return self.filter_key(key).map(lambda x:
|
|
241
|
+
def map_key(self, key: Key):
|
|
242
|
+
return self.filter_key(key).map(lambda x: _get_key_value(x, key))
|
|
243
|
+
|
|
244
|
+
def kmap(self, key: Key):
|
|
245
|
+
"""This is an alias for map_key"""
|
|
246
|
+
return self.map_key(key)
|
|
229
247
|
|
|
230
248
|
def map_keys(self, *iterables):
|
|
231
249
|
inst = self
|
|
@@ -242,8 +260,11 @@ class Stream(Iterator[T]):
|
|
|
242
260
|
def filter(self, predicate: Predicate[T]):
|
|
243
261
|
return Stream[T](filter(predicate, self.__iterable))
|
|
244
262
|
|
|
245
|
-
def filter_key(self, key:
|
|
246
|
-
return self.filter(lambda x:
|
|
263
|
+
def filter_key(self, key: Key, invert: bool = False):
|
|
264
|
+
return self.filter(lambda x: _key_exists(x, key, invert))
|
|
265
|
+
|
|
266
|
+
def filter_key_value(self, key: Key, value: any):
|
|
267
|
+
return self.filter(lambda x: _key_exists(x, key, False) and _get_key_value(x, key) == value)
|
|
247
268
|
|
|
248
269
|
def flatmap(self, mapper: FlatMapper[T, R] = None):
|
|
249
270
|
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|