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

def performance(func):
"""
A decorator that measures and tracks the performance of decorated functions.

Attributes:
counter (int): The number of times the decorated function has been called.
total_time (float): The cumulative execution time (in seconds) of all calls.
total_mem (int): The cumulative memory usage (in bytes) of all calls.

Args:
func (callable): The function to be decorated.

Returns:
callable: A wrapper function that executes the original function while tracking performance metrics
"""

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

start_time = time.perf_counter()
result = func(*args, **kwargs)
end_time = time.perf_counter()

current, peak = tracemalloc.get_traced_memory()
tracemalloc.stop()

performance.counter += 1
performance.total_time += (end_time - start_time)
performance.total_mem += current

return result

return wrapper


performance.counter = 0
performance.total_time = 0.0
performance.total_mem = 0
34 changes: 34 additions & 0 deletions Week04/functions_okay_sezer.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
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:
"""
This function solves the equation (x**a + y**b) / c.
:param x: The value of x in the equation. Default is 0.
:param y: The value of y in the equation. Default is 0.
:param a: The coefficient of x in the equation. Default is 1.
:param b: The coefficient of y in the equation. Default is 1.
:param c: The constant term in the equation. Default is 1.
:return: The result of the equation.
"""
for name, val in [("x", x), ("y", y), ("a", a), ("b", b), ("c", c)]:
if not isinstance(val, int):
raise TypeError(f"{name} must be an integer")
return (x**a + y**b) / c


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

fn_w_counter.total_calls += 1
caller = fn_w_counter.__module__

if caller in fn_w_counter.callers_dict:
fn_w_counter.callers_dict[caller] += 1
else:
fn_w_counter.callers_dict[caller] = 1

return fn_w_counter.total_calls, fn_w_counter.callers_dict

6 changes: 6 additions & 0 deletions Week05/awaitme_okay_sezer.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
def awaitme(func):
"""A decorator that allows the decorated function to be called with 'await' syntax, enabling asynchronous behavior."""
async def wrapper(*args, **kwargs):
return func(*args, **kwargs)

return wrapper
13 changes: 13 additions & 0 deletions Week06/timer_okay_sezer.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import time

class Timer:
def __init__(self):
self.start_time = None
self.end_time = None

def __enter__(self):
self.start_time = time.time()
return self

def __exit__(self, exc_type, exc_val, exc_tb):
self.end_time = time.time()
Loading