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


def performance(func):

@wraps(func)
def wrapper(*args, **kwargs):

tracemalloc.start()
start_time = time.perf_counter()

try:
result = func(*args, **kwargs)
finally:
elapsed_time = time.perf_counter() - start_time
current, peak = tracemalloc.get_traced_memory()
tracemalloc.stop()

performance.counter += 1
performance.total_time += elapsed_time
performance.total_mem += peak

return result

return wrapper


performance.counter = 0
performance.total_time = 0.0
performance.total_mem = 0
39 changes: 39 additions & 0 deletions Week04/functions_yunus_dermencioglu.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import inspect

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 the result of the equation.

:param x: First integer.
:param y: Second integer.
:param a: Exponent of x.
:param b: Exponent of y.
:param c: Divisor.
:return: The calculated result.
:rtype: float
"""
return float((x ** a + y ** b) / c)


def fn_w_counter() -> (int, dict[str, int]):
if not hasattr(fn_w_counter, "counts"):
fn_w_counter.counts = {}

caller_name = fn_w_counter.__module__

counts = fn_w_counter.counts
counts[caller_name] = counts.get(caller_name, 0) + 1

return sum(counts.values()), counts.copy()

Loading