Skip to content

Commit d35f671

Browse files
authored
Merge pull request #669 from doganenes/master
Create functions_enes_dogan.py
2 parents a6312ef + 88c6d76 commit d35f671

File tree

4 files changed

+118
-0
lines changed

4 files changed

+118
-0
lines changed

Week01/info_enes_dogan.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
student_id="200315030"
2+
full_name="Enes Doğan"

Week04/decorators_enes_dogan.py

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
import tracemalloc
2+
import time
3+
4+
def performance(func):
5+
"""
6+
Decorator to measure the performance of a function in terms of
7+
execution time and memory usage.
8+
9+
Attributes:
10+
counter (int): Counts the number of times the decorated function has been called.
11+
total_time (float): Accumulates the total execution time of the function calls.
12+
total_mem (int): Accumulates the total memory used (in bytes) by the function calls.
13+
14+
Args:
15+
func (function): The function to be decorated.
16+
17+
Returns:
18+
function: The wrapped function with performance tracking.
19+
"""
20+
21+
# Initialize performance attributes
22+
performance.counter = 0
23+
performance.total_time = 0
24+
performance.total_mem = 0
25+
26+
def _wrapper(*args, **kwargs):
27+
"""
28+
Wrapper function that measures and accumulates the performance
29+
metrics of the decorated function.
30+
31+
Args:
32+
*args: Variable length argument list for the decorated function.
33+
**kwargs: Arbitrary keyword arguments for the decorated function.
34+
35+
Returns:
36+
function: The result returned by the decorated function.
37+
"""
38+
39+
performance.counter += 1
40+
start_time = time.time()
41+
tracemalloc.start()
42+
snapshot1 = tracemalloc.take_snapshot()
43+
44+
result = func(*args, **kwargs)
45+
46+
snapshot2 = tracemalloc.take_snapshot()
47+
tracemalloc.stop()
48+
end_time = time.time()
49+
50+
# Calculate the memory difference and accumulate it
51+
performance.total_time += end_time - start_time
52+
performance.total_mem += snapshot2.compare_to(snapshot1, "lineno")[0].size_diff
53+
54+
return result
55+
56+
return _wrapper

Week04/functions_enes_dogan.py

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
import functools
2+
3+
custom_power = lambda x=0, /, e=1: x ** e
4+
5+
def custom_equation(x: int = 0, y: int = 0, /, a: int = 1, b: int = 1, *, c: int = 1) -> float:
6+
"""
7+
Calculates (x**a + y**b) / c.
8+
9+
:param x: Base of the first term (default: 0)
10+
:param y: Base of the second term (default: 0)
11+
:param a: Power of the first term (default: 1)
12+
:param b: Power of the second term (default: 1)
13+
:param c: Constant divisor (default: 1)
14+
:return: Result of (x**a + y**b) / c
15+
"""
16+
return (x ** a + y ** b) / c
17+
18+
# 3. Function for fn_w_counter
19+
def fn_w_counter() -> (int, dict[str, int]):
20+
"""
21+
Count the number of calls with caller information.
22+
23+
:return: Tuple containing total number of calls and a dictionary
24+
with the caller name as key and the number of calls as value.
25+
"""
26+
if not hasattr(fn_w_counter, "call_count"):
27+
fn_w_counter.call_count = 0
28+
fn_w_counter.callers_dict = {}
29+
30+
fn_w_counter.call_count += 1
31+
caller = __name__
32+
33+
if caller in fn_w_counter.callers_dict:
34+
fn_w_counter.callers_dict[caller] += 1
35+
else:
36+
fn_w_counter.callers_dict[caller] = 1
37+
38+
return fn_w_counter.call_count, fn_w_counter.callers_dict
39+
40+
if __name__ == "__main__":
41+
# Example usage of custom_power
42+
print("custom_power(2, 3):", custom_power(2, 3))
43+
print("custom_power(5):", custom_power(5))
44+
45+
# Example usage of custom_equation
46+
print("custom_equation(2, 3, 2, 1, 1):", custom_equation(2, 3, 2, 1, 1))
47+
print("custom_equation(2, 3):", custom_equation(2, 3))
48+
49+
# Example usage of fn_w_counter
50+
print(fn_w_counter())
51+
print(fn_w_counter())

Week05/awaitme_enes_dogan.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import asyncio
2+
3+
def awaitme(function):
4+
async def _awaitme(*args, **kwargs):
5+
result = function(*args, **kwargs)
6+
if asyncio.iscoroutine(result):
7+
return await result
8+
return result
9+
return _awaitme

0 commit comments

Comments
 (0)