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


def performance(func):
@wraps(func)
def wrapper(*args, **kwargs):
start = time.perf_counter()
was_tracing = tracemalloc.is_tracing()
if not was_tracing:
tracemalloc.start()
tracemalloc.reset_peak()
start_mem = tracemalloc.get_traced_memory()[0]
try:
return func(*args, **kwargs)
finally:
peak = tracemalloc.get_traced_memory()[1]
if not was_tracing:
tracemalloc.stop()
performance.counter += 1
performance.total_mem += peak - start_mem
performance.total_time += time.perf_counter() - start

return wrapper


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

:param x: base of the first term (positional-only)
:param y: base of the second term (positional-only)
:param a: exponent of x
:param b: exponent of y
:param c: divisor (keyword-only)
:return: the result of (x**a + y**b) / c
"""
for name, value in (("x", x), ("y", y), ("a", a), ("b", b), ("c", c)):
if not isinstance(value, int):
raise TypeError(f"{name} must be an int")
return (x**a + y**b) / c


_counter = 0


def fn_w_counter() -> (int, dict[str, int]):
global _counter
_counter += 1
return _counter, {__name__: _counter}
Loading