pjdev-decorators 3.4.2__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.
@@ -0,0 +1,4 @@
1
+ # SPDX-FileCopyrightText: 2024-present Chris O'Neill <chris@purplejay.io>
2
+ #
3
+ # SPDX-License-Identifier: MIT
4
+ __version__ = "3.4.2"
@@ -0,0 +1,7 @@
1
+ # SPDX-FileCopyrightText: 2024-present Chris O'Neill <chris@purplejay.io>
2
+ #
3
+ # SPDX-License-Identifier: MIT
4
+
5
+ from loguru import logger
6
+
7
+ logger.disable("pjdev_decorators")
@@ -0,0 +1,80 @@
1
+ import asyncio
2
+ import time
3
+ from functools import wraps
4
+ from typing import Any, Optional
5
+ from loguru import logger
6
+
7
+
8
+ def retry_http(max_attempts: int, delay_seconds: int):
9
+ def decorator(func):
10
+ @wraps(func)
11
+ def wrapper(*args, **kwargs):
12
+ attempts = 0
13
+ while attempts < max_attempts:
14
+ try:
15
+ return func(*args, **kwargs)
16
+ except Exception as e:
17
+ attempts += 1
18
+ logger.warning(f"Attempt {attempts}/{max_attempts} failed: {e}")
19
+ time.sleep(delay_seconds)
20
+ raise Exception(f"Failed after {max_attempts} attempts")
21
+
22
+ return wrapper
23
+
24
+ return decorator
25
+
26
+
27
+ def async_retry_http(
28
+ max_attempts: int, delay_seconds: int, default_value: Optional[Any] = None
29
+ ):
30
+ def decorator(func):
31
+ @wraps(func)
32
+ async def wrapper(*args, **kwargs):
33
+ attempts = 0
34
+ exceptions = []
35
+ while attempts < max_attempts:
36
+ try:
37
+ return await func(*args, **kwargs)
38
+ except Exception as e:
39
+ exceptions.append(e)
40
+ attempts += 1
41
+ logger.warning(f"Attempt {attempts}/{max_attempts} failed: {e}")
42
+ await asyncio.sleep(delay_seconds)
43
+ if default_value is None:
44
+ raise ExceptionGroup(f"Failed after {max_attempts} attempts", exceptions)
45
+ logger.error(f"Failed after {max_attempts} attempts")
46
+ return default_value
47
+
48
+ return wrapper
49
+
50
+ return decorator
51
+
52
+
53
+ def record_time(log: bool = False):
54
+ def decorator(func):
55
+ if asyncio.iscoroutinefunction(func):
56
+ @wraps(func)
57
+ async def wrap_func(*args, **kwargs):
58
+ t1 = time.time()
59
+ result = await func(*args, **kwargs)
60
+ t2 = time.time()
61
+ if log:
62
+ logger.info(f"Function {func.__name__!r} executed in {(t2 - t1):.2f}s")
63
+ return result
64
+
65
+ return wrap_func
66
+
67
+ else:
68
+ @wraps(func)
69
+ def wrap_func(*args, **kwargs):
70
+ t1 = time.time()
71
+ result = func(*args, **kwargs)
72
+ t2 = time.time()
73
+ if log:
74
+ logger.info(f"Function {func.__name__!r} executed in {(t2 - t1):.2f}s")
75
+
76
+ return result
77
+
78
+ return wrap_func
79
+
80
+ return decorator
@@ -0,0 +1,40 @@
1
+ Metadata-Version: 2.3
2
+ Name: pjdev-decorators
3
+ Version: 3.4.2
4
+ Summary: common decorators
5
+ Project-URL: Documentation, https://gitlab.purplejay.net/keystone/python
6
+ Project-URL: Issues, https://gitlab.purplejay.net/keystone/python/issues
7
+ Project-URL: Source, https://gitlab.purplejay.net/keystone/python
8
+ Author-email: Purple Jay LLC <apps+keystone-python-221-issue-@purplejay.io>
9
+ License-Expression: MIT
10
+ License-File: LICENSE.txt
11
+ Classifier: Development Status :: 4 - Beta
12
+ Classifier: Programming Language :: Python
13
+ Classifier: Programming Language :: Python :: 3.12
14
+ Classifier: Programming Language :: Python :: Implementation :: CPython
15
+ Classifier: Programming Language :: Python :: Implementation :: PyPy
16
+ Requires-Python: >=3.12
17
+ Requires-Dist: loguru
18
+ Description-Content-Type: text/markdown
19
+
20
+ # pjdev-decorators
21
+
22
+ [![PyPI - Version](https://img.shields.io/pypi/v/pjdev-decorators.svg)](https://pypi.org/project/pjdev-decorators)
23
+ [![PyPI - Python Version](https://img.shields.io/pypi/pyversions/pjdev-decorators.svg)](https://pypi.org/project/pjdev-decorators)
24
+
25
+ -----
26
+
27
+ **Table of Contents**
28
+
29
+ - [Installation](#installation)
30
+ - [License](#license)
31
+
32
+ ## Installation
33
+
34
+ ```console
35
+ pip install pjdev-decorators
36
+ ```
37
+
38
+ ## License
39
+
40
+ `pjdev-decorators` is distributed under the terms of the [MIT](https://spdx.org/licenses/MIT.html) license.
@@ -0,0 +1,7 @@
1
+ pjdev_decorators/__about__.py,sha256=VBxYJCtTzt3WTyoyH1MXhzlp3RdqBH_1HZ85nWClSn0,129
2
+ pjdev_decorators/__init__.py,sha256=NLKR3tdO6zqG7mZ4MdxL0m63rJUuLMLEKX19CXnuucg,170
3
+ pjdev_decorators/decorators.py,sha256=b0lUj-bFF3wFv15XCYWYVKvvoUvDXvAd7pG0pkFEQcU,2482
4
+ pjdev_decorators-3.4.2.dist-info/METADATA,sha256=aHdNIplrBYwrrtFaVt2-2seP1HbuFbeE8QcmP2R0zBU,1324
5
+ pjdev_decorators-3.4.2.dist-info/WHEEL,sha256=1yFddiXMmvYK7QYTqtRNtX66WJ0Mz8PYEiEUoOUUxRY,87
6
+ pjdev_decorators-3.4.2.dist-info/licenses/LICENSE.txt,sha256=0wYf-NtzsFHBi7LG7ANo5X1v8sZfdv-7ZoslV6Vy3nA,1099
7
+ pjdev_decorators-3.4.2.dist-info/RECORD,,
@@ -0,0 +1,4 @@
1
+ Wheel-Version: 1.0
2
+ Generator: hatchling 1.25.0
3
+ Root-Is-Purelib: true
4
+ Tag: py3-none-any
@@ -0,0 +1,9 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2024-present Chris O'Neill <chris@purplejay.io>
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
6
+
7
+ The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
8
+
9
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.