infinity-loop 0.2.0__tar.gz → 0.2.1__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.
- {infinity_loop-0.2.0 → infinity_loop-0.2.1}/PKG-INFO +1 -1
- {infinity_loop-0.2.0 → infinity_loop-0.2.1}/infinity/__init__.py +1 -1
- {infinity_loop-0.2.0 → infinity_loop-0.2.1}/infinity/core.py +30 -25
- {infinity_loop-0.2.0 → infinity_loop-0.2.1}/infinity_loop.egg-info/PKG-INFO +1 -1
- {infinity_loop-0.2.0 → infinity_loop-0.2.1}/pyproject.toml +1 -1
- {infinity_loop-0.2.0 → infinity_loop-0.2.1}/README.md +0 -0
- {infinity_loop-0.2.0 → infinity_loop-0.2.1}/infinity_loop.egg-info/SOURCES.txt +0 -0
- {infinity_loop-0.2.0 → infinity_loop-0.2.1}/infinity_loop.egg-info/dependency_links.txt +0 -0
- {infinity_loop-0.2.0 → infinity_loop-0.2.1}/infinity_loop.egg-info/top_level.txt +0 -0
- {infinity_loop-0.2.0 → infinity_loop-0.2.1}/setup.cfg +0 -0
- {infinity_loop-0.2.0 → infinity_loop-0.2.1}/tests/test_infinity.py +0 -0
|
@@ -4,6 +4,7 @@ import inspect
|
|
|
4
4
|
import functools
|
|
5
5
|
from typing import Callable, Any
|
|
6
6
|
|
|
7
|
+
|
|
7
8
|
class Infinity:
|
|
8
9
|
"""
|
|
9
10
|
Универсальный декоратор для бесконечных (и ограниченных) циклов.
|
|
@@ -11,7 +12,7 @@ class Infinity:
|
|
|
11
12
|
"""
|
|
12
13
|
def __init__(
|
|
13
14
|
self,
|
|
14
|
-
func: Callable | None = None,
|
|
15
|
+
func: Callable[..., Any] | None = None,
|
|
15
16
|
*,
|
|
16
17
|
delay: float = 0,
|
|
17
18
|
max_runs: int | None = None,
|
|
@@ -28,67 +29,71 @@ class Infinity:
|
|
|
28
29
|
functools.update_wrapper(self, func)
|
|
29
30
|
|
|
30
31
|
def __call__(self, *args: Any, **kwargs: Any) -> Any:
|
|
31
|
-
#
|
|
32
|
+
# Сценарий 1: декоратор вызван со скобками -> @Infinity(delay=1)
|
|
32
33
|
if self.func is None:
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
)
|
|
34
|
+
target_func, *_ = args # Распаковываем первый элемент кортежа без скобок!
|
|
35
|
+
|
|
36
|
+
@functools.wraps(target_func)
|
|
37
|
+
def decorator_wrapper(*f_args: Any, **f_kwargs: Any) -> Any:
|
|
38
|
+
if inspect.iscoroutinefunction(target_func):
|
|
39
|
+
return self._run_async(target_func, *f_args, **f_kwargs)
|
|
40
|
+
return self._run_sync(target_func, *f_args, **f_kwargs)
|
|
41
41
|
|
|
42
|
-
|
|
42
|
+
return decorator_wrapper
|
|
43
|
+
|
|
44
|
+
# Сценарий 2: декоратор вызван без скобок -> @Infinity
|
|
43
45
|
if inspect.iscoroutinefunction(self.func):
|
|
44
|
-
return self._run_async(*args, **kwargs)
|
|
45
|
-
return self._run_sync(*args, **kwargs)
|
|
46
|
+
return self._run_async(self.func, *args, **kwargs)
|
|
47
|
+
return self._run_sync(self.func, *args, **kwargs)
|
|
46
48
|
|
|
47
|
-
def _log_status(self, count: int, start_time: float) -> None:
|
|
49
|
+
def _log_status(self, func_name: str, count: int, start_time: float) -> None:
|
|
48
50
|
if not self.verbose:
|
|
49
51
|
return
|
|
50
52
|
elapsed = time.perf_counter() - start_time
|
|
51
53
|
limit = f"/{self.max_runs}" if self.max_runs else "/∞"
|
|
52
|
-
print(f"♾️ [{
|
|
54
|
+
print(f"♾️ [{func_name}] Итерация: {count}{limit} | Прошло: {elapsed:.2f}с")
|
|
53
55
|
|
|
54
|
-
def _run_sync(self, *args: Any, **kwargs: Any) ->
|
|
56
|
+
def _run_sync(self, func: Callable[..., Any], *args: Any, **kwargs: Any) -> Any:
|
|
55
57
|
count = 0
|
|
56
58
|
start_time = time.perf_counter()
|
|
57
59
|
try:
|
|
58
60
|
while self.max_runs is None or count < self.max_runs:
|
|
59
61
|
count += 1
|
|
60
|
-
self._log_status(count, start_time)
|
|
62
|
+
self._log_status(func.__name__, count, start_time)
|
|
61
63
|
try:
|
|
62
|
-
if
|
|
64
|
+
if func(*args, **kwargs) is False:
|
|
63
65
|
break
|
|
64
66
|
except Exception as e:
|
|
65
67
|
if not self.ignore_errors:
|
|
66
68
|
raise
|
|
67
|
-
print(f"⚠️ [Infinity | Ошибка в {
|
|
69
|
+
print(f"⚠️ [Infinity | Ошибка в {func.__name__}]: {e}")
|
|
68
70
|
|
|
69
71
|
if self.delay > 0 and (self.max_runs is None or count < self.max_runs):
|
|
70
72
|
time.sleep(self.delay)
|
|
71
73
|
except KeyboardInterrupt:
|
|
72
|
-
print(f"\n🛑 [Infinity] Цикл '{
|
|
74
|
+
print(f"\n🛑 [Infinity] Цикл '{func.__name__}' остановлен (итераций: {count}).")
|
|
75
|
+
return None
|
|
73
76
|
|
|
74
|
-
async def _run_async(self, *args: Any, **kwargs: Any) ->
|
|
77
|
+
async def _run_async(self, func: Callable[..., Any], *args: Any, **kwargs: Any) -> Any:
|
|
75
78
|
count = 0
|
|
76
79
|
start_time = time.perf_counter()
|
|
77
80
|
try:
|
|
78
81
|
while self.max_runs is None or count < self.max_runs:
|
|
79
82
|
count += 1
|
|
80
|
-
self._log_status(count, start_time)
|
|
83
|
+
self._log_status(func.__name__, count, start_time)
|
|
81
84
|
try:
|
|
82
|
-
if await
|
|
85
|
+
if await func(*args, **kwargs) is False:
|
|
83
86
|
break
|
|
84
87
|
except Exception as e:
|
|
85
88
|
if not self.ignore_errors:
|
|
86
89
|
raise
|
|
87
|
-
print(f"⚠️ [Infinity | Ошибка в {
|
|
90
|
+
print(f"⚠️ [Infinity | Ошибка в {func.__name__}]: {e}")
|
|
88
91
|
|
|
89
92
|
if self.delay > 0 and (self.max_runs is None or count < self.max_runs):
|
|
90
93
|
await asyncio.sleep(self.delay)
|
|
91
94
|
except (KeyboardInterrupt, asyncio.CancelledError):
|
|
92
|
-
print(f"\n🛑 [Infinity] Асинхронный цикл '{
|
|
95
|
+
print(f"\n🛑 [Infinity] Асинхронный цикл '{func.__name__}' остановлен (итераций: {count}).")
|
|
96
|
+
return None
|
|
97
|
+
|
|
93
98
|
|
|
94
99
|
infinity = Infinity
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|
|
File without changes
|