From 1ce3c3d49f87303332af8d82f1a89ef8a364aa5d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Enes=20Do=C4=9Fan?= Date: Wed, 30 Oct 2024 19:04:35 +0300 Subject: [PATCH 1/5] Create functions_enes_dogan.py --- Week04/functions_enes_dogan.py | 51 ++++++++++++++++++++++++++++++++++ 1 file changed, 51 insertions(+) create mode 100644 Week04/functions_enes_dogan.py 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()) From 8e2c3f2c8d9c64e464285a4acfae2cf21b31628c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Enes=20Do=C4=9Fan?= Date: Sat, 2 Nov 2024 00:53:55 +0300 Subject: [PATCH 2/5] Create decorators_enes_dogan.py --- Week04/decorators_enes_dogan.py | 56 +++++++++++++++++++++++++++++++++ 1 file changed, 56 insertions(+) create mode 100644 Week04/decorators_enes_dogan.py 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 From 4ca5e11b2650482b1865ea3b3fba4dc97d736554 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Enes=20Do=C4=9Fan?= Date: Sat, 2 Nov 2024 11:01:34 +0300 Subject: [PATCH 3/5] Create info_enes_dogan.py --- Week01/info_enes_dogan.py | 2 ++ 1 file changed, 2 insertions(+) create mode 100644 Week01/info_enes_dogan.py 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 From b06d50621bfe6c06371d63db4be6797bd555cd62 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Enes=20Do=C4=9Fan?= Date: Mon, 4 Nov 2024 02:45:59 +0300 Subject: [PATCH 4/5] Create awaitme_enes_dogan.py --- Week05/awaitme_enes_dogan.py | 0 1 file changed, 0 insertions(+), 0 deletions(-) create mode 100644 Week05/awaitme_enes_dogan.py diff --git a/Week05/awaitme_enes_dogan.py b/Week05/awaitme_enes_dogan.py new file mode 100644 index 00000000..e69de29b From ed7953a627cc42d07d31b4507d025523601bd8a8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Enes=20Do=C4=9Fan?= Date: Mon, 4 Nov 2024 02:52:01 +0300 Subject: [PATCH 5/5] Update awaitme_enes_dogan.py --- Week05/awaitme_enes_dogan.py | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/Week05/awaitme_enes_dogan.py b/Week05/awaitme_enes_dogan.py index e69de29b..ed19157a 100644 --- a/Week05/awaitme_enes_dogan.py +++ 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