Skip to content

Commit 1c092d6

Browse files
authored
Add functions for custom power and equation calculations
1 parent 71f5b39 commit 1c092d6

File tree

1 file changed

+116
-0
lines changed

1 file changed

+116
-0
lines changed

Week04/functions_Helin_Harman.py

Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
1+
2+
custom_power = lambda x, /, e=1: x**e
3+
4+
result = custom_power(3)
5+
print(result)
6+
7+
def custom_equation(x=0, y=0, /, a=1, *, b=1, c=1) -> float:
8+
"""
9+
This function calculates
10+
(x*a + y*b)/c
11+
"""
12+
return (x*a + y*b)/c
13+
14+
result1 = custom_equation(3, 4, 5, b=4, c=2)
15+
print(result1)
16+
17+
import inspect
18+
19+
global_counter = 0
20+
caller_log = {}
21+
22+
def fn_w_counter (c: int, d: dict) -> tuple[int, dict]:
23+
global global_counter
24+
global caller_log
25+
26+
# Güvenli çağırıcı tespiti
27+
caller_name = 'unknown_run'
28+
try:
29+
# inspect.stack()[1] normalde '__main__' veya 'fonksiyon_adı'nı bulur.
30+
caller_name = inspect.stack()[1].f_globals.get('__name__', 'system_call')
31+
except (IndexError, AttributeError, ValueError):
32+
caller_name = 'ide_console' # Hata durumunda bunu logla
33+
34+
global_counter += 1
35+
caller_log[caller_name] = caller_log.get(caller_name, 0) + 1
36+
37+
return (global_counter, caller_log.copy())
38+
39+
def helper_test(val):
40+
return fn_w_counter(val, {})
41+
42+
if __name__ == '__main__':
43+
print("--- fn_w_counter Gelişmiş Testi ---")
44+
45+
fn_w_counter(1, {})
46+
47+
count2, log2 = fn_w_counter(2, {})
48+
print(f"1. Aşamadan Toplam: {count2}, Log: {log2}")
49+
50+
count3, log3 = helper_test(3)
51+
print(f"2. Aşamadan Toplam: {count3}, Log: {log3}")
52+
53+
54+
import time
55+
import tracemalloc
56+
from functools import wraps
57+
58+
def performance(func):
59+
@wraps(func) #docstringi korur
60+
def _performance(*args, **kwargs):
61+
62+
tracemalloc.start()
63+
t1 = time.perf_counter()
64+
65+
result = func(*args, **kwargs)
66+
67+
current, peak = tracemalloc.get_traced_memory()
68+
tracemalloc.stop()
69+
t2 = time.perf_counter()
70+
71+
elapsed_time = t2 - t1
72+
73+
_performance.counter += 1
74+
_performance.total_time += elapsed_time
75+
_performance.total_mem += peak
76+
77+
return result
78+
79+
_performance.counter = 0
80+
_performance.total_time = 0.0
81+
_performance.total_mem = 0
82+
83+
return _performance
84+
85+
@performance
86+
def calculate_process_time(n):
87+
data = list(range(n))
88+
result = sum(data)
89+
return result
90+
91+
print("Performance dekoratif testi")
92+
93+
resulta =calculate_process_time(500000)
94+
print(resulta)
95+
96+
calculate_process_time(1000000)
97+
98+
print(" ")
99+
100+
print("sonuçlar : ")
101+
102+
print(f"Toplam Çağrı Sayısı (counter): {calculate_process_time.counter}")
103+
print(f"Toplam Süre (total_time): {calculate_process_time.total_time:.4f} saniye")
104+
print(f"Toplam Bellek (total_mem): {calculate_process_time.total_mem} byte")
105+
106+
107+
108+
109+
110+
111+
112+
113+
114+
115+
116+

0 commit comments

Comments
 (0)