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


def performance(func):
def wrapper(*args, **kwargs):
started_here = not tracemalloc.is_tracing()
if started_here:
tracemalloc.start()
tracemalloc.reset_peak()
start = time.perf_counter()
result = func(*args, **kwargs)
elapsed = time.perf_counter() - start
_, peak = tracemalloc.get_traced_memory()
if started_here:
tracemalloc.stop()

performance.counter += 1
performance.total_time += elapsed
performance.total_mem += peak
return result

wrapper.__name__ = func.__name__
wrapper.__doc__ = func.__doc__
return wrapper


performance.counter = 0
performance.total_time = 0
performance.total_mem = 0

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

:param x: First base, positional-only
:type x: int
:param y: Second base, positional-only
:type y: int
:param a: Exponent of x
:type a: int
:param b: Exponent of y
:type b: int
:param c: Divisor, keyword-only
:type c: int
:return: Result of (x**a + y**b) / c
:rtype: float
"""
return (x ** a + y ** b) / c


def fn_w_counter() -> (int, dict[str, int]):
caller = __name__
fn_w_counter.total += 1
fn_w_counter.callers[caller] = fn_w_counter.callers.get(caller, 0) + 1
return fn_w_counter.total, fn_w_counter.callers


fn_w_counter.total = 0
fn_w_counter.callers = {}
Loading