Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import time
- from typing import Callable, TypeVar
- T = TypeVar("T")
- def is_floats_eq(lhs: float, rhs: float, eps: float = 1e-6) -> bool:
- return abs(lhs - rhs) < eps
- def collect_statistic(
- statistics: dict[str, list[float, int]]
- ) -> Callable[[T], T]:
- # ваш код
- def recycle(func):
- global statistics
- start = time.time()
- func()
- end = time.time()
- """
- print("check")
- print("statistics", statistics)
- print("func", func.__name__)
- print(statistics)
- """
- if func.__name__ not in statistics.keys():
- #print("zero")
- statistics[func.__name__] = [0.0, 0]
- #print("check done")
- n = statistics[func.__name__][1]
- statistics[func.__name__][1] += 1
- statistics[func.__name__][0] = (statistics[func.__name__][0] * n + end - start) / (n + 1)
- return func
- return recycle
- statistics: list[str, list[float, int]] = {}
- #сверху или снизу?
- @collect_statistic(statistics)
- def func1() -> None:
- time.sleep(2)
- #print(statistics)
- @collect_statistic(statistics)
- def func2() -> None:
- time.sleep(1)
- #print(statistics)
- #print("func1")
- for i in range(3):
- func1()
- #print()
- #print("func2")
- for i in range(6):
- func2()
- eps = 1e-3
- assert statistics[func1.__name__][1] == 3
- assert statistics[func2.__name__][1] == 6
- assert is_floats_eq(statistics[func1.__name__][0], 2, eps)
- assert is_floats_eq(statistics[func2.__name__][0], 1, eps)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement