diff --git a/Week01/info_enes_dogan.py b/Week01/info_enes_dogan.py new file mode 100644 index 00000000..75321935 --- /dev/null +++ b/Week01/info_enes_dogan.py @@ -0,0 +1,2 @@ +student_id="200315030" +full_name="Enes Doğan" \ No newline at end of file diff --git a/Week04/decorators_enes_dogan.py b/Week04/decorators_enes_dogan.py new file mode 100644 index 00000000..71313e9c --- /dev/null +++ b/Week04/decorators_enes_dogan.py @@ -0,0 +1,56 @@ +import tracemalloc +import time + +def performance(func): + """ + Decorator to measure the performance of a function in terms of + execution time and memory usage. + + Attributes: + counter (int): Counts the number of times the decorated function has been called. + total_time (float): Accumulates the total execution time of the function calls. + total_mem (int): Accumulates the total memory used (in bytes) by the function calls. + + Args: + func (function): The function to be decorated. + + Returns: + function: The wrapped function with performance tracking. + """ + + # Initialize performance attributes + performance.counter = 0 + performance.total_time = 0 + performance.total_mem = 0 + + def _wrapper(*args, **kwargs): + """ + Wrapper function that measures and accumulates the performance + metrics of the decorated function. + + Args: + *args: Variable length argument list for the decorated function. + **kwargs: Arbitrary keyword arguments for the decorated function. + + Returns: + function: The result returned by the decorated function. + """ + + performance.counter += 1 + start_time = time.time() + tracemalloc.start() + snapshot1 = tracemalloc.take_snapshot() + + result = func(*args, **kwargs) + + snapshot2 = tracemalloc.take_snapshot() + tracemalloc.stop() + end_time = time.time() + + # Calculate the memory difference and accumulate it + performance.total_time += end_time - start_time + performance.total_mem += snapshot2.compare_to(snapshot1, "lineno")[0].size_diff + + return result + + return _wrapper \ No newline at end of file diff --git a/Week04/functions_enes_dogan.py b/Week04/functions_enes_dogan.py new file mode 100644 index 00000000..30fa4039 --- /dev/null +++ b/Week04/functions_enes_dogan.py @@ -0,0 +1,51 @@ +import functools + +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: + """ + Calculates (x**a + y**b) / c. + + :param x: Base of the first term (default: 0) + :param y: Base of the second term (default: 0) + :param a: Power of the first term (default: 1) + :param b: Power of the second term (default: 1) + :param c: Constant divisor (default: 1) + :return: Result of (x**a + y**b) / c + """ + return (x ** a + y ** b) / c + +# 3. Function for fn_w_counter +def fn_w_counter() -> (int, dict[str, int]): + """ + Count the number of calls with caller information. + + :return: Tuple containing total number of calls and a dictionary + with the caller name as key and the number of calls as value. + """ + if not hasattr(fn_w_counter, "call_count"): + fn_w_counter.call_count = 0 + fn_w_counter.callers_dict = {} + + fn_w_counter.call_count += 1 + caller = __name__ + + 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.call_count, fn_w_counter.callers_dict + +if __name__ == "__main__": + # Example usage of custom_power + print("custom_power(2, 3):", custom_power(2, 3)) + print("custom_power(5):", custom_power(5)) + + # Example usage of custom_equation + print("custom_equation(2, 3, 2, 1, 1):", custom_equation(2, 3, 2, 1, 1)) + print("custom_equation(2, 3):", custom_equation(2, 3)) + + # Example usage of fn_w_counter + print(fn_w_counter()) + print(fn_w_counter()) diff --git a/Week05/awaitme_enes_dogan.py b/Week05/awaitme_enes_dogan.py new file mode 100644 index 00000000..ed19157a --- /dev/null +++ b/Week05/awaitme_enes_dogan.py @@ -0,0 +1,9 @@ +import asyncio + +def awaitme(function): + async def _awaitme(*args, **kwargs): + result = function(*args, **kwargs) + if asyncio.iscoroutine(result): + return await result + return result + return _awaitme