Skip to content
Closed
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
2 changes: 2 additions & 0 deletions Week01/info_jalil_guliyev.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
student_id = "220315102"
full_name = "Jalil Guliyev"
34 changes: 34 additions & 0 deletions Week04/decorators_jalil_guliyev.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
"""Week 04: Decorators homework."""

import functools
import time
import tracemalloc


def performance(func):
"""Track cumulative calls, elapsed time and peak memory allocation."""
@functools.wraps(func)
def wrapper(*args, **kwargs):
tracing_started_here = not tracemalloc.is_tracing()
if tracing_started_here:
tracemalloc.start()
before_current, before_peak = tracemalloc.get_traced_memory()
start = time.perf_counter()
try:
return func(*args, **kwargs)
finally:
elapsed = time.perf_counter() - start
current, peak = tracemalloc.get_traced_memory()
allocated = max(0, peak - before_current)
performance.counter += 1
performance.total_time += elapsed
performance.total_mem += allocated
if tracing_started_here:
tracemalloc.stop()

return wrapper


performance.counter = 0
performance.total_time = 0.0
performance.total_mem = 0
34 changes: 34 additions & 0 deletions Week04/functions_jalil_guliyev.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
"""Week 04: Functions homework."""

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 a polynomial-style expression.

:param x: First integer base.
:param y: Second integer base.
:param a: Exponent of x.
:param b: Exponent of y.
:param c: Integer divisor, which must not be zero.
:return: The value of (x ** a + y ** b) / c as a float.
"""
for name, value in (("x", x), ("y", y), ("a", a), ("b", b), ("c", c)):
if type(value) is not int:
raise TypeError(f"{name} must be an integer")
return (x ** a + y ** b) / c


def fn_w_counter() -> (int, dict[str, int]):
"""Return the total calls and per-module call counts."""
fn_w_counter.total += 1
caller = __name__
fn_w_counter.callers[caller] = fn_w_counter.callers.get(caller, 0) + 1
return fn_w_counter.total, fn_w_counter.callers.copy()


fn_w_counter.total = 0
fn_w_counter.callers = {}