Coverage for /pythoncovmergedfiles/medio/medio/usr/local/lib/python3.8/site-packages/backoff/_wait_gen.py: 24%

29 statements  

« prev     ^ index     » next       coverage.py v7.2.7, created at 2023-06-07 06:35 +0000

1# coding:utf-8 

2 

3import itertools 

4from typing import Any, Callable, Generator, Iterable, Optional, Union 

5 

6 

7def expo( 

8 base: float = 2, 

9 factor: float = 1, 

10 max_value: Optional[float] = None 

11) -> Generator[float, Any, None]: 

12 

13 """Generator for exponential decay. 

14 

15 Args: 

16 base: The mathematical base of the exponentiation operation 

17 factor: Factor to multiply the exponentiation by. 

18 max_value: The maximum value to yield. Once the value in the 

19 true exponential sequence exceeds this, the value 

20 of max_value will forever after be yielded. 

21 """ 

22 # Advance past initial .send() call 

23 yield # type: ignore[misc] 

24 n = 0 

25 while True: 

26 a = factor * base ** n 

27 if max_value is None or a < max_value: 

28 yield a 

29 n += 1 

30 else: 

31 yield max_value 

32 

33 

34def fibo(max_value: Optional[int] = None) -> Generator[int, None, None]: 

35 """Generator for fibonaccial decay. 

36 

37 Args: 

38 max_value: The maximum value to yield. Once the value in the 

39 true fibonacci sequence exceeds this, the value 

40 of max_value will forever after be yielded. 

41 """ 

42 # Advance past initial .send() call 

43 yield # type: ignore[misc] 

44 

45 a = 1 

46 b = 1 

47 while True: 

48 if max_value is None or a < max_value: 

49 yield a 

50 a, b = b, a + b 

51 else: 

52 yield max_value 

53 

54 

55def constant( 

56 interval: Union[int, Iterable[float]] = 1 

57) -> Generator[float, None, None]: 

58 """Generator for constant intervals. 

59 

60 Args: 

61 interval: A constant value to yield or an iterable of such values. 

62 """ 

63 # Advance past initial .send() call 

64 yield # type: ignore[misc] 

65 

66 try: 

67 itr = iter(interval) # type: ignore 

68 except TypeError: 

69 itr = itertools.repeat(interval) # type: ignore 

70 

71 for val in itr: 

72 yield val 

73 

74 

75def runtime( 

76 *, 

77 value: Callable[[Any], float] 

78) -> Generator[float, None, None]: 

79 """Generator that is based on parsing the return value or thrown 

80 exception of the decorated method 

81 

82 Args: 

83 value: a callable which takes as input the decorated 

84 function's return value or thrown exception and 

85 determines how long to wait 

86 """ 

87 ret_or_exc = yield # type: ignore[misc] 

88 while True: 

89 ret_or_exc = yield value(ret_or_exc)