diff --git a/Week04/decorators_pelin_akkus,py b/Week04/decorators_pelin_akkus,py new file mode 100644 index 00000000..002e6c7e --- /dev/null +++ b/Week04/decorators_pelin_akkus,py @@ -0,0 +1,31 @@ +from functools import wraps +from time import perf_counter +import tracemalloc + +def performance(func): + """Track how many times a function runs, its time and memory use.""" + + @wraps(func) + def wrapper(*args, **kwargs): + wrapper.counter += 1 + was_tracing = tracemalloc.is_tracing() + if not was_tracing: + tracemalloc.start() + + # Check the memory usage before running the function. + memory_before, _ = tracemalloc.get_traced_memory() + start_time = perf_counter() + + try: + return func(*args, **kwargs) + finally: + wrapper.total_time += perf_counter() - start_time + _, peak_memory = tracemalloc.get_traced_memory() + wrapper.total_mem += max(0, peak_memory - memory_before) + if not was_tracing: + tracemalloc.stop() + + wrapper.counter = 0 + wrapper.total_time = 0.0 + wrapper.total_mem = 0 + return wrapper diff --git a/Week04/functions_pelin_akkus.py b/Week04/functions_pelin_akkus.py new file mode 100644 index 00000000..77f1954f --- /dev/null +++ b/Week04/functions_pelin_akkus.py @@ -0,0 +1,47 @@ +import inspect + +# A short function to calculate x to the power of e. +custom_power = lambda x=0, /, e=1: x ** e + +# Calculate the equation using the given numbers. +def custom_equation( + x: int = 0, + y: int = 0, + /, + a: int = 1, + b: int = 1, + *, + c: int = 1, +) -> float: + """Calculate the sum of two powers divided by a coefficient. + + :param x: First base. + :type x: int + :param y: Second base. + :type y: int + :param a: Exponent of the first base. + :type a: int + :param b: Exponent of the second base. + :type b: int + :param c: Divisor (must not be zero). + :type c: int + :returns: The result of (x**a + y**b) / c. + :rtype: float + :raises ZeroDivisionError: If c is zero. + """ + # Divide the sum of the powers by c. + return (x ** a + y ** b) / c + +def fn_w_counter() -> tuple[int, dict[str, int]]: + """Return total number of calls and counts by caller name.""" + # Find the function that called this one. + caller_name = inspect.currentframe().f_back.f_code.co_name + # Update the total count and the count for this caller. + fn_w_counter.count += 1 + fn_w_counter.callers[caller_name] = fn_w_counter.callers.get(caller_name, 0) + 1 + # Return a copy so the saved counts do not change outside. + return fn_w_counter.count, fn_w_counter.callers.copy() + +# Starting values for the counters. +fn_w_counter.count = 0 +fn_w_counter.callers = {} diff --git a/Week05/emails_pelin_akkus.py b/Week05/emails_pelin_akkus.py new file mode 100644 index 00000000..208a99ea --- /dev/null +++ b/Week05/emails_pelin_akkus.py @@ -0,0 +1,39 @@ +import re + + +class Emails(list): + """A list that stores unique, valid email addresses.""" + + def __init__(self, emails=None): + super().__init__() + self.data = self + + if emails is None: + emails = [] + + for email in emails: + self.append(email) + + @staticmethod + def validate(email): + """Check an email address and raise ValueError if it is invalid.""" + if not isinstance(email, str): + raise ValueError("Email must be a string") + + # Simple format check for username@domain.extension + pattern = r"^[A-Za-z0-9._%+-]+@[A-Za-z0-9-]+(?:\.[A-Za-z0-9-]+)+$" + if not re.fullmatch(pattern, email): + raise ValueError("Invalid email address") + return True + + def append(self, email): + self.validate(email) + # Keep only one copy of each address. + if email not in self: + super().append(email) + + def __str__(self): + return ", ".join(self) + + def __repr__(self): + return f"Emails({list(self)!r})"