mm-std 0.1.3__py3-none-any.whl → 0.1.4__py3-none-any.whl
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.
mm_std/result.py
CHANGED
@@ -4,6 +4,8 @@ import time
|
|
4
4
|
from collections.abc import Callable
|
5
5
|
from typing import Any, Generic, Literal, NoReturn, TypeAlias, TypeVar
|
6
6
|
|
7
|
+
from pydantic_core import core_schema
|
8
|
+
|
7
9
|
T = TypeVar("T", covariant=True) # Success type
|
8
10
|
U = TypeVar("U")
|
9
11
|
F = TypeVar("F")
|
@@ -13,24 +15,24 @@ TBE = TypeVar("TBE", bound=BaseException)
|
|
13
15
|
class Ok(Generic[T]):
|
14
16
|
__match_args__ = ("ok",)
|
15
17
|
|
16
|
-
def __init__(self,
|
17
|
-
self.
|
18
|
+
def __init__(self, ok: T, data: Any = None) -> None:
|
19
|
+
self.ok = ok
|
18
20
|
self.data = data
|
19
21
|
|
20
22
|
def __repr__(self) -> str:
|
21
23
|
if self.data is None:
|
22
|
-
return f"Ok({self.
|
24
|
+
return f"Ok({self.ok!r})"
|
23
25
|
else:
|
24
|
-
return f"Ok({self.
|
26
|
+
return f"Ok({self.ok!r}, data={self.data!r})"
|
25
27
|
|
26
28
|
def __eq__(self, other: Any) -> bool:
|
27
|
-
return isinstance(other, Ok) and self.
|
29
|
+
return isinstance(other, Ok) and self.ok == other.ok and self.data == other.data
|
28
30
|
|
29
31
|
def __ne__(self, other: Any) -> bool:
|
30
32
|
return not (self == other)
|
31
33
|
|
32
34
|
def __hash__(self) -> int:
|
33
|
-
return hash((True, self.
|
35
|
+
return hash((True, self.ok, self.data))
|
34
36
|
|
35
37
|
def is_ok(self) -> Literal[True]:
|
36
38
|
return True
|
@@ -38,47 +40,43 @@ class Ok(Generic[T]):
|
|
38
40
|
def is_err(self) -> Literal[False]:
|
39
41
|
return False
|
40
42
|
|
41
|
-
@property
|
42
|
-
def ok(self) -> T:
|
43
|
-
return self._value
|
44
|
-
|
45
43
|
@property
|
46
44
|
def err(self) -> None:
|
47
45
|
return None
|
48
46
|
|
49
47
|
def expect(self, _message: str) -> T:
|
50
|
-
return self.
|
48
|
+
return self.ok
|
51
49
|
|
52
50
|
def expect_err(self, message: str) -> NoReturn:
|
53
51
|
raise UnwrapError(self, message)
|
54
52
|
|
55
53
|
def unwrap(self) -> T:
|
56
|
-
return self.
|
54
|
+
return self.ok
|
57
55
|
|
58
56
|
def unwrap_err(self) -> NoReturn:
|
59
57
|
raise UnwrapError(self, "Called `Result.unwrap_err()` on an `Ok` value")
|
60
58
|
|
61
59
|
def unwrap_or(self, _default: U) -> T:
|
62
|
-
return self.
|
60
|
+
return self.ok
|
63
61
|
|
64
62
|
def unwrap_or_else(self, op: object) -> T:
|
65
|
-
return self.
|
63
|
+
return self.ok
|
66
64
|
|
67
65
|
def unwrap_or_raise(self, e: object) -> T:
|
68
|
-
return self.
|
66
|
+
return self.ok
|
69
67
|
|
70
68
|
def map(self, op: Callable[[T], U]) -> Ok[U]:
|
71
|
-
return Ok(op(self.
|
69
|
+
return Ok(op(self.ok), data=self.data)
|
72
70
|
|
73
71
|
def map_or(self, default: object, op: Callable[[T], U]) -> U:
|
74
|
-
return op(self.
|
72
|
+
return op(self.ok)
|
75
73
|
|
76
74
|
def map_or_else(self, err_op: object, ok_op: Callable[[T], U]) -> U:
|
77
75
|
"""
|
78
76
|
The contained result is `Ok`, so return original value mapped to
|
79
77
|
a new value using the passed in `op` function.
|
80
78
|
"""
|
81
|
-
return ok_op(self.
|
79
|
+
return ok_op(self.ok)
|
82
80
|
|
83
81
|
def map_err(self, op: object) -> Ok[T]:
|
84
82
|
"""
|
@@ -92,7 +90,7 @@ class Ok(Generic[T]):
|
|
92
90
|
original value passed in. If return of `op` function is not Result, it will be a Ok value.
|
93
91
|
"""
|
94
92
|
try:
|
95
|
-
res = op(self.
|
93
|
+
res = op(self.ok)
|
96
94
|
if not isinstance(res, Ok | Err):
|
97
95
|
res = Ok(res)
|
98
96
|
except Exception as e:
|
@@ -104,33 +102,45 @@ class Ok(Generic[T]):
|
|
104
102
|
return self
|
105
103
|
|
106
104
|
def ok_or_err(self) -> T | str:
|
107
|
-
return self.
|
105
|
+
return self.ok
|
108
106
|
|
109
107
|
def ok_or_none(self) -> T | None:
|
110
|
-
return self.
|
108
|
+
return self.ok
|
109
|
+
|
110
|
+
@classmethod
|
111
|
+
def __get_pydantic_core_schema__(cls, _source_type: Any, _handler: Any) -> core_schema.CoreSchema:
|
112
|
+
return core_schema.model_schema(
|
113
|
+
cls,
|
114
|
+
core_schema.model_fields_schema(
|
115
|
+
{
|
116
|
+
"ok": core_schema.model_field(core_schema.any_schema()),
|
117
|
+
"data": core_schema.model_field(core_schema.any_schema()),
|
118
|
+
}
|
119
|
+
),
|
120
|
+
)
|
111
121
|
|
112
122
|
|
113
123
|
class Err:
|
114
124
|
__match_args__ = ("err",)
|
115
125
|
|
116
|
-
def __init__(self,
|
117
|
-
self.
|
126
|
+
def __init__(self, err: str | Exception, data: Any = None) -> None:
|
127
|
+
self.err = f"exception: {err}" if isinstance(err, Exception) else err
|
118
128
|
self.data = data
|
119
129
|
|
120
130
|
def __repr__(self) -> str:
|
121
131
|
if self.data is None:
|
122
|
-
return f"Err({self.
|
132
|
+
return f"Err({self.err!r})"
|
123
133
|
else:
|
124
|
-
return f"Err({self.
|
134
|
+
return f"Err({self.err!r}, data={self.data!r})"
|
125
135
|
|
126
136
|
def __eq__(self, other: Any) -> bool:
|
127
|
-
return isinstance(other, Err) and self.
|
137
|
+
return isinstance(other, Err) and self.err == other.err and self.data == other.data
|
128
138
|
|
129
139
|
def __ne__(self, other: Any) -> bool:
|
130
140
|
return not (self == other)
|
131
141
|
|
132
142
|
def __hash__(self) -> int:
|
133
|
-
return hash((False, self.
|
143
|
+
return hash((False, self.err, self.data))
|
134
144
|
|
135
145
|
def is_ok(self) -> Literal[False]:
|
136
146
|
return False
|
@@ -145,30 +155,23 @@ class Err:
|
|
145
155
|
"""
|
146
156
|
return None
|
147
157
|
|
148
|
-
@property
|
149
|
-
def err(self) -> str:
|
150
|
-
"""
|
151
|
-
Return the error.
|
152
|
-
"""
|
153
|
-
return self._value
|
154
|
-
|
155
158
|
def expect(self, message: str) -> NoReturn:
|
156
159
|
"""
|
157
160
|
Raises an `UnwrapError`.
|
158
161
|
"""
|
159
162
|
exc = UnwrapError(
|
160
163
|
self,
|
161
|
-
f"{message}: {self.
|
164
|
+
f"{message}: {self.err!r}",
|
162
165
|
)
|
163
|
-
if isinstance(self.
|
164
|
-
raise exc from self.
|
166
|
+
if isinstance(self.err, BaseException):
|
167
|
+
raise exc from self.err
|
165
168
|
raise exc
|
166
169
|
|
167
170
|
def expect_err(self, _message: str) -> str:
|
168
171
|
"""
|
169
172
|
Return the inner value
|
170
173
|
"""
|
171
|
-
return self.
|
174
|
+
return self.err
|
172
175
|
|
173
176
|
def unwrap(self) -> NoReturn:
|
174
177
|
"""
|
@@ -176,17 +179,17 @@ class Err:
|
|
176
179
|
"""
|
177
180
|
exc = UnwrapError(
|
178
181
|
self,
|
179
|
-
f"Called `Result.unwrap()` on an `Err` value: {self.
|
182
|
+
f"Called `Result.unwrap()` on an `Err` value: {self.err!r}",
|
180
183
|
)
|
181
|
-
if isinstance(self.
|
182
|
-
raise exc from self.
|
184
|
+
if isinstance(self.err, BaseException):
|
185
|
+
raise exc from self.err
|
183
186
|
raise exc
|
184
187
|
|
185
188
|
def unwrap_err(self) -> str:
|
186
189
|
"""
|
187
190
|
Return the inner value
|
188
191
|
"""
|
189
|
-
return self.
|
192
|
+
return self.err
|
190
193
|
|
191
194
|
def unwrap_or(self, default: U) -> U:
|
192
195
|
"""
|
@@ -199,13 +202,13 @@ class Err:
|
|
199
202
|
The contained result is ``Err``, so return the result of applying
|
200
203
|
``op`` to the error value.
|
201
204
|
"""
|
202
|
-
return op(self.
|
205
|
+
return op(self.err)
|
203
206
|
|
204
207
|
def unwrap_or_raise(self, e: type[TBE]) -> NoReturn:
|
205
208
|
"""
|
206
209
|
The contained result is ``Err``, so raise the exception with the value.
|
207
210
|
"""
|
208
|
-
raise e(self.
|
211
|
+
raise e(self.err)
|
209
212
|
|
210
213
|
def map(self, op: object) -> Err:
|
211
214
|
"""
|
@@ -223,7 +226,7 @@ class Err:
|
|
223
226
|
"""
|
224
227
|
Return the result of the default operation
|
225
228
|
"""
|
226
|
-
return err_op(self.
|
229
|
+
return err_op(self.err)
|
227
230
|
|
228
231
|
def and_then(self, op: object) -> Err:
|
229
232
|
"""
|
@@ -232,11 +235,23 @@ class Err:
|
|
232
235
|
return self
|
233
236
|
|
234
237
|
def ok_or_err(self) -> T | str:
|
235
|
-
return self.
|
238
|
+
return self.err
|
236
239
|
|
237
240
|
def ok_or_none(self) -> T | None:
|
238
241
|
return None
|
239
242
|
|
243
|
+
@classmethod
|
244
|
+
def __get_pydantic_core_schema__(cls, _source_type: Any, _handler: Any) -> core_schema.CoreSchema:
|
245
|
+
return core_schema.model_schema(
|
246
|
+
cls,
|
247
|
+
core_schema.model_fields_schema(
|
248
|
+
{
|
249
|
+
"err": core_schema.model_field(core_schema.any_schema()),
|
250
|
+
"data": core_schema.model_field(core_schema.any_schema()),
|
251
|
+
}
|
252
|
+
),
|
253
|
+
)
|
254
|
+
|
240
255
|
|
241
256
|
Result: TypeAlias = Ok[T] | Err
|
242
257
|
|
@@ -13,11 +13,11 @@ mm_std/net.py,sha256=FIH_drMlf_ng-icI2w_mXiVyiLQUvJkskh3rQF8x8pM,4705
|
|
13
13
|
mm_std/print_.py,sha256=mMixwfdrLEYW15ez7_QxXdrV-d38q9XJP8tB8F7P2pI,1553
|
14
14
|
mm_std/py.typed,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
|
15
15
|
mm_std/random_.py,sha256=KZauQzHsJaCVIeyHrJ74hiH34hdFE7pUxuv5i0Hl28g,1175
|
16
|
-
mm_std/result.py,sha256=
|
16
|
+
mm_std/result.py,sha256=mYjQdKsSEMCqRFOsq7c-ZRp2faGnO_fieTzAeUwsa20,7592
|
17
17
|
mm_std/str.py,sha256=nG5XF5870xM2PAvU0LZrJDk-d54LYwRLGnSahIekOVw,3151
|
18
18
|
mm_std/telegram.py,sha256=QrHPnsy0LTeqpd-g3RaHhk7gWIfHZEgnMs-S5DLW-vU,1220
|
19
19
|
mm_std/types.py,sha256=KpFtJ-BTmDfmmFeOSlgq6cMbCfGGOQjh1oWvdcrW-kw,116
|
20
20
|
mm_std/zip.py,sha256=2EXcae4HO5U4kObj2Lj8jl5F2OUpT-WRlJybTyFzt6I,370
|
21
|
-
mm_std-0.1.
|
22
|
-
mm_std-0.1.
|
23
|
-
mm_std-0.1.
|
21
|
+
mm_std-0.1.4.dist-info/METADATA,sha256=Szn_7ov6SgljedK3va8zwTXjMav1i5lLHikp1n_nSHs,335
|
22
|
+
mm_std-0.1.4.dist-info/WHEEL,sha256=1yFddiXMmvYK7QYTqtRNtX66WJ0Mz8PYEiEUoOUUxRY,87
|
23
|
+
mm_std-0.1.4.dist-info/RECORD,,
|
File without changes
|