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
33 changes: 33 additions & 0 deletions Week04/decorators_hilmi_aydin.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import time
import tracemalloc


def performance(func):
def wrapper(*args, **kwargs):

tracemalloc.start()
start = time.perf_counter()
start_mem = tracemalloc.get_traced_memory()[0]
tracemalloc.reset_peak()

try:
result = func(*args, **kwargs)

finally:
end = time.perf_counter()
current, peak = tracemalloc.get_traced_memory()

performance.counter += 1
performance.total_time += end - start
performance.total_mem += peak - start_mem

tracemalloc.stop()

return result

return wrapper


performance.counter = 0
performance.total_time = 0
performance.total_mem = 0
25 changes: 25 additions & 0 deletions Week04/functions_hilmi_aydin.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
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:
"""
Calculates the result of (x**a + y**b) / c.

:param x: First base, positional-only (default 0)
:param y: Second base, positional-only (default 0)
:param a: Exponent of x (default 1)
:param b: Exponent of y (default 1)
:param c: Divisor, keyword-only (default 1)
:return: The result as a float
"""
return (x ** a + y ** b) / c


def fn_w_counter() -> (int, dict[str, int]):
"""Counts the total number of calls and the number of calls per caller."""
if not hasattr(fn_w_counter, "total"):
fn_w_counter.total = 0
fn_w_counter.callers = {}
fn_w_counter.total += 1
fn_w_counter.callers[__name__] = fn_w_counter.callers.get(__name__, 0) + 1
return fn_w_counter.total, fn_w_counter.callers
Loading