Skip to content
Merged
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_enes_dogan.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
student_id="200315030"
full_name="Enes Doğan"
56 changes: 56 additions & 0 deletions Week04/decorators_enes_dogan.py
Original file line number Diff line number Diff line change
@@ -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
51 changes: 51 additions & 0 deletions Week04/functions_enes_dogan.py
Original file line number Diff line number Diff line change
@@ -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())
9 changes: 9 additions & 0 deletions Week05/awaitme_enes_dogan.py
Original file line number Diff line number Diff line change
@@ -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
Loading