diff --git a/Week04/decorators_oleksii_rak.py b/Week04/decorators_oleksii_rak.py new file mode 100644 index 00000000..32e8d7a4 --- /dev/null +++ b/Week04/decorators_oleksii_rak.py @@ -0,0 +1,28 @@ +import time +import tracemalloc +from functools import wraps + +def performance(func): + @wraps(func) + def wrapper(*args, **kwargs): + performance.counter += 1 + + tracemalloc.start() + start_time = time.perf_counter() + + result = func(*args, **kwargs) + + end_time = time.perf_counter() + _, peak_mem = tracemalloc.get_traced_memory() + tracemalloc.stop() + + performance.total_time += (end_time - start_time) + performance.total_mem += peak_mem + + return result + + return wrapper + +performance.counter = 0 +performance.total_time = 0.0 +performance.total_mem = 0 diff --git a/Week04/functions_oleksii_rak.py b/Week04/functions_oleksii_rak.py new file mode 100644 index 00000000..a9767452 --- /dev/null +++ b/Week04/functions_oleksii_rak.py @@ -0,0 +1,25 @@ +custom_power = lambda x=0, /, e=1: float(x ** e) + +def custom_equation(x: int = 0, y: int = 0, /, a: int = 1, b: int = 1, *, c: int = 1) -> float: + """ + Calculates a custom equation: (x**a + y**b) / c. + + :param x: The first base (positional-only). + :param y: The second base (positional-only). + :param a: The first exponent (positional-or-keyword). + :param b: The second exponent (positional-or-keyword). + :param c: The divisor (keyword-only). + :return: The result of the equation as a float. + """ + return float((x ** a + y ** b) / c) + +def fn_w_counter() -> (int, dict[str, int]): + if not hasattr(fn_w_counter, 'total_calls'): + fn_w_counter.total_calls = 0 + fn_w_counter.callers_dict = {} + + fn_w_counter.total_calls += 1 + + fn_w_counter.callers_dict[__name__] = fn_w_counter.callers_dict.get(__name__, 0) + 1 + + return fn_w_counter.total_calls, fn_w_counter.callers_dict diff --git a/Week05/awaitme_oleksii_rak.py b/Week05/awaitme_oleksii_rak.py new file mode 100644 index 00000000..c6cfcc9a --- /dev/null +++ b/Week05/awaitme_oleksii_rak.py @@ -0,0 +1,11 @@ +import inspect +from functools import wraps + +def awaitme(func): + @wraps(func) + async def wrapper(*args, **kwargs): + if inspect.iscoroutinefunction(func): + return await func(*args, **kwargs) + return func(*args, **kwargs) + + return wrapper diff --git a/Week06/timer_oleksii_rak.py b/Week06/timer_oleksii_rak.py new file mode 100644 index 00000000..7bfa47e6 --- /dev/null +++ b/Week06/timer_oleksii_rak.py @@ -0,0 +1,13 @@ +from time import perf_counter + +class Timer: + def __init__(self): + self.start_time = 0.0 + self.end_time = 0.0 + + def __enter__(self): + self.start_time = perf_counter() + return self + + def __exit__(self, *args): + self.end_time = perf_counter() diff --git a/Week07/threaded_oleksii_rak.py b/Week07/threaded_oleksii_rak.py new file mode 100644 index 00000000..c50c23ae --- /dev/null +++ b/Week07/threaded_oleksii_rak.py @@ -0,0 +1,21 @@ +import threading +from functools import wraps + +def threaded(n: int): + def decorator(func): + @wraps(func) + def wrapper(*args, **kwargs): + threads = [] + + for _ in range(n): + t = threading.Thread(target=func, args=args, kwargs=kwargs) + threads.append(t) + + for t in threads: + t.start() + + for t in threads: + t.join() + + return wrapper + return decorator