tinystream 0.1.16__tar.gz → 0.1.18__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.16/tinystream.egg-info → tinystream-0.1.18}/PKG-INFO +62 -66
- {tinystream-0.1.16 → tinystream-0.1.18}/README.md +53 -63
- {tinystream-0.1.16 → tinystream-0.1.18}/setup.py +4 -1
- tinystream-0.1.18/tests/test_opt.py +181 -0
- tinystream-0.1.18/tests/test_streams.py +411 -0
- {tinystream-0.1.16 → tinystream-0.1.18/tinystream.egg-info}/PKG-INFO +62 -66
- {tinystream-0.1.16 → tinystream-0.1.18}/tinystream.egg-info/SOURCES.txt +2 -0
- {tinystream-0.1.16 → tinystream-0.1.18}/tinystream.py +19 -20
- {tinystream-0.1.16 → tinystream-0.1.18}/LICENSE +0 -0
- {tinystream-0.1.16 → tinystream-0.1.18}/setup.cfg +0 -0
- {tinystream-0.1.16 → tinystream-0.1.18}/tinystream.egg-info/dependency_links.txt +0 -0
- {tinystream-0.1.16 → tinystream-0.1.18}/tinystream.egg-info/top_level.txt +0 -0
|
@@ -1,14 +1,21 @@
|
|
|
1
|
-
Metadata-Version: 2.
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
2
|
Name: tinystream
|
|
3
|
-
Version: 0.1.
|
|
3
|
+
Version: 0.1.18
|
|
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
|
-
|
|
10
|
-
|
|
9
|
+
Dynamic: author
|
|
10
|
+
Dynamic: description
|
|
11
|
+
Dynamic: description-content-type
|
|
12
|
+
Dynamic: home-page
|
|
13
|
+
Dynamic: license-file
|
|
14
|
+
Dynamic: summary
|
|
15
|
+
|
|
16
|
+
[](https://github.com/mreiche/python-streams/actions/workflows/tests.yml)
|
|
11
17
|
[](https://app.codecov.io/github/mreiche/python-streams)
|
|
18
|
+
[](https://badge.fury.io/py/tinystream)
|
|
12
19
|
|
|
13
20
|
# tinystream / python-streams
|
|
14
21
|
|
|
@@ -39,50 +46,15 @@ Aggregators like `sum()`, `count()`, `max()` will `collect()` the data and end t
|
|
|
39
46
|
|
|
40
47
|
## Built-in Optional support
|
|
41
48
|
|
|
42
|
-
Some aggregators like `sum()`, `max()` are
|
|
43
|
-
|
|
44
|
-
```python
|
|
45
|
-
assert Stream((1, 2, 3, 4, 5)).sum().present
|
|
46
|
-
```
|
|
47
|
-
|
|
48
|
-
Get next value as *optional*:
|
|
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:
|
|
49
|
+
Some aggregators like `sum()`, `max()` are `Opt`:
|
|
75
50
|
|
|
76
51
|
```python
|
|
77
|
-
assert
|
|
52
|
+
assert Stream((1, 2, 3, 4, 5)).sum().get() == 15
|
|
78
53
|
```
|
|
79
54
|
|
|
80
|
-
|
|
81
|
-
```python
|
|
82
|
-
assert Stream([])[2].absent
|
|
83
|
-
```
|
|
55
|
+
## More features
|
|
84
56
|
|
|
85
|
-
|
|
57
|
+
### Type hinting
|
|
86
58
|
|
|
87
59
|
You can typehint datatypes like:
|
|
88
60
|
|
|
@@ -113,8 +85,6 @@ def map_parent(n: Node):
|
|
|
113
85
|
assert stream.map(map_parent).next().get().name == "B"
|
|
114
86
|
```
|
|
115
87
|
|
|
116
|
-
## More features
|
|
117
|
-
|
|
118
88
|
### Typed dictionaries
|
|
119
89
|
|
|
120
90
|
Dictionaries are streamed as `tuple(key, value)`
|
|
@@ -194,9 +164,40 @@ if char == "a":
|
|
|
194
164
|
stream.end()
|
|
195
165
|
```
|
|
196
166
|
|
|
167
|
+
### Opt usage
|
|
168
|
+
|
|
169
|
+
Get next value as `Opt`:
|
|
170
|
+
|
|
171
|
+
```python
|
|
172
|
+
assert Stream((1, 2, 3, 4, 5)).next().present
|
|
173
|
+
```
|
|
174
|
+
|
|
175
|
+
Mapping:
|
|
176
|
+
```python
|
|
177
|
+
assert Opt("String").map(str.lower).len == 6
|
|
178
|
+
```
|
|
179
|
+
|
|
180
|
+
Get default value:
|
|
181
|
+
```python
|
|
182
|
+
assert Opt(None).get(6) == 6
|
|
183
|
+
assert Opt(None).get(lambda: 6) == 6
|
|
184
|
+
assert Opt(None).if_absent(lambda: 3).present
|
|
185
|
+
```
|
|
186
|
+
|
|
187
|
+
Filter value:
|
|
188
|
+
```python
|
|
189
|
+
assert Opt(0).filter(lambda x: x > 0).absent
|
|
190
|
+
```
|
|
191
|
+
|
|
192
|
+
You can also access optional index elements of the stream, but this will `collect()` and end the stream.
|
|
193
|
+
```python
|
|
194
|
+
assert Stream([])[2].absent
|
|
195
|
+
```
|
|
196
|
+
|
|
197
197
|
## Examples
|
|
198
198
|
|
|
199
|
-
|
|
199
|
+
### Write better code with `Stream`
|
|
200
|
+
|
|
200
201
|
```python
|
|
201
202
|
data = {
|
|
202
203
|
"ranges": [
|
|
@@ -204,22 +205,28 @@ data = {
|
|
|
204
205
|
{"weeks": 1},
|
|
205
206
|
]
|
|
206
207
|
}
|
|
207
|
-
```
|
|
208
208
|
|
|
209
|
-
|
|
210
|
-
|
|
209
|
+
# With tinystream Stream
|
|
210
|
+
for range_data in Opt(data).map_key("ranges").stream().map_kwargs(timedelta):
|
|
211
|
+
pass
|
|
212
|
+
|
|
213
|
+
# Vanilly Python
|
|
211
214
|
if "ranges" in data:
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
+
range_data: timedelta
|
|
216
|
+
for range_data in map(lambda x: timedelta(**x), data["ranges"]):
|
|
217
|
+
pass
|
|
215
218
|
```
|
|
216
219
|
|
|
217
|
-
|
|
220
|
+
### Write better code with `Opt`
|
|
218
221
|
```python
|
|
219
|
-
|
|
220
|
-
|
|
222
|
+
# tinystream Opt
|
|
223
|
+
var = Opt(my_dict).kmap("key").filter(not_empty).get("default")
|
|
224
|
+
|
|
225
|
+
# Vanilla Python
|
|
226
|
+
var = my_dict["key"] if "key" in my_dict and not_empty(my_dict["key"]) else "default"
|
|
221
227
|
```
|
|
222
228
|
|
|
229
|
+
|
|
223
230
|
## Comparison with other libraries
|
|
224
231
|
|
|
225
232
|
There are a couple of other implementation to fulfill similar requirements.
|
|
@@ -240,17 +247,6 @@ There are a couple of other implementation to fulfill similar requirements.
|
|
|
240
247
|
PYTHONPATH="." pytest --cov=tinystream -n 4 tests/
|
|
241
248
|
```
|
|
242
249
|
|
|
243
|
-
### Release update
|
|
244
|
-
1. Update version in `setup.py`
|
|
245
|
-
2. Package library
|
|
246
|
-
```shell
|
|
247
|
-
python setup.py sdist
|
|
248
|
-
```
|
|
249
|
-
3. Publish library
|
|
250
|
-
```shell
|
|
251
|
-
twine upload dist/tinystream-[version].tar.gz
|
|
252
|
-
```
|
|
253
|
-
|
|
254
250
|
## References
|
|
255
251
|
|
|
256
252
|
- https://github.com/MichaelKim0407/tutorial-pip-package
|
|
@@ -1,5 +1,6 @@
|
|
|
1
|
-

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