1+ from dataclasses import dataclass
2+ from typing import Callable , Any
3+ from time import sleep
4+ from functools import partial
5+ from maz import compose
6+
7+ class retry_until :
8+
9+ """
10+ Calls input function until condition is met
11+ or number of retries equals `retries`.
12+ """
13+
14+ def __init__ (self , function , retries : int , condition : Callable [[Any ], bool ]):
15+ if retries < 1 :
16+ raise ValueError (f"`retries` must be greater or equal to 1, got { retries } " )
17+
18+ self .function = function
19+ self .retries = retries
20+ self .condition = condition
21+
22+ def __call__ (self , * args , ** kwargs ):
23+
24+ for i in range (self .retries ):
25+ result = self .function (* args , ** kwargs )
26+ if self .condition (result ):
27+ return result
28+
29+ return result
30+
31+ class waiting :
32+
33+ """
34+ Returns a new function that when executed will wait `in_seconds` seconds
35+ before executing the original function.
36+ """
37+
38+ def __init__ (self , function , in_seconds : float ):
39+ self .function = function
40+ self .in_seconds = in_seconds
41+
42+ def __call__ (self , * args , ** kwargs ):
43+ sleep (self .in_seconds )
44+ return self .function (* args , ** kwargs )
45+
46+ class named :
47+
48+ """
49+ Returns an object with properties name and function.
50+ """
51+
52+ name : str
53+ function : Callable [[Any ], Any ]
54+
55+ def __call__ (self , * args : Any , ** kwds : Any ) -> Any :
56+ return self .function (args , kwds )
0 commit comments