mm-std 0.4.1__py3-none-any.whl → 0.4.3__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/__init__.py CHANGED
@@ -21,8 +21,8 @@ from .dict import replace_empty_dict_values as replace_empty_dict_values
21
21
  from .env import get_dotenv as get_dotenv
22
22
  from .http.http_request import http_request as http_request
23
23
  from .http.http_request_sync import http_request_sync as http_request_sync
24
- from .http.response import HttpError as HttpError
25
- from .http.response import HttpResponse as HttpResponse
24
+ from .http.http_response import HttpError as HttpError
25
+ from .http.http_response import HttpResponse as HttpResponse
26
26
  from .json_ import CustomJSONEncoder as CustomJSONEncoder
27
27
  from .json_ import json_dumps as json_dumps
28
28
  from .log import configure_logging as configure_logging
mm_std/config.py CHANGED
@@ -45,6 +45,6 @@ class BaseConfig(BaseModel):
45
45
  data = tomllib.load(f)
46
46
  return Result.success(cls(**data))
47
47
  except ValidationError as e:
48
- return Result.failure_with_exception(e, error="validator_error", extra={"errors": e.errors()})
48
+ return Result.failure(("validator_error", e), extra={"errors": e.errors()})
49
49
  except Exception as e:
50
- return Result.failure_with_exception(e)
50
+ return Result.failure(e)
@@ -3,7 +3,7 @@ from aiohttp.typedefs import LooseCookies, Query
3
3
  from aiohttp_socks import ProxyConnector
4
4
  from multidict import CIMultiDictProxy
5
5
 
6
- from mm_std.http.response import HttpError, HttpResponse
6
+ from mm_std.http.http_response import HttpError, HttpResponse
7
7
 
8
8
 
9
9
  async def http_request(
@@ -2,7 +2,7 @@ from typing import Any
2
2
 
3
3
  import requests
4
4
 
5
- from mm_std.http.response import HttpError, HttpResponse
5
+ from mm_std.http.http_response import HttpError, HttpResponse
6
6
 
7
7
 
8
8
  def http_request_sync(
@@ -57,10 +57,10 @@ class HttpResponse:
57
57
  def is_error(self) -> bool:
58
58
  return self.error is not None or (self.status_code is not None and self.status_code >= 400)
59
59
 
60
- def to_result_err[T](self, error: str | None = None) -> Result[T]:
60
+ def to_result_failure[T](self, error: str | Exception | tuple[str, Exception] | None = None) -> Result[T]:
61
61
  return Result.failure(error or self.error or "error", extra=self.to_dict())
62
62
 
63
- def to_result_ok[T](self, result: T) -> Result[T]:
63
+ def to_result_success[T](self, result: T) -> Result[T]:
64
64
  return Result.success(result, extra=self.to_dict())
65
65
 
66
66
  def to_dict(self) -> dict[str, Any]:
mm_std/result.py CHANGED
@@ -79,6 +79,14 @@ class Result[T]:
79
79
  return self.exception
80
80
  raise RuntimeError("No exception provided")
81
81
 
82
+ def ok_or_error(self) -> T | str:
83
+ """
84
+ Returns the success value if available, otherwise returns the error message.
85
+ """
86
+ if self.is_ok():
87
+ return self.unwrap()
88
+ return self.unwrap_error()
89
+
82
90
  def to_dict(self) -> dict[str, object]:
83
91
  """
84
92
  Returns a dictionary representation of the result.
@@ -97,7 +105,7 @@ class Result[T]:
97
105
  new_value = fn(cast(T, self.ok))
98
106
  return Result.success(new_value, extra=self.extra)
99
107
  except Exception as e:
100
- return Result.failure_with_exception(e, error="map_exception", extra=self.extra)
108
+ return Result.failure(("map_exception", e), extra=self.extra)
101
109
  return cast(Result[U], self)
102
110
 
103
111
  async def map_async(self, fn: Callable[[T], Awaitable[U]]) -> Result[U]:
@@ -106,7 +114,7 @@ class Result[T]:
106
114
  new_value = await fn(cast(T, self.ok))
107
115
  return Result.success(new_value, extra=self.extra)
108
116
  except Exception as e:
109
- return Result.failure_with_exception(e, error="map_exception", extra=self.extra)
117
+ return Result.failure(("map_exception", e), extra=self.extra)
110
118
  return cast(Result[U], self)
111
119
 
112
120
  def and_then(self, fn: Callable[[T], Result[U]]) -> Result[U]:
@@ -114,7 +122,7 @@ class Result[T]:
114
122
  try:
115
123
  return fn(cast(T, self.ok))
116
124
  except Exception as e:
117
- return Result.failure_with_exception(e, error="and_then_exception", extra=self.extra)
125
+ return Result.failure(("and_then_exception", e), extra=self.extra)
118
126
  return cast(Result[U], self)
119
127
 
120
128
  async def and_then_async(self, fn: Callable[[T], Awaitable[Result[U]]]) -> Result[U]:
@@ -122,7 +130,7 @@ class Result[T]:
122
130
  try:
123
131
  return await fn(cast(T, self.ok))
124
132
  except Exception as e:
125
- return Result.failure_with_exception(e, error="and_then_exception", extra=self.extra)
133
+ return Result.failure(("and_then_exception", e), extra=self.extra)
126
134
  return cast(Result[U], self)
127
135
 
128
136
  def __repr__(self) -> str:
@@ -165,15 +173,43 @@ class Result[T]:
165
173
 
166
174
  @staticmethod
167
175
  def success(ok: T, extra: Extra = None) -> Result[T]:
168
- return Result._create(ok=ok, error=None, exception=None, extra=extra)
176
+ """
177
+ Creates a successful Result instance.
169
178
 
170
- @staticmethod
171
- def failure(error: str, extra: Extra = None) -> Result[T]:
172
- return Result._create(ok=None, error=error, exception=None, extra=extra)
179
+ Args:
180
+ ok: The success value to store in the Result.
181
+ extra: Optional extra metadata to associate with the Result.
182
+
183
+ Returns:
184
+ A Result instance representing success with the provided value.
185
+ """
186
+ return Result._create(ok=ok, error=None, exception=None, extra=extra)
173
187
 
174
188
  @staticmethod
175
- def failure_with_exception(exception: Exception, *, error: str = "exception", extra: Extra = None) -> Result[T]:
176
- return Result._create(ok=None, error=error, exception=exception, extra=extra)
189
+ def failure(error: str | Exception | tuple[str, Exception], extra: Extra = None) -> Result[T]:
190
+ """
191
+ Creates a Result instance representing a failure.
192
+
193
+ Args:
194
+ error: The error information, which can be:
195
+ - A string error message
196
+ - An Exception object
197
+ - A tuple containing (error_message, exception)
198
+ extra: Optional extra metadata to associate with the Result.
199
+
200
+ Returns:
201
+ A Result instance representing failure with the provided error information.
202
+ """
203
+ if isinstance(error, tuple):
204
+ error_, exception = error
205
+ elif isinstance(error, Exception):
206
+ error_ = "exception"
207
+ exception = error
208
+ else:
209
+ error_ = error
210
+ exception = None
211
+
212
+ return Result._create(ok=None, error=error_, exception=exception, extra=extra)
177
213
 
178
214
  @classmethod
179
215
  def __get_pydantic_core_schema__(cls, _source_type: type[Any], _handler: GetCoreSchemaHandler) -> CoreSchema:
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: mm-std
3
- Version: 0.4.1
3
+ Version: 0.4.3
4
4
  Requires-Python: >=3.12
5
5
  Requires-Dist: aiohttp-socks~=0.10.1
6
6
  Requires-Dist: aiohttp~=3.11.16
@@ -1,6 +1,6 @@
1
- mm_std/__init__.py,sha256=Kl-7TT_hd-xmwDJJ0E5AO2Qjpj5H8H7A93DgDwv_r-0,2804
1
+ mm_std/__init__.py,sha256=gSMy_KY9kffPFnrDzdUti_JiQP1hB3ZtF7GxhOCc1Vc,2814
2
2
  mm_std/command.py,sha256=ze286wjUjg0QSTgIu-2WZks53_Vclg69UaYYgPpQvCU,1283
3
- mm_std/config.py,sha256=VCrvTIjq21uDJngPANVClq5CO9xjmeDJCjRlZgn-fBs,1918
3
+ mm_std/config.py,sha256=QtF-SCzaHZf511miTDjmIWR2dFfVPsnw2x6B5rmhMMk,1884
4
4
  mm_std/crypto.py,sha256=jdk0_TCmeU0pPXMyz9xH6kQHSjjZ9GcGClBwQps5vBo,340
5
5
  mm_std/date.py,sha256=976eEkSONuNqHQBgSRu8hrtH23tJqztbmHFHLdbP2TY,1879
6
6
  mm_std/dict.py,sha256=6GkhJPXD0LiJDxPcYe6jPdEDw-MN7P7mKu6U5XxwYDk,675
@@ -12,7 +12,7 @@ mm_std/net.py,sha256=qdRCBIDneip6FaPNe5mx31UtYVmzqam_AoUF7ydEyjA,590
12
12
  mm_std/print_.py,sha256=zB7sVbSSF8RffMxvnOdbKCXjCKtKzKV3R68pBri4NkQ,1638
13
13
  mm_std/py.typed,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
14
14
  mm_std/random_.py,sha256=OuUX4VJeSd13NZBya4qrGpR2TfN7_87tfebOY6DBUnI,1113
15
- mm_std/result.py,sha256=gUmseH3fkapLIcTDkaeilPkubO5nHXWUCc5PmWh3EiY,6874
15
+ mm_std/result.py,sha256=Q4mmX2VP2TNpVFf1J_YWZY-rKwmqJwI1RlMjLsDF7hY,7896
16
16
  mm_std/str.py,sha256=BEjJ1p5O4-uSYK0h-enasSSDdwzkBbiwdQ4_dsrlEE8,3257
17
17
  mm_std/toml.py,sha256=CNznWKR0bpOxS6e3VB5LGS-Oa9lW-wterkcPUFtPcls,610
18
18
  mm_std/types_.py,sha256=9FGd2q47a8M9QQgsWJR1Kq34jLxBAkYSoJuwih4PPqg,257
@@ -25,9 +25,9 @@ mm_std/concurrency/sync_decorators.py,sha256=syCQBOmN7qPO55yzgJB2rbkh10CVww376hm
25
25
  mm_std/concurrency/sync_scheduler.py,sha256=j4tBL_cBI1spr0cZplTA7N2CoYsznuORMeRN8rpR6gY,2407
26
26
  mm_std/concurrency/sync_task_runner.py,sha256=s5JPlLYLGQGHIxy4oDS-PN7O9gcy-yPZFoNm8RQwzcw,1780
27
27
  mm_std/http/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
28
- mm_std/http/http_request.py,sha256=h74_ZjACwdbeINjzhuEwNUpvnaHZvyGl7TExjlBwMGg,3704
29
- mm_std/http/http_request_sync.py,sha256=aawVZfopzMI0alS3lkJQmVOVxH51rmtvsOK_inUlJOs,1537
30
- mm_std/http/response.py,sha256=wRFeBgsmShwsC3goSpKq7VG-Pcr9FGsPwKn4vd_VWqU,3875
31
- mm_std-0.4.1.dist-info/METADATA,sha256=9ZG4BGhh3YvzMNgnOT_hgUUhJfWUJ_iYuIjI1Up_R_A,446
32
- mm_std-0.4.1.dist-info/WHEEL,sha256=qtCwoSJWgHk21S1Kb4ihdzI2rlJ1ZKaIurTj_ngOhyQ,87
33
- mm_std-0.4.1.dist-info/RECORD,,
28
+ mm_std/http/http_request.py,sha256=VnjZKrfaXQfMxrHRUqQ-Sxtr5Qf9FXBiJ-mmJTCzNkY,3709
29
+ mm_std/http/http_request_sync.py,sha256=bqCBilbe4ZJ9vkhuBQeU5UMTJh6BtvtUwjieEodu6rw,1542
30
+ mm_std/http/http_response.py,sha256=gz4kCCL0qr7wvw4NNiK1K2hXlrY0B0Iq4fJuYSrv3bw,3920
31
+ mm_std-0.4.3.dist-info/METADATA,sha256=JSvnB6eXS5YrbuHr15igF-YOvHK7_YD_1bPP4was71k,446
32
+ mm_std-0.4.3.dist-info/WHEEL,sha256=qtCwoSJWgHk21S1Kb4ihdzI2rlJ1ZKaIurTj_ngOhyQ,87
33
+ mm_std-0.4.3.dist-info/RECORD,,
File without changes