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


class performance:
"""Decorator class collecting performance statistics of functions."""
counter = 0
total_time = 0.0
total_mem = 0

def __init__(self, func):
functools.update_wrapper(self, func)
self.func = func

def __call__(self, *args, **kwargs):
was_tracing = tracemalloc.is_tracing()
if not was_tracing:
tracemalloc.start()
mem_before = tracemalloc.get_traced_memory()[0]
t0 = time.perf_counter()
try:
return self.func(*args, **kwargs)
finally:
t1 = time.perf_counter()
mem_after, peak = tracemalloc.get_traced_memory()
if not was_tracing:
tracemalloc.stop()
performance.counter += 1
performance.total_time += t1 - t0
performance.total_mem += max(peak - mem_before, 0)
42 changes: 42 additions & 0 deletions Week04/functions_aleyna_gultekin.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
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:
"""
Raises x to the power a and y to the power b, adds them and divides by c.

:param x: Base of the first term (positional-only)
:type x: int
:param y: Base of the second term (positional-only)
:type y: int
:param a: Power applied to x
:type a: int
:param b: Power applied to y
:type b: int
:param c: Number to divide the sum by (keyword-only)
:type c: int
:return: The calculated value
:rtype: float
"""
first_term = x ** a
second_term = y ** b
return float((first_term + second_term) / c)


def _make_counter():
calls = {"count": 0}
by_caller = {}

def fn_w_counter() -> (int, dict[str, int]):
name = fn_w_counter.__module__
calls["count"] += 1
if name in by_caller:
by_caller[name] += 1
else:
by_caller[name] = 1
return calls["count"], by_caller

return fn_w_counter


fn_w_counter = _make_counter()
Loading