Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 29 additions & 0 deletions Week04/decorators_beyza_arman.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
from functools import wraps
from time import perf_counter
import tracemalloc


def performance(function):
"""Measure calls, elapsed time and peak traced memory."""
@wraps(function)
def wrapper(*args, **kwargs):
owns_tracing = not tracemalloc.is_tracing()
if owns_tracing:
tracemalloc.start()
before, _ = tracemalloc.get_traced_memory()
start = perf_counter()
try:
return function(*args, **kwargs)
finally:
performance.counter += 1
performance.total_time += perf_counter() - start
_, peak = tracemalloc.get_traced_memory()
performance.total_mem += max(0, peak - before)
if owns_tracing:
tracemalloc.stop()
return wrapper


performance.counter = 0
performance.total_time = 0.0
performance.total_mem = 0
30 changes: 30 additions & 0 deletions Week04/functions_beyza_arman.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
custom_power = lambda x=0, /, e=1: x ** e


def custom_equation(x: int = 0, y: int = 0, /,
a: int = 1, b: int = 1, *, c: int = 1) -> float:
"""Calculate a sum of powers divided by c.

:param x: First base.
:param y: Second base.
:param a: Exponent of x.
:param b: Exponent of y.
:param c: Divisor.
:return: The value of (x**a + y**b) / c.
:raises TypeError: If an argument is not an integer.
"""
if not all(isinstance(value, int) for value in (x, y, a, b, c)):
raise TypeError("All arguments must be integers")
return (x ** a + y ** b) / c


def fn_w_counter() -> (int, dict[str, int]):
"""Count calls and keep a per-module record."""
fn_w_counter.counter += 1
calls = fn_w_counter.calls
calls[__name__] = calls.get(__name__, 0) + 1
return fn_w_counter.counter, dict(calls)


fn_w_counter.counter = 0
fn_w_counter.calls = {}
Loading