stdlibx-streams 0.1.0__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.
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 Lucino772
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
@@ -0,0 +1,25 @@
1
+ Metadata-Version: 2.4
2
+ Name: stdlibx-streams
3
+ Version: 0.1.0
4
+ Summary: stdlibx-streams
5
+ Author: Lucino772
6
+ License-Expression: MIT
7
+ License-File: LICENSE
8
+ Requires-Dist: typing-extensions>=4.10.0,<5
9
+ Requires-Python: >=3.9.25
10
+ Project-URL: Documentation, http://stdlibx.lucapalmi.com/
11
+ Project-URL: Repository, https://github.com/Lucino772/stdlibx
12
+ Description-Content-Type: text/markdown
13
+
14
+ [![docs](https://github.com/Lucino772/stdlibx/actions/workflows/deploy-docs.yaml/badge.svg?branch=main)](https://github.com/Lucino772/stdlibx/actions/workflows/deploy-docs.yaml)
15
+
16
+ # stdlibx
17
+ **stdlibx** is a collection of small, focused Python utilities that simplify common programming patterns and help you write clearer, more composable code.
18
+
19
+ It provides practical tools for handling optional values, explicit error management, cancellation, composition, configuration, pattern matching, and reactive flows.
20
+
21
+ ## Documentation
22
+ Checkout the [documentation](http://stdlibx.lucapalmi.com/)
23
+
24
+ ## Licence
25
+ This project uses a **MIT** Licence [view](https://github.com/Lucino772/stdlibx/blob/main/LICENSE)
@@ -0,0 +1,12 @@
1
+ [![docs](https://github.com/Lucino772/stdlibx/actions/workflows/deploy-docs.yaml/badge.svg?branch=main)](https://github.com/Lucino772/stdlibx/actions/workflows/deploy-docs.yaml)
2
+
3
+ # stdlibx
4
+ **stdlibx** is a collection of small, focused Python utilities that simplify common programming patterns and help you write clearer, more composable code.
5
+
6
+ It provides practical tools for handling optional values, explicit error management, cancellation, composition, configuration, pattern matching, and reactive flows.
7
+
8
+ ## Documentation
9
+ Checkout the [documentation](http://stdlibx.lucapalmi.com/)
10
+
11
+ ## Licence
12
+ This project uses a **MIT** Licence [view](https://github.com/Lucino772/stdlibx/blob/main/LICENSE)
@@ -0,0 +1,25 @@
1
+ [project]
2
+ name = "stdlibx-streams"
3
+ version = "0.1.0"
4
+ description = "stdlibx-streams"
5
+ license = "MIT"
6
+ license-files = ["LICEN[CS]E*"]
7
+ readme = "README.md"
8
+ authors = [
9
+ { name = "Lucino772" }
10
+ ]
11
+ requires-python = ">=3.9.25"
12
+ dependencies = [
13
+ "typing_extensions>=4.10.0,<5"
14
+ ]
15
+
16
+ [project.urls]
17
+ Documentation = "http://stdlibx.lucapalmi.com/"
18
+ Repository = "https://github.com/Lucino772/stdlibx"
19
+
20
+ [tool.uv.build-backend]
21
+ module-name = "stdlibx.streams"
22
+
23
+ [build-system]
24
+ requires = ["uv_build>=0.9.28,<0.10.0"]
25
+ build-backend = "uv_build"
@@ -0,0 +1,5 @@
1
+ from stdlibx.streams._subject import Subject
2
+ from stdlibx.streams._types import Disposable, Observable, Observer
3
+ from stdlibx.streams.utils import as_observable
4
+
5
+ __all__ = ["Disposable", "Observable", "Observer", "Subject", "as_observable"]
@@ -0,0 +1,39 @@
1
+ from __future__ import annotations
2
+
3
+ from typing import TYPE_CHECKING, Generic, TypeVar
4
+
5
+ if TYPE_CHECKING:
6
+ from collections.abc import Callable
7
+
8
+ from stdlibx.streams._types import Disposable, Observable, Operation
9
+
10
+ T = TypeVar("T")
11
+ U = TypeVar("U")
12
+
13
+
14
+ class ObservableBase(Generic[T]):
15
+ def __init__(self) -> None:
16
+ self._subscribers: list[Callable[[T], None]] = []
17
+
18
+ def apply(self, func: Operation[Observable[T], U]) -> U:
19
+ return func(self)
20
+
21
+ def __or__(self, func: Operation[Observable[T], U]) -> U:
22
+ return self.apply(func)
23
+
24
+ def subscribe(self, func: Callable[[T], None]) -> Disposable:
25
+ self._subscribers.append(func)
26
+ return _FuncDisposable(lambda: self._subscribers.remove(func))
27
+
28
+
29
+ class _FuncDisposable:
30
+ def __init__(self, func: Callable[[], None]) -> None:
31
+ self.__func = func
32
+ self.__disposed = False
33
+
34
+ def dispose(self) -> None:
35
+ if self.__disposed:
36
+ return
37
+
38
+ self.__func()
39
+ self.__disposed = True
@@ -0,0 +1,32 @@
1
+ from __future__ import annotations
2
+
3
+ from typing import TYPE_CHECKING, TypeVar, cast
4
+
5
+ from stdlibx.streams._abc import ObservableBase
6
+
7
+ if TYPE_CHECKING:
8
+ from collections.abc import Callable
9
+
10
+ from stdlibx.streams._types import Disposable
11
+
12
+ T = TypeVar("T")
13
+
14
+
15
+ class Subject(ObservableBase[T]):
16
+ def __init__(self, initial: T, /) -> None:
17
+ super().__init__()
18
+
19
+ self.__value = initial
20
+
21
+ def subscribe(self, func: Callable[[T], None]) -> Disposable:
22
+ func(self.__value)
23
+ return super().subscribe(func)
24
+
25
+ def push(self, val: T | Callable[[T], T]) -> None:
26
+ if callable(val):
27
+ self.__value = cast("T", val(self.__value))
28
+ else:
29
+ self.__value = cast("T", val)
30
+
31
+ for subscriber in self._subscribers:
32
+ subscriber(self.__value)
@@ -0,0 +1,34 @@
1
+ from __future__ import annotations
2
+
3
+ from typing import Callable, Protocol, TypeVar, runtime_checkable
4
+
5
+ from typing_extensions import TypeAlias
6
+
7
+ T = TypeVar("T")
8
+ U = TypeVar("U")
9
+
10
+ Operation: TypeAlias = Callable[[T], U]
11
+
12
+
13
+ T_co = TypeVar("T_co", covariant=True)
14
+ U_contra = TypeVar("U_contra", contravariant=True)
15
+
16
+
17
+ class Disposable(Protocol):
18
+ def dispose(self) -> None: ...
19
+
20
+
21
+ @runtime_checkable
22
+ class Observable(Protocol[T_co]):
23
+ def apply(self, func: Operation[Observable[T_co], U_contra]) -> U_contra: ...
24
+
25
+ def __or__(self, func: Operation[Observable[T_co], U_contra]) -> U_contra: ...
26
+
27
+ def subscribe(self, func: Callable[[T_co], None]) -> Disposable: ...
28
+
29
+
30
+ class Observer(Protocol[U_contra]):
31
+ def push(self, val: U_contra) -> None: ...
32
+
33
+
34
+ Subscriber: TypeAlias = Callable[[Observer[T]], Disposable]
@@ -0,0 +1,23 @@
1
+ from stdlibx.streams.fn.base import (
2
+ as_tuple,
3
+ combine,
4
+ distinct,
5
+ for_,
6
+ if_,
7
+ is_,
8
+ is_not_none,
9
+ map_,
10
+ start_with,
11
+ )
12
+
13
+ __all__ = [
14
+ "as_tuple",
15
+ "combine",
16
+ "distinct",
17
+ "for_",
18
+ "if_",
19
+ "is_",
20
+ "is_not_none",
21
+ "map_",
22
+ "start_with",
23
+ ]
@@ -0,0 +1,68 @@
1
+ from __future__ import annotations
2
+
3
+ import operator
4
+ from functools import partial
5
+ from typing import (
6
+ TYPE_CHECKING,
7
+ TypeVar,
8
+ Union,
9
+ )
10
+
11
+ from stdlibx.streams import methods
12
+ from typing_extensions import TypeGuard, TypeVarTuple, Unpack
13
+
14
+ if TYPE_CHECKING:
15
+ from collections.abc import Callable, Iterable
16
+
17
+ from stdlibx.streams._types import (
18
+ Observable,
19
+ Operation,
20
+ )
21
+
22
+ T = TypeVar("T")
23
+ U = TypeVar("U")
24
+ Ts = TypeVarTuple("Ts")
25
+
26
+
27
+ def map_(func: Callable[[T], U]) -> Operation[Observable[T], Observable[U]]:
28
+ return partial(methods.map_, func=func)
29
+
30
+
31
+ def if_(func: Callable[[T], bool]) -> Operation[Observable[T], Observable[T]]:
32
+ return partial(methods.if_, func=func)
33
+
34
+
35
+ def is_(
36
+ func: Callable[[Union[T, U]], TypeGuard[U]],
37
+ ) -> Operation[Observable[Union[T, U]], Observable[U]]:
38
+ return partial(methods.is_, func=func)
39
+
40
+
41
+ def is_not_none() -> Operation[Observable[Union[T, None]], Observable[T]]:
42
+ return methods.is_not_none
43
+
44
+
45
+ def as_tuple() -> Operation[Observable[T], Observable[tuple[T]]]:
46
+ return methods.as_tuple
47
+
48
+
49
+ def combine(
50
+ other: Observable[U],
51
+ ) -> Operation[Observable[tuple[Unpack[Ts]]], Observable[tuple[Unpack[Ts], U]]]:
52
+ return partial(methods.combine, other=other)
53
+
54
+
55
+ def start_with(value: T) -> Operation[Observable[T], Observable[T]]:
56
+ return partial(methods.start_with, value=value)
57
+
58
+
59
+ def for_(
60
+ func: Callable[[T], U],
61
+ ) -> Operation[Observable[Iterable[T]], Observable[Iterable[U]]]:
62
+ return partial(methods.for_, func=func)
63
+
64
+
65
+ def distinct(
66
+ equal_fn: Callable[[T, T], bool] = operator.eq,
67
+ ) -> Operation[Observable[T], Observable[T]]:
68
+ return partial(methods.distinct, equal_fn=equal_fn)
@@ -0,0 +1,23 @@
1
+ from stdlibx.streams.methods.base import (
2
+ as_tuple,
3
+ combine,
4
+ distinct,
5
+ for_,
6
+ if_,
7
+ is_,
8
+ is_not_none,
9
+ map_,
10
+ start_with,
11
+ )
12
+
13
+ __all__ = [
14
+ "as_tuple",
15
+ "combine",
16
+ "distinct",
17
+ "for_",
18
+ "if_",
19
+ "is_",
20
+ "is_not_none",
21
+ "map_",
22
+ "start_with",
23
+ ]
@@ -0,0 +1,151 @@
1
+ from __future__ import annotations
2
+
3
+ import operator
4
+ from functools import partial
5
+ from typing import (
6
+ TYPE_CHECKING,
7
+ Generic,
8
+ TypeVar,
9
+ Union,
10
+ )
11
+
12
+ from typing_extensions import TypeGuard, TypeVarTuple, Unpack
13
+
14
+ if TYPE_CHECKING:
15
+ from collections.abc import Callable, Iterable
16
+
17
+ from stdlibx.streams._types import (
18
+ Disposable,
19
+ Observable,
20
+ Observer,
21
+ Operation,
22
+ Subscriber,
23
+ )
24
+
25
+ T = TypeVar("T")
26
+ U = TypeVar("U")
27
+ Ts = TypeVarTuple("Ts")
28
+
29
+
30
+ def map_(source: Observable[T], func: Callable[[T], U]) -> Observable[U]:
31
+ def _subscribe(other: Observer[U]) -> Disposable:
32
+ return source.subscribe(lambda val: other.push(func(val)))
33
+
34
+ return _Observable(_subscribe)
35
+
36
+
37
+ def if_(source: Observable[T], func: Callable[[T], bool]) -> Observable[T]:
38
+ def _subscribe(other: Observer[T]) -> Disposable:
39
+ return source.subscribe(
40
+ lambda val: other.push(val) if func(val) is True else None
41
+ )
42
+
43
+ return _Observable(_subscribe)
44
+
45
+
46
+ def is_(
47
+ source: Observable[Union[T, U]], func: Callable[[Union[T, U]], TypeGuard[U]]
48
+ ) -> Observable[U]:
49
+ def _subscribe(other: Observer[U]) -> Disposable:
50
+ return source.subscribe(lambda val: other.push(val) if func(val) else None)
51
+
52
+ return _Observable(_subscribe)
53
+
54
+
55
+ def is_not_none(source: Observable[Union[T, None]]) -> Observable[T]:
56
+ return is_(source, lambda val: val is not None) # type: ignore
57
+
58
+
59
+ def as_tuple(source: Observable[T]) -> Observable[tuple[T]]:
60
+ return map_(source, lambda v: (v,))
61
+
62
+
63
+ def combine(
64
+ source: Observable[tuple[Unpack[Ts]]], other: Observable[U]
65
+ ) -> Observable[tuple[Unpack[Ts], U]]:
66
+ values = {}
67
+
68
+ def _update(observer: Observer[tuple[Unpack[Ts]]], **kwargs) -> None:
69
+ nonlocal values
70
+ values.update(kwargs)
71
+
72
+ if ("first" not in values) or ("second" not in values):
73
+ return
74
+
75
+ if isinstance(values["first"], tuple):
76
+ observer.push((*values["first"], values["second"])) # type: ignore
77
+ else:
78
+ observer.push((values["first"], values["second"])) # type: ignore
79
+
80
+ def _subscribe(observer: Observer[tuple]) -> Disposable:
81
+ _updater = partial(_update, observer)
82
+ return _CompositeDisposable(
83
+ [
84
+ source.subscribe(lambda v: _updater(first=v)),
85
+ other.subscribe(lambda v: _updater(second=v)),
86
+ ]
87
+ )
88
+
89
+ return _Observable(_subscribe)
90
+
91
+
92
+ def start_with(source: Observable[T], value: T) -> Observable[T]:
93
+ def _subscribe(other: Observer[T]) -> Disposable:
94
+ other.push(value)
95
+ return source.subscribe(other.push)
96
+
97
+ return _Observable(_subscribe)
98
+
99
+
100
+ def for_(
101
+ source: Observable[Iterable[T]], func: Callable[[T], U]
102
+ ) -> Observable[Iterable[U]]:
103
+ return map_(source, lambda items: [func(item) for item in items])
104
+
105
+
106
+ def distinct(
107
+ source: Observable[T], equal_fn: Callable[[T, T], bool] = operator.eq
108
+ ) -> Observable[T]:
109
+ def _subscribe(other: Observer[T]) -> Disposable:
110
+ curr_val: "Union[T, None]" = None
111
+
112
+ def _subscriber(other: Observer[T], val: T) -> None:
113
+ nonlocal curr_val
114
+ if curr_val is None or equal_fn(curr_val, val) is False:
115
+ other.push(val)
116
+ curr_val = val
117
+
118
+ return source.subscribe(partial(_subscriber, other))
119
+
120
+ return _Observable(_subscribe)
121
+
122
+
123
+ class _Observable(Generic[T]):
124
+ def __init__(self, subscribe: Subscriber[T]) -> None:
125
+ self.__subscribe = subscribe
126
+
127
+ def apply(self, func: Operation[Observable[T], U]) -> U:
128
+ return func(self)
129
+
130
+ def __or__(self, func: Operation[Observable[T], U]) -> U:
131
+ return self.apply(func)
132
+
133
+ def subscribe(self, func: Callable[[T], None]) -> Disposable:
134
+ return self.__subscribe(_CallbackObserver(func))
135
+
136
+
137
+ class _CallbackObserver(Generic[T]):
138
+ def __init__(self, func: Callable[[T], None]) -> None:
139
+ self.__func = func
140
+
141
+ def push(self, val: T) -> None:
142
+ self.__func(val)
143
+
144
+
145
+ class _CompositeDisposable:
146
+ def __init__(self, disposables: list[Disposable]) -> None:
147
+ self.__disposables = disposables
148
+
149
+ def dispose(self) -> None:
150
+ for disposable in self.__disposables:
151
+ disposable.dispose()
@@ -0,0 +1,14 @@
1
+ from __future__ import annotations
2
+
3
+ from typing import TypeVar
4
+
5
+ from stdlibx.streams._subject import Subject
6
+ from stdlibx.streams._types import Observable
7
+
8
+ T = TypeVar("T")
9
+
10
+
11
+ def as_observable(value: Observable[T] | T) -> Observable[T]:
12
+ if isinstance(value, Observable):
13
+ return value
14
+ return Subject(value)