stdlibx-option 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.
- stdlibx_option-0.1.0/LICENSE +21 -0
- stdlibx_option-0.1.0/PKG-INFO +25 -0
- stdlibx_option-0.1.0/README.md +12 -0
- stdlibx_option-0.1.0/pyproject.toml +25 -0
- stdlibx_option-0.1.0/src/stdlibx/option/__init__.py +23 -0
- stdlibx_option-0.1.0/src/stdlibx/option/_errors.py +10 -0
- stdlibx_option-0.1.0/src/stdlibx/option/_option.py +91 -0
- stdlibx_option-0.1.0/src/stdlibx/option/_types.py +10 -0
- stdlibx_option-0.1.0/src/stdlibx/option/fn/__init__.py +51 -0
- stdlibx_option-0.1.0/src/stdlibx/option/fn/base.py +90 -0
- stdlibx_option-0.1.0/src/stdlibx/option/fn/collect.py +111 -0
- stdlibx_option-0.1.0/src/stdlibx/option/fn/result.py +26 -0
- stdlibx_option-0.1.0/src/stdlibx/option/methods/__init__.py +48 -0
- stdlibx_option-0.1.0/src/stdlibx/option/methods/base.py +127 -0
- stdlibx_option-0.1.0/src/stdlibx/option/methods/result.py +31 -0
- stdlibx_option-0.1.0/src/stdlibx/option/py.typed +0 -0
|
@@ -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-option
|
|
3
|
+
Version: 0.1.0
|
|
4
|
+
Summary: stdlibx-option
|
|
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
|
|
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
|
+
[](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
|
+
[](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-option"
|
|
3
|
+
version = "0.1.0"
|
|
4
|
+
description = "stdlibx-option"
|
|
5
|
+
license = "MIT"
|
|
6
|
+
license-files = ["LICEN[CS]E*"]
|
|
7
|
+
readme = "README.md"
|
|
8
|
+
authors = [
|
|
9
|
+
{ name = "Lucino772" }
|
|
10
|
+
]
|
|
11
|
+
requires-python = ">=3.9"
|
|
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.option"
|
|
22
|
+
|
|
23
|
+
[build-system]
|
|
24
|
+
requires = ["uv_build>=0.9.28,<0.10.0"]
|
|
25
|
+
build-backend = "uv_build"
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
from stdlibx.option._errors import OptionError, OptionExpectError, OptionUnwrapError
|
|
2
|
+
from stdlibx.option._option import (
|
|
3
|
+
Nothing,
|
|
4
|
+
Option,
|
|
5
|
+
Some,
|
|
6
|
+
as_optional,
|
|
7
|
+
is_none,
|
|
8
|
+
is_some,
|
|
9
|
+
optional_of,
|
|
10
|
+
)
|
|
11
|
+
|
|
12
|
+
__all__ = [
|
|
13
|
+
"Nothing",
|
|
14
|
+
"Option",
|
|
15
|
+
"OptionError",
|
|
16
|
+
"OptionExpectError",
|
|
17
|
+
"OptionUnwrapError",
|
|
18
|
+
"Some",
|
|
19
|
+
"as_optional",
|
|
20
|
+
"is_none",
|
|
21
|
+
"is_some",
|
|
22
|
+
"optional_of",
|
|
23
|
+
]
|
|
@@ -0,0 +1,91 @@
|
|
|
1
|
+
from __future__ import annotations
|
|
2
|
+
|
|
3
|
+
from functools import wraps
|
|
4
|
+
from typing import TYPE_CHECKING, Callable, Generic, TypeVar
|
|
5
|
+
|
|
6
|
+
from typing_extensions import ParamSpec, TypeAlias, TypeGuard
|
|
7
|
+
|
|
8
|
+
if TYPE_CHECKING:
|
|
9
|
+
from stdlibx.option._types import Operation
|
|
10
|
+
|
|
11
|
+
|
|
12
|
+
T = TypeVar("T")
|
|
13
|
+
E = TypeVar("E")
|
|
14
|
+
U = TypeVar("U")
|
|
15
|
+
P = ParamSpec("P")
|
|
16
|
+
|
|
17
|
+
Option: TypeAlias = "Some[T] | Nothing[T]"
|
|
18
|
+
|
|
19
|
+
|
|
20
|
+
def is_some(opt: Option[T]) -> TypeGuard[Some[T]]:
|
|
21
|
+
return opt.is_some()
|
|
22
|
+
|
|
23
|
+
|
|
24
|
+
def is_none(opt: Option[T]) -> TypeGuard[Nothing[T]]:
|
|
25
|
+
return opt.is_none()
|
|
26
|
+
|
|
27
|
+
|
|
28
|
+
def optional_of(
|
|
29
|
+
func: Callable[P, T | None], *args: P.args, **kwargs: P.kwargs
|
|
30
|
+
) -> Option[T]:
|
|
31
|
+
value = func(*args, **kwargs)
|
|
32
|
+
if value is None:
|
|
33
|
+
return Nothing()
|
|
34
|
+
return Some(value)
|
|
35
|
+
|
|
36
|
+
|
|
37
|
+
def as_optional(func: Callable[P, T | None]) -> Callable[P, Option[T]]:
|
|
38
|
+
@wraps(func)
|
|
39
|
+
def _wrapped(*args: P.args, **kwargs: P.kwargs) -> Option[T]:
|
|
40
|
+
result = func(*args, **kwargs)
|
|
41
|
+
if result is None:
|
|
42
|
+
return Nothing()
|
|
43
|
+
return Some(result)
|
|
44
|
+
|
|
45
|
+
return _wrapped
|
|
46
|
+
|
|
47
|
+
|
|
48
|
+
class Some(Generic[T]):
|
|
49
|
+
__match_args__ = ("value",)
|
|
50
|
+
__slots__ = ("value",)
|
|
51
|
+
|
|
52
|
+
value: T
|
|
53
|
+
|
|
54
|
+
def __init__(self, value: T) -> None:
|
|
55
|
+
self.value = value
|
|
56
|
+
|
|
57
|
+
def __repr__(self) -> str:
|
|
58
|
+
return f"Some({self.value!r})"
|
|
59
|
+
|
|
60
|
+
def __eq__(self, other: object) -> bool:
|
|
61
|
+
if isinstance(other, Some):
|
|
62
|
+
return other.value == self.value
|
|
63
|
+
return False
|
|
64
|
+
|
|
65
|
+
def is_some(self):
|
|
66
|
+
return True
|
|
67
|
+
|
|
68
|
+
def is_none(self):
|
|
69
|
+
return False
|
|
70
|
+
|
|
71
|
+
def apply(self, operation: Operation[Option[T], U]) -> U:
|
|
72
|
+
return operation(self)
|
|
73
|
+
|
|
74
|
+
|
|
75
|
+
class Nothing(Generic[T]):
|
|
76
|
+
__slots__ = ()
|
|
77
|
+
|
|
78
|
+
def __repr__(self) -> str:
|
|
79
|
+
return "Nothing()"
|
|
80
|
+
|
|
81
|
+
def __eq__(self, other: object) -> bool:
|
|
82
|
+
return isinstance(other, Nothing)
|
|
83
|
+
|
|
84
|
+
def is_some(self):
|
|
85
|
+
return False
|
|
86
|
+
|
|
87
|
+
def is_none(self):
|
|
88
|
+
return True
|
|
89
|
+
|
|
90
|
+
def apply(self, operation: Operation[Option[T], U]) -> U:
|
|
91
|
+
return operation(self)
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
from stdlibx.option.fn.base import (
|
|
2
|
+
and_,
|
|
3
|
+
and_then,
|
|
4
|
+
expect,
|
|
5
|
+
filter_,
|
|
6
|
+
flatten,
|
|
7
|
+
inspect,
|
|
8
|
+
is_none_or,
|
|
9
|
+
is_some_and,
|
|
10
|
+
map_,
|
|
11
|
+
map_or,
|
|
12
|
+
map_or_else,
|
|
13
|
+
or_,
|
|
14
|
+
or_else,
|
|
15
|
+
unwrap,
|
|
16
|
+
unwrap_or,
|
|
17
|
+
unwrap_or_else,
|
|
18
|
+
xor,
|
|
19
|
+
zipped,
|
|
20
|
+
)
|
|
21
|
+
from stdlibx.option.fn.collect import collect, collect_all
|
|
22
|
+
|
|
23
|
+
__all__ = [
|
|
24
|
+
"and_",
|
|
25
|
+
"and_then",
|
|
26
|
+
"collect",
|
|
27
|
+
"collect_all",
|
|
28
|
+
"expect",
|
|
29
|
+
"filter_",
|
|
30
|
+
"flatten",
|
|
31
|
+
"inspect",
|
|
32
|
+
"is_none_or",
|
|
33
|
+
"is_some_and",
|
|
34
|
+
"map_",
|
|
35
|
+
"map_or",
|
|
36
|
+
"map_or_else",
|
|
37
|
+
"or_",
|
|
38
|
+
"or_else",
|
|
39
|
+
"unwrap",
|
|
40
|
+
"unwrap_or",
|
|
41
|
+
"unwrap_or_else",
|
|
42
|
+
"xor",
|
|
43
|
+
"zipped",
|
|
44
|
+
]
|
|
45
|
+
|
|
46
|
+
try:
|
|
47
|
+
from stdlibx.option.fn.result import ok_or, ok_or_else, transpose
|
|
48
|
+
|
|
49
|
+
__all__ += ["ok_or", "ok_or_else", "transpose"]
|
|
50
|
+
except ImportError:
|
|
51
|
+
pass
|
|
@@ -0,0 +1,90 @@
|
|
|
1
|
+
from __future__ import annotations
|
|
2
|
+
|
|
3
|
+
from functools import partial
|
|
4
|
+
from typing import TYPE_CHECKING, Callable, TypeVar
|
|
5
|
+
|
|
6
|
+
from stdlibx.option import Option, methods
|
|
7
|
+
from typing_extensions import TypeVarTuple, Unpack
|
|
8
|
+
|
|
9
|
+
if TYPE_CHECKING:
|
|
10
|
+
from stdlibx.option._option import Operation
|
|
11
|
+
|
|
12
|
+
T = TypeVar("T")
|
|
13
|
+
U = TypeVar("U")
|
|
14
|
+
Ts = TypeVarTuple("Ts")
|
|
15
|
+
|
|
16
|
+
|
|
17
|
+
def is_some_and(func: Callable[[T], bool]) -> Operation[Option[T], bool]:
|
|
18
|
+
return partial(methods.is_some_and, func=func)
|
|
19
|
+
|
|
20
|
+
|
|
21
|
+
def is_none_or(func: Callable[[T], bool]) -> Operation[Option[T], bool]:
|
|
22
|
+
return partial(methods.is_none_or, func=func)
|
|
23
|
+
|
|
24
|
+
|
|
25
|
+
def expect(msg: str) -> Operation[Option[T], T]:
|
|
26
|
+
return partial(methods.expect, msg=msg)
|
|
27
|
+
|
|
28
|
+
|
|
29
|
+
def unwrap() -> Operation[Option[T], T]:
|
|
30
|
+
return methods.unwrap
|
|
31
|
+
|
|
32
|
+
|
|
33
|
+
def unwrap_or(default: T) -> Operation[Option[T], T]:
|
|
34
|
+
return partial(methods.unwrap_or, default=default)
|
|
35
|
+
|
|
36
|
+
|
|
37
|
+
def unwrap_or_else(func: Callable[[], T]) -> Operation[Option[T], T]:
|
|
38
|
+
return partial(methods.unwrap_or_else, func=func)
|
|
39
|
+
|
|
40
|
+
|
|
41
|
+
def map_(func: Callable[[T], U]) -> Operation[Option[T], Option[U]]:
|
|
42
|
+
return partial(methods.map_, func=func)
|
|
43
|
+
|
|
44
|
+
|
|
45
|
+
def inspect(func: Callable[[T], None]) -> Operation[Option[T], Option[T]]:
|
|
46
|
+
return partial(methods.inspect, func=func)
|
|
47
|
+
|
|
48
|
+
|
|
49
|
+
def map_or(default: U, func: Callable[[T], U]) -> Operation[Option[T], U]:
|
|
50
|
+
return partial(methods.map_or, default=default, func=func)
|
|
51
|
+
|
|
52
|
+
|
|
53
|
+
def map_or_else(
|
|
54
|
+
default: Callable[[], U], func: Callable[[T], U]
|
|
55
|
+
) -> Operation[Option[T], U]:
|
|
56
|
+
return partial(methods.map_or_else, default=default, func=func)
|
|
57
|
+
|
|
58
|
+
|
|
59
|
+
def and_(other: Option[U]) -> Operation[Option[T], Option[U]]:
|
|
60
|
+
return partial(methods.and_, other=other)
|
|
61
|
+
|
|
62
|
+
|
|
63
|
+
def and_then(func: Callable[[T], Option[U]]) -> Operation[Option[T], Option[U]]:
|
|
64
|
+
return partial(methods.and_then, func=func)
|
|
65
|
+
|
|
66
|
+
|
|
67
|
+
def filter_(predicate: Callable[[T], bool]) -> Operation[Option[T], Option[T]]:
|
|
68
|
+
return partial(methods.filter_, predicate=predicate)
|
|
69
|
+
|
|
70
|
+
|
|
71
|
+
def or_(default: Option[T]) -> Operation[Option[T], Option[T]]:
|
|
72
|
+
return partial(methods.or_, default=default)
|
|
73
|
+
|
|
74
|
+
|
|
75
|
+
def or_else(default: Callable[[], Option[T]]) -> Operation[Option[T], Option[T]]:
|
|
76
|
+
return partial(methods.or_else, default=default)
|
|
77
|
+
|
|
78
|
+
|
|
79
|
+
def xor(other: Option[T]) -> Operation[Option[T], Option[T]]:
|
|
80
|
+
return partial(methods.xor, other=other)
|
|
81
|
+
|
|
82
|
+
|
|
83
|
+
def flatten() -> Operation[Option[Option[T]], Option[T]]:
|
|
84
|
+
return methods.flatten
|
|
85
|
+
|
|
86
|
+
|
|
87
|
+
def zipped(
|
|
88
|
+
func: Callable[[Unpack[Ts]], Option[U]],
|
|
89
|
+
) -> Operation[Option[tuple[Unpack[Ts]]], Option[tuple[Unpack[Ts], U]]]:
|
|
90
|
+
return partial(methods.zipped, func=func)
|
|
@@ -0,0 +1,111 @@
|
|
|
1
|
+
from __future__ import annotations
|
|
2
|
+
|
|
3
|
+
import functools
|
|
4
|
+
from typing import Any, Iterable, TypeVar, overload
|
|
5
|
+
|
|
6
|
+
from stdlibx.option._option import Nothing, Option, Some, is_some
|
|
7
|
+
|
|
8
|
+
T = TypeVar("T")
|
|
9
|
+
T1 = TypeVar("T1")
|
|
10
|
+
T2 = TypeVar("T2")
|
|
11
|
+
T3 = TypeVar("T3")
|
|
12
|
+
T4 = TypeVar("T4")
|
|
13
|
+
T5 = TypeVar("T5")
|
|
14
|
+
T6 = TypeVar("T6")
|
|
15
|
+
T7 = TypeVar("T7")
|
|
16
|
+
T8 = TypeVar("T8")
|
|
17
|
+
T9 = TypeVar("T9")
|
|
18
|
+
|
|
19
|
+
|
|
20
|
+
@overload
|
|
21
|
+
def collect(a: Option[T1], b: Option[T2], /) -> Option[tuple[T1, T2]]: ...
|
|
22
|
+
|
|
23
|
+
|
|
24
|
+
@overload
|
|
25
|
+
def collect(
|
|
26
|
+
a: Option[T1], b: Option[T2], c: Option[T3], /
|
|
27
|
+
) -> Option[tuple[T1, T2, T3]]: ...
|
|
28
|
+
|
|
29
|
+
|
|
30
|
+
@overload
|
|
31
|
+
def collect(
|
|
32
|
+
a: Option[T1], b: Option[T2], c: Option[T3], d: Option[T4], /
|
|
33
|
+
) -> Option[tuple[T1, T2, T3, T4]]: ...
|
|
34
|
+
|
|
35
|
+
|
|
36
|
+
@overload
|
|
37
|
+
def collect(
|
|
38
|
+
a: Option[T1], b: Option[T2], c: Option[T3], d: Option[T4], e: Option[T5], /
|
|
39
|
+
) -> Option[tuple[T1, T2, T3, T4, T5]]: ...
|
|
40
|
+
|
|
41
|
+
|
|
42
|
+
@overload
|
|
43
|
+
def collect(
|
|
44
|
+
a: Option[T1],
|
|
45
|
+
b: Option[T2],
|
|
46
|
+
c: Option[T3],
|
|
47
|
+
d: Option[T4],
|
|
48
|
+
e: Option[T5],
|
|
49
|
+
f: Option[T6],
|
|
50
|
+
/,
|
|
51
|
+
) -> Option[tuple[T1, T2, T3, T4, T5, T6]]: ...
|
|
52
|
+
|
|
53
|
+
|
|
54
|
+
@overload
|
|
55
|
+
def collect(
|
|
56
|
+
a: Option[T1],
|
|
57
|
+
b: Option[T2],
|
|
58
|
+
c: Option[T3],
|
|
59
|
+
d: Option[T4],
|
|
60
|
+
e: Option[T5],
|
|
61
|
+
f: Option[T6],
|
|
62
|
+
g: Option[T7],
|
|
63
|
+
/,
|
|
64
|
+
) -> Option[tuple[T1, T2, T3, T4, T5, T6, T7]]: ...
|
|
65
|
+
|
|
66
|
+
|
|
67
|
+
@overload
|
|
68
|
+
def collect(
|
|
69
|
+
a: Option[T1],
|
|
70
|
+
b: Option[T2],
|
|
71
|
+
c: Option[T3],
|
|
72
|
+
d: Option[T4],
|
|
73
|
+
e: Option[T5],
|
|
74
|
+
f: Option[T6],
|
|
75
|
+
g: Option[T7],
|
|
76
|
+
h: Option[T8],
|
|
77
|
+
/,
|
|
78
|
+
) -> Option[tuple[T1, T2, T3, T4, T5, T6, T7, T8]]: ...
|
|
79
|
+
|
|
80
|
+
|
|
81
|
+
@overload
|
|
82
|
+
def collect(
|
|
83
|
+
a: Option[T1],
|
|
84
|
+
b: Option[T2],
|
|
85
|
+
c: Option[T3],
|
|
86
|
+
d: Option[T4],
|
|
87
|
+
e: Option[T5],
|
|
88
|
+
f: Option[T6],
|
|
89
|
+
g: Option[T7],
|
|
90
|
+
h: Option[T8],
|
|
91
|
+
i: Option[T9],
|
|
92
|
+
/,
|
|
93
|
+
) -> Option[tuple[T1, T2, T3, T4, T5, T6, T7, T8, T9]]: ...
|
|
94
|
+
|
|
95
|
+
|
|
96
|
+
def collect(initial: Option[Any], *others: Option[Any]) -> Option[tuple[Any, ...]]:
|
|
97
|
+
def _combine(a: Option[tuple[Any, ...]], b: Option[Any]) -> Option[tuple[Any, ...]]:
|
|
98
|
+
if is_some(a) and is_some(b):
|
|
99
|
+
return Some(((*a.value, b.value)))
|
|
100
|
+
return Nothing()
|
|
101
|
+
|
|
102
|
+
return functools.reduce(_combine, [initial, *others], Some(()))
|
|
103
|
+
|
|
104
|
+
|
|
105
|
+
def collect_all(iterable: Iterable[Option[T]]) -> Option[tuple[T, ...]]:
|
|
106
|
+
def _combine(a: Option[tuple[T, ...]], b: Option[T]) -> Option[tuple[T, ...]]:
|
|
107
|
+
if is_some(a) and is_some(b):
|
|
108
|
+
return Some(((*a.value, b.value)))
|
|
109
|
+
return Nothing()
|
|
110
|
+
|
|
111
|
+
return functools.reduce(_combine, iterable, Some(()))
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
from __future__ import annotations
|
|
2
|
+
|
|
3
|
+
from functools import partial
|
|
4
|
+
from typing import TYPE_CHECKING, Callable, TypeVar
|
|
5
|
+
|
|
6
|
+
from stdlibx.option import Option, methods
|
|
7
|
+
|
|
8
|
+
if TYPE_CHECKING:
|
|
9
|
+
from stdlibx.option._option import Operation
|
|
10
|
+
from stdlibx.result import Result
|
|
11
|
+
|
|
12
|
+
T = TypeVar("T")
|
|
13
|
+
U = TypeVar("U")
|
|
14
|
+
E = TypeVar("E")
|
|
15
|
+
|
|
16
|
+
|
|
17
|
+
def ok_or(error: E) -> Operation[Option[T], Result[T, E]]:
|
|
18
|
+
return partial(methods.ok_or, error=error)
|
|
19
|
+
|
|
20
|
+
|
|
21
|
+
def ok_or_else(error: Callable[[], E]) -> Operation[Option[T], Result[T, E]]:
|
|
22
|
+
return partial(methods.ok_or_else, error=error)
|
|
23
|
+
|
|
24
|
+
|
|
25
|
+
def transpose() -> Operation[Option[Result[U, E]], Result[Option[U], E]]:
|
|
26
|
+
return methods.transpose
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
from stdlibx.option.methods.base import (
|
|
2
|
+
and_,
|
|
3
|
+
and_then,
|
|
4
|
+
expect,
|
|
5
|
+
filter_,
|
|
6
|
+
flatten,
|
|
7
|
+
inspect,
|
|
8
|
+
is_none_or,
|
|
9
|
+
is_some_and,
|
|
10
|
+
map_,
|
|
11
|
+
map_or,
|
|
12
|
+
map_or_else,
|
|
13
|
+
or_,
|
|
14
|
+
or_else,
|
|
15
|
+
unwrap,
|
|
16
|
+
unwrap_or,
|
|
17
|
+
unwrap_or_else,
|
|
18
|
+
xor,
|
|
19
|
+
zipped,
|
|
20
|
+
)
|
|
21
|
+
|
|
22
|
+
__all__ = [
|
|
23
|
+
"and_",
|
|
24
|
+
"and_then",
|
|
25
|
+
"expect",
|
|
26
|
+
"filter_",
|
|
27
|
+
"flatten",
|
|
28
|
+
"inspect",
|
|
29
|
+
"is_none_or",
|
|
30
|
+
"is_some_and",
|
|
31
|
+
"map_",
|
|
32
|
+
"map_or",
|
|
33
|
+
"map_or_else",
|
|
34
|
+
"or_",
|
|
35
|
+
"or_else",
|
|
36
|
+
"unwrap",
|
|
37
|
+
"unwrap_or",
|
|
38
|
+
"unwrap_or_else",
|
|
39
|
+
"xor",
|
|
40
|
+
"zipped",
|
|
41
|
+
]
|
|
42
|
+
|
|
43
|
+
try:
|
|
44
|
+
from stdlibx.option.methods.result import ok_or, ok_or_else, transpose
|
|
45
|
+
|
|
46
|
+
__all__ += ["ok_or", "ok_or_else", "transpose"]
|
|
47
|
+
except ImportError:
|
|
48
|
+
pass
|
|
@@ -0,0 +1,127 @@
|
|
|
1
|
+
from __future__ import annotations
|
|
2
|
+
|
|
3
|
+
from typing import Callable, TypeVar
|
|
4
|
+
|
|
5
|
+
from stdlibx.option import (
|
|
6
|
+
Nothing,
|
|
7
|
+
Option,
|
|
8
|
+
OptionExpectError,
|
|
9
|
+
OptionUnwrapError,
|
|
10
|
+
Some,
|
|
11
|
+
is_none,
|
|
12
|
+
is_some,
|
|
13
|
+
)
|
|
14
|
+
from typing_extensions import TypeVarTuple, Unpack
|
|
15
|
+
|
|
16
|
+
T = TypeVar("T")
|
|
17
|
+
U = TypeVar("U")
|
|
18
|
+
Ts = TypeVarTuple("Ts")
|
|
19
|
+
|
|
20
|
+
|
|
21
|
+
def is_some_and(opt: Option[T], func: Callable[[T], bool]) -> bool:
|
|
22
|
+
return is_some(opt) and func(opt.value)
|
|
23
|
+
|
|
24
|
+
|
|
25
|
+
def is_none_or(opt: Option[T], func: Callable[[T], bool]) -> bool:
|
|
26
|
+
return is_none(opt) or (is_some(opt) and func(opt.value))
|
|
27
|
+
|
|
28
|
+
|
|
29
|
+
def expect(opt: Option[T], msg: str) -> T:
|
|
30
|
+
if is_some(opt):
|
|
31
|
+
return opt.value
|
|
32
|
+
raise OptionExpectError(msg)
|
|
33
|
+
|
|
34
|
+
|
|
35
|
+
def unwrap(opt: Option[T]) -> T:
|
|
36
|
+
if is_some(opt):
|
|
37
|
+
return opt.value
|
|
38
|
+
raise OptionUnwrapError
|
|
39
|
+
|
|
40
|
+
|
|
41
|
+
def unwrap_or(opt: Option[T], default: T) -> T:
|
|
42
|
+
if is_some(opt):
|
|
43
|
+
return opt.value
|
|
44
|
+
return default
|
|
45
|
+
|
|
46
|
+
|
|
47
|
+
def unwrap_or_else(opt: Option[T], func: Callable[[], T]) -> T:
|
|
48
|
+
if is_some(opt):
|
|
49
|
+
return opt.value
|
|
50
|
+
return func()
|
|
51
|
+
|
|
52
|
+
|
|
53
|
+
def map_(opt: Option[T], func: Callable[[T], U]) -> Option[U]:
|
|
54
|
+
if is_some(opt):
|
|
55
|
+
return Some(func(opt.value))
|
|
56
|
+
return Nothing()
|
|
57
|
+
|
|
58
|
+
|
|
59
|
+
def inspect(opt: Option[T], func: Callable[[T], None]) -> Option[T]:
|
|
60
|
+
if is_some(opt):
|
|
61
|
+
func(opt.value)
|
|
62
|
+
return opt
|
|
63
|
+
|
|
64
|
+
|
|
65
|
+
def map_or(opt: Option[T], default: U, func: Callable[[T], U]) -> U:
|
|
66
|
+
if is_some(opt):
|
|
67
|
+
return func(opt.value)
|
|
68
|
+
return default
|
|
69
|
+
|
|
70
|
+
|
|
71
|
+
def map_or_else(opt: Option[T], default: Callable[[], U], func: Callable[[T], U]) -> U:
|
|
72
|
+
if is_some(opt):
|
|
73
|
+
return func(opt.value)
|
|
74
|
+
return default()
|
|
75
|
+
|
|
76
|
+
|
|
77
|
+
def and_(opt: Option[T], other: Option[U]) -> Option[U]:
|
|
78
|
+
if is_some(opt):
|
|
79
|
+
return other
|
|
80
|
+
return Nothing()
|
|
81
|
+
|
|
82
|
+
|
|
83
|
+
def and_then(opt: Option[T], func: Callable[[T], Option[U]]) -> Option[U]:
|
|
84
|
+
if is_some(opt):
|
|
85
|
+
return func(opt.value)
|
|
86
|
+
return Nothing()
|
|
87
|
+
|
|
88
|
+
|
|
89
|
+
def filter_(opt: Option[T], predicate: Callable[[T], bool]) -> Option[T]:
|
|
90
|
+
if is_some(opt) and predicate(opt.value) is True:
|
|
91
|
+
return Some(opt.value)
|
|
92
|
+
return Nothing()
|
|
93
|
+
|
|
94
|
+
|
|
95
|
+
def or_(opt: Option[T], default: Option[T]) -> Option[T]:
|
|
96
|
+
if is_some(opt):
|
|
97
|
+
return Some(opt.value)
|
|
98
|
+
return default
|
|
99
|
+
|
|
100
|
+
|
|
101
|
+
def or_else(opt: Option[T], default: Callable[[], Option[T]]) -> Option[T]:
|
|
102
|
+
if is_some(opt):
|
|
103
|
+
return Some(opt.value)
|
|
104
|
+
return default()
|
|
105
|
+
|
|
106
|
+
|
|
107
|
+
def xor(opt: Option[T], other: Option[T]) -> Option[T]:
|
|
108
|
+
if is_some(opt) and is_none(other):
|
|
109
|
+
return Some(opt.value)
|
|
110
|
+
elif is_some(other) and is_none(opt):
|
|
111
|
+
return Some(other.value)
|
|
112
|
+
else:
|
|
113
|
+
return Nothing()
|
|
114
|
+
|
|
115
|
+
|
|
116
|
+
def flatten(opt: Option[Option[T]]) -> Option[T]:
|
|
117
|
+
if is_some(opt):
|
|
118
|
+
return opt.value
|
|
119
|
+
return Nothing()
|
|
120
|
+
|
|
121
|
+
|
|
122
|
+
def zipped(
|
|
123
|
+
opt: Option[tuple[Unpack[Ts]]], func: Callable[[Unpack[Ts]], Option[U]]
|
|
124
|
+
) -> Option[tuple[Unpack[Ts], U]]:
|
|
125
|
+
if is_some(opt):
|
|
126
|
+
return map_(func(*opt.value), lambda val: (*opt.value, val))
|
|
127
|
+
return Nothing()
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
from __future__ import annotations
|
|
2
|
+
|
|
3
|
+
from typing import Callable, TypeVar
|
|
4
|
+
|
|
5
|
+
from stdlibx.option import Nothing, Option, Some, is_some
|
|
6
|
+
from stdlibx.result import Error, Ok, Result, is_err, is_ok
|
|
7
|
+
|
|
8
|
+
T = TypeVar("T")
|
|
9
|
+
U = TypeVar("U")
|
|
10
|
+
E = TypeVar("E")
|
|
11
|
+
|
|
12
|
+
|
|
13
|
+
def ok_or(opt: Option[T], error: E) -> Result[T, E]:
|
|
14
|
+
if is_some(opt):
|
|
15
|
+
return Ok(opt.value)
|
|
16
|
+
return Error(error)
|
|
17
|
+
|
|
18
|
+
|
|
19
|
+
def ok_or_else(opt: Option[T], error: Callable[[], E]) -> Result[T, E]:
|
|
20
|
+
if is_some(opt):
|
|
21
|
+
return Ok(opt.value)
|
|
22
|
+
return Error(error())
|
|
23
|
+
|
|
24
|
+
|
|
25
|
+
def transpose(opt: Option[Result[U, E]]) -> Result[Option[U], E]:
|
|
26
|
+
if is_some(opt) and is_ok(opt.value):
|
|
27
|
+
return Ok(Some(opt.value.value))
|
|
28
|
+
elif is_some(opt) and is_err(opt.value):
|
|
29
|
+
return Error(opt.value.error)
|
|
30
|
+
else:
|
|
31
|
+
return Ok(Nothing())
|
|
File without changes
|