Skip to content

Commit

Permalink
1.4.0-rc3: wip to be splitten
Browse files Browse the repository at this point in the history
  • Loading branch information
ebonnal committed Dec 17, 2024
1 parent a36c3fc commit d985769
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 24 deletions.
25 changes: 15 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -236,14 +236,14 @@ assert list(slow_integers) == list(integers) # takes 10 * 0.1 = 1 second
> Groups elements into `List`s:
```python
integers_5_by_5: Stream[List[int]] = integers.group(size=5)
groups_of_5_ints: Stream[List[int]] = integers.group(size=5)

assert list(integers_5_by_5) == [[0, 1, 2, 3, 4], [5, 6, 7, 8, 9]]
assert list(groups_of_5_ints) == [[0, 1, 2, 3, 4], [5, 6, 7, 8, 9]]
```
```python
integers_by_parity: Stream[List[int]] = integers.group(by=lambda n: n % 2)
int_groups_of_same_parity: Stream[List[int]] = integers.group(by=lambda n: n % 2)

assert list(integers_by_parity) == [[0, 2, 4, 6, 8], [1, 3, 5, 7, 9]]
assert list(int_groups_of_same_parity) == [[0, 2, 4, 6, 8], [1, 3, 5, 7, 9]]
```
```python
from datetime import timedelta
Expand All @@ -259,18 +259,18 @@ assert list(integers_within_1s) == [[0, 1, 2], [3, 4], [5, 6], [7, 8], [9]]

> Mix `size`/`by`/`interval` parameters:
```python
integers_2_by_2_by_parity: Stream[List[int]] = integers.group(by=lambda n: n % 2, size=2)
groups_of_2_ints_of_same_parity: Stream[List[int]] = integers.group(by=lambda n: n % 2, size=2)

assert list(integers_2_by_2_by_parity) == [[0, 2], [1, 3], [4, 6], [5, 7], [8], [9]]
assert list(groups_of_2_ints_of_same_parity) == [[0, 2], [1, 3], [4, 6], [5, 7], [8], [9]]
```

### `.groupby`

> Like `.group`, but groups into `(key, elements)` tuples:
```python
integers_by_parity: Stream[Tuple[str, List[int]]] = integers.groupby(lambda n: "odd" if n % 2 else "pair")
int_groups_of_same_parity: Stream[Tuple[str, List[int]]] = integers.groupby(lambda n: "odd" if n % 2 else "pair")

assert list(integers_by_parity) == [("pair", [0, 2, 4, 6, 8]), ("odd", [1, 3, 5, 7, 9])]
assert list(int_groups_of_same_parity) == [("pair", [0, 2, 4, 6, 8]), ("odd", [1, 3, 5, 7, 9])]
```

> [!TIP]
Expand All @@ -279,7 +279,7 @@ assert list(integers_by_parity) == [("pair", [0, 2, 4, 6, 8]), ("odd", [1, 3, 5,
```python
from streamable import star

counts_by_parity: Stream[Tuple[str, int]] = integers_by_parity.map(star(lambda parity, ints: (parity, len(ints))))
counts_by_parity: Stream[Tuple[str, int]] = int_groups_of_same_parity.map(star(lambda parity, ints: (parity, len(ints))))

assert list(counts_by_parity) == [("pair", 5), ("odd", 5)]
```
Expand All @@ -289,7 +289,7 @@ assert list(counts_by_parity) == [("pair", 5), ("odd", 5)]
> Ungroups elements assuming that they are `Iterable`s:
```python
pair_then_odd_integers: Stream[int] = integers_by_parity.flatten()
pair_then_odd_integers: Stream[int] = int_groups_of_same_parity.flatten()

assert list(pair_then_odd_integers) == [0, 2, 4, 6, 8, 1, 3, 5, 7, 9]
```
Expand Down Expand Up @@ -410,6 +410,11 @@ INFO: [duration=0:00:05.039571 errors=0] 10 integers yielded
> [!WARNING]
> It is mute between *v1.1.0* and *v1.3.1*, please `pip install --upgrade streamable`
## `+`

> Concatenates streams:
assert list(integers + integers) == [0, 1, 2, 3 ,4, 5, 6, 7, 8, 9, 0, 1, 2, 3 ,4, 5, 6, 7, 8, 9]

## `zip`

> [!TIP]
Expand Down
8 changes: 7 additions & 1 deletion streamable/stream.py
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ def __add__(self, other: "Stream[T]") -> "Stream[T]":
"""
`a + b` returns a stream yielding all elements of `a`, followed by all elements of `b`.
"""
return cast(Stream[T], Stream([self, other].__iter__).flatten())
return cast(Stream[T], Stream((self, other)).flatten())

def __iter__(self) -> Iterator[T]:
from streamable.visitors.iterator import IteratorVisitor
Expand Down Expand Up @@ -251,6 +251,12 @@ def flatten(
self: "Stream[Set[U]]",
concurrency: int = 1,
) -> "Stream[U]": ...

@overload
def flatten(
self: "Stream[range]",
concurrency: int = 1,
) -> "Stream[int]": ...
# fmt: on

def flatten(
Expand Down
29 changes: 17 additions & 12 deletions tests/test_readme.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
ZeroDivisionError
)

integers_by_parity: Stream[List[int]] = integers.group(by=lambda n: n % 2)
int_groups_of_same_parity: Stream[List[int]] = integers.group(by=lambda n: n % 2)

slow_integers: Stream[int] = integers.throttle(per_second=5)

Expand Down Expand Up @@ -115,12 +115,12 @@ def test_throttle_example(self) -> None:
assert list(slow_integers) == list(integers) # takes 10 * 0.1 = 1 second

def test_group_example(self) -> None:
global integers_by_parity
integers_5_by_5: Stream[List[int]] = integers.group(size=5)
global int_groups_of_same_parity
groups_of_5_ints: Stream[List[int]] = integers.group(size=5)

assert list(integers_5_by_5) == [[0, 1, 2, 3, 4], [5, 6, 7, 8, 9]]
assert list(groups_of_5_ints) == [[0, 1, 2, 3, 4], [5, 6, 7, 8, 9]]

assert list(integers_by_parity) == [[0, 2, 4, 6, 8], [1, 3, 5, 7, 9]]
assert list(int_groups_of_same_parity) == [[0, 2, 4, 6, 8], [1, 3, 5, 7, 9]]

from datetime import timedelta

Expand All @@ -130,11 +130,11 @@ def test_group_example(self) -> None:

assert list(integers_within_1s) == [[0, 1, 2], [3, 4], [5, 6], [7, 8], [9]]

integers_2_by_2_by_parity: Stream[List[int]] = integers.group(
groups_of_2_ints_of_same_parity: Stream[List[int]] = integers.group(
by=lambda n: n % 2, size=2
)

assert list(integers_2_by_2_by_parity) == [
assert list(groups_of_2_ints_of_same_parity) == [
[0, 2],
[1, 3],
[4, 6],
Expand All @@ -144,26 +144,26 @@ def test_group_example(self) -> None:
]

def test_groupby_example(self) -> None:
integers_by_parity: Stream[Tuple[str, List[int]]] = integers.groupby(
int_groups_of_same_parity: Stream[Tuple[str, List[int]]] = integers.groupby(
lambda n: "odd" if n % 2 else "pair"
)

assert list(integers_by_parity) == [
assert list(int_groups_of_same_parity) == [
("pair", [0, 2, 4, 6, 8]),
("odd", [1, 3, 5, 7, 9]),
]

from streamable import star

counts_by_parity: Stream[Tuple[str, int]] = integers_by_parity.map(
counts_by_parity: Stream[Tuple[str, int]] = int_groups_of_same_parity.map(
star(lambda parity, ints: (parity, len(ints)))
)

assert list(counts_by_parity) == [("pair", 5), ("odd", 5)]

def test_flatten_example(self) -> None:
global integers_by_parity
pair_then_odd_integers: Stream[int] = integers_by_parity.flatten()
global int_groups_of_same_parity
pair_then_odd_integers: Stream[int] = int_groups_of_same_parity.flatten()

assert list(pair_then_odd_integers) == [0, 2, 4, 6, 8, 1, 3, 5, 7, 9]

Expand Down Expand Up @@ -242,6 +242,11 @@ def test_distinct_example(self) -> None:
def test_observe_example(self) -> None:
observed_slow_integers: Stream[int] = slow_integers.observe("integers")

def test_plus_example(self) -> None:
# fmt: off
assert list(integers + integers) == [0, 1, 2, 3 ,4, 5, 6, 7, 8, 9, 0, 1, 2, 3 ,4, 5, 6, 7, 8, 9]
# fmt: on

def test_zip_example(self) -> None:
from streamable import star

Expand Down
3 changes: 3 additions & 0 deletions tests/test_stream.py
Original file line number Diff line number Diff line change
Expand Up @@ -627,6 +627,9 @@ def test_flatten(self, concurrency) -> None:
):
next(iter(Stream(cast(Iterable, src)).flatten()))

# test typing with ranges
_: Stream[int] = Stream((src, src)).flatten()

def test_flatten_concurrency(self) -> None:
iterable_size = 5
runtime, res = timestream(
Expand Down
2 changes: 1 addition & 1 deletion version.py
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
# to show the CHANGELOG: git log -- version.py
__version__ = "1.4.0-rc2"
__version__ = "1.4.0-rc3"

0 comments on commit d985769

Please sign in to comment.