haiway 0.19.4__py3-none-any.whl → 0.20.0__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.
- haiway/__init__.py +4 -0
- haiway/context/__init__.py +2 -0
- haiway/context/access.py +88 -8
- haiway/context/disposables.py +63 -0
- haiway/context/identifier.py +81 -27
- haiway/context/observability.py +303 -7
- haiway/context/state.py +126 -0
- haiway/context/tasks.py +66 -0
- haiway/context/types.py +16 -0
- haiway/helpers/__init__.py +2 -0
- haiway/helpers/asynchrony.py +61 -12
- haiway/helpers/caching.py +31 -0
- haiway/helpers/concurrent.py +74 -0
- haiway/helpers/observability.py +94 -11
- haiway/helpers/retries.py +59 -18
- haiway/helpers/throttling.py +42 -15
- haiway/helpers/timeouted.py +25 -10
- haiway/helpers/tracing.py +31 -0
- haiway/opentelemetry/observability.py +346 -29
- haiway/state/attributes.py +104 -0
- haiway/state/path.py +427 -12
- haiway/state/requirement.py +196 -0
- haiway/state/structure.py +359 -1
- haiway/state/validation.py +293 -0
- haiway/types/default.py +56 -0
- haiway/types/frozen.py +18 -0
- haiway/types/missing.py +89 -0
- haiway/utils/collections.py +36 -28
- haiway/utils/env.py +145 -13
- haiway/utils/formatting.py +27 -0
- haiway/utils/freezing.py +21 -1
- haiway/utils/noop.py +34 -2
- haiway/utils/queue.py +68 -1
- haiway/utils/stream.py +83 -0
- {haiway-0.19.4.dist-info → haiway-0.20.0.dist-info}/METADATA +1 -1
- haiway-0.20.0.dist-info/RECORD +46 -0
- haiway-0.19.4.dist-info/RECORD +0 -45
- {haiway-0.19.4.dist-info → haiway-0.20.0.dist-info}/WHEEL +0 -0
- {haiway-0.19.4.dist-info → haiway-0.20.0.dist-info}/licenses/LICENSE +0 -0
haiway/helpers/throttling.py
CHANGED
@@ -43,28 +43,55 @@ def throttle[**Args, Result](
|
|
43
43
|
]
|
44
44
|
| Callable[Args, Coroutine[Any, Any, Result]]
|
45
45
|
):
|
46
|
-
"""
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
46
|
+
"""
|
47
|
+
Rate-limit asynchronous function calls.
|
48
|
+
|
49
|
+
This decorator restricts the frequency of function calls by enforcing a maximum
|
50
|
+
number of executions within a specified time period. When the limit is reached,
|
51
|
+
subsequent calls will wait until they can be executed without exceeding the limit.
|
52
|
+
|
53
|
+
Can be used as a simple decorator (@throttle) or with configuration
|
54
|
+
parameters (@throttle(limit=5, period=60)).
|
51
55
|
|
52
56
|
Parameters
|
53
57
|
----------
|
54
|
-
function: Callable[Args, Coroutine[Any, Any, Result]]
|
55
|
-
function to
|
58
|
+
function: Callable[Args, Coroutine[Any, Any, Result]] | None
|
59
|
+
The async function to throttle. When used as a simple decorator,
|
60
|
+
this parameter is provided automatically.
|
56
61
|
limit: int
|
57
|
-
|
58
|
-
|
59
|
-
period: timedelta | float
|
60
|
-
|
62
|
+
Maximum number of executions allowed within the specified period.
|
63
|
+
Default is 1, meaning only one call is allowed per period.
|
64
|
+
period: timedelta | float
|
65
|
+
Time window in which the limit applies. Can be specified as a timedelta
|
66
|
+
object or as a float (seconds). Default is 1 second.
|
61
67
|
|
62
68
|
Returns
|
63
69
|
-------
|
64
|
-
Callable
|
65
|
-
|
66
|
-
|
67
|
-
|
70
|
+
Callable
|
71
|
+
When used as @throttle: Returns the wrapped function that enforces the rate limit.
|
72
|
+
When used as @throttle(...): Returns a decorator that can be applied to a function.
|
73
|
+
|
74
|
+
Notes
|
75
|
+
-----
|
76
|
+
- Works only with asynchronous functions.
|
77
|
+
- Cannot be used on class or instance methods.
|
78
|
+
- Not thread-safe, should only be used within a single event loop.
|
79
|
+
- The function preserves the original function's signature, docstring, and other attributes.
|
80
|
+
|
81
|
+
Examples
|
82
|
+
--------
|
83
|
+
Basic usage to limit to 1 call per second:
|
84
|
+
|
85
|
+
>>> @throttle
|
86
|
+
... async def api_call(data):
|
87
|
+
... return await external_api.send(data)
|
88
|
+
|
89
|
+
Limit to 5 calls per minute:
|
90
|
+
|
91
|
+
>>> @throttle(limit=5, period=60)
|
92
|
+
... async def api_call(data):
|
93
|
+
... return await external_api.send(data)
|
94
|
+
"""
|
68
95
|
|
69
96
|
def _wrap(
|
70
97
|
function: Callable[Args, Coroutine[Any, Any, Result]],
|
haiway/helpers/timeouted.py
CHANGED
@@ -14,23 +14,38 @@ def timeout[**Args, Result](
|
|
14
14
|
[Callable[Args, Coroutine[Any, Any, Result]]],
|
15
15
|
Callable[Args, Coroutine[Any, Any, Result]],
|
16
16
|
]:
|
17
|
-
"""
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
function
|
22
|
-
|
17
|
+
"""
|
18
|
+
Add a timeout to an asynchronous function.
|
19
|
+
|
20
|
+
This decorator enforces a maximum execution time for the decorated function.
|
21
|
+
If the function does not complete within the specified timeout period, it
|
22
|
+
will be cancelled and a TimeoutError will be raised.
|
23
23
|
|
24
24
|
Parameters
|
25
25
|
----------
|
26
26
|
timeout: float
|
27
|
-
|
27
|
+
Maximum execution time in seconds allowed for the function
|
28
28
|
|
29
29
|
Returns
|
30
30
|
-------
|
31
|
-
Callable[[Callable[
|
32
|
-
function
|
33
|
-
|
31
|
+
Callable[[Callable[Args, Coroutine[Any, Any, Result]]], Callable[Args, Coroutine[Any, Any, Result]]]
|
32
|
+
A decorator that can be applied to an async function to add timeout behavior
|
33
|
+
|
34
|
+
Notes
|
35
|
+
-----
|
36
|
+
- Works only with asynchronous functions.
|
37
|
+
- The wrapped function will be properly cancelled when the timeout occurs.
|
38
|
+
- Not thread-safe, should only be used within a single event loop.
|
39
|
+
- The original function should handle cancellation properly to ensure
|
40
|
+
resources are released when timeout occurs.
|
41
|
+
|
42
|
+
Examples
|
43
|
+
--------
|
44
|
+
>>> @timeout(5.0)
|
45
|
+
... async def fetch_data(url):
|
46
|
+
... # Will raise TimeoutError if it takes more than 5 seconds
|
47
|
+
... return await http_client.get(url)
|
48
|
+
""" # noqa: E501
|
34
49
|
|
35
50
|
def _wrap(
|
36
51
|
function: Callable[Args, Coroutine[Any, Any, Result]],
|
haiway/helpers/tracing.py
CHANGED
@@ -33,6 +33,37 @@ def traced[**Args, Result](
|
|
33
33
|
level: ObservabilityLevel = ObservabilityLevel.DEBUG,
|
34
34
|
label: str | None = None,
|
35
35
|
) -> Callable[[Callable[Args, Result]], Callable[Args, Result]] | Callable[Args, Result]:
|
36
|
+
"""
|
37
|
+
Decorator that adds tracing to functions, recording inputs, outputs, and exceptions.
|
38
|
+
|
39
|
+
Automatically records function arguments, return values, and any exceptions
|
40
|
+
within the current observability context. The recorded data can be used for
|
41
|
+
debugging, performance analysis, and understanding program execution flow.
|
42
|
+
|
43
|
+
In non-debug builds (when __debug__ is False), this decorator has no effect
|
44
|
+
and returns the original function to avoid performance impact in production.
|
45
|
+
|
46
|
+
Parameters
|
47
|
+
----------
|
48
|
+
function: Callable[Args, Result] | None
|
49
|
+
The function to be traced
|
50
|
+
level: ObservabilityLevel
|
51
|
+
The observability level at which to record trace information (default: DEBUG)
|
52
|
+
label: str | None
|
53
|
+
Custom label for the trace; defaults to the function name if not provided
|
54
|
+
|
55
|
+
Returns
|
56
|
+
-------
|
57
|
+
Callable
|
58
|
+
A decorated function that performs the same operation as the original
|
59
|
+
but with added tracing
|
60
|
+
|
61
|
+
Notes
|
62
|
+
-----
|
63
|
+
Works with both synchronous and asynchronous functions. For asynchronous
|
64
|
+
functions, properly awaits the result before recording it.
|
65
|
+
"""
|
66
|
+
|
36
67
|
def wrap(
|
37
68
|
wrapped: Callable[Args, Result],
|
38
69
|
) -> Callable[Args, Result]:
|