Skip to content

Commit 2f8b189

Browse files
authored
Merge pull request canbula#548 from icelal-kskn/master
Create functions_ikram_celal_keskin.py
2 parents 9f4f530 + 692de0b commit 2f8b189

File tree

3 files changed

+111
-0
lines changed

3 files changed

+111
-0
lines changed
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
import time
2+
import sys
3+
4+
5+
def performance(func):
6+
"""
7+
This function is a decorator that measures the performance of a function.
8+
This decorator function has a nested function called wrapper.
9+
It measures the time ,memory usage, and how many called of the this function.
10+
11+
:param func: function of the decorator
12+
:type func: function
13+
:return: wrapper of the decorator
14+
:rtype: function
15+
"""
16+
if not hasattr(performance, 'counter'):
17+
setattr(performance, 'counter', 0)
18+
19+
if not hasattr(performance, 'total_time'):
20+
setattr(performance, 'total_time', 0.0)
21+
22+
if not hasattr(performance, 'total_mem'):
23+
setattr(performance, 'total_mem', 0)
24+
25+
def wrapper(*args, **kwargs):
26+
"""
27+
This function is a wrapper that measures the performance of the decorated function.
28+
It measures the execution time, memory usage, and the number of calls to the function.
29+
30+
:param *args: Positional arguments to pass to the decorated function.
31+
:param **kwargs: Keyword arguments to pass to the decorated function.
32+
:return: The result of the decorated function.
33+
"""
34+
start_time = time.time()
35+
36+
#source https://www.geeksforgeeks.org/find-out-how-much-memory-is-being-used-by-an-object-in-python/
37+
memory_usage=sys.getsizeof(func(*args, **kwargs))
38+
end_time = time.time()
39+
40+
41+
setattr(performance, 'counter', getattr(performance, 'counter') + 1)
42+
setattr(performance, 'total_time', getattr(performance, 'total_time') + (end_time - start_time))
43+
setattr(performance, 'total_mem', getattr(performance, 'total_mem') + memory_usage)
44+
45+
return func(*args, **kwargs)
46+
47+
return wrapper
48+
49+
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
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+
This function calculates a custom equation that.
8+
9+
:param x: Positional-only parameter, default value is 0.
10+
:param y: Positional-only parameter, default value is 0.
11+
:param a: Exponent for `x`, default value is 1.
12+
:param b: Exponent for `y`, default value is 1.
13+
:param c: Divisor of the equation, default value is 1.
14+
15+
:type x: int
16+
:type y: int
17+
:type a: int
18+
:type b: int
19+
:type c: int
20+
21+
:return: The result of the custom equation ((x^a)+(y^b))/c.
22+
:rtype: float
23+
"""
24+
return (x**a + y**b) / c
25+
26+
27+
28+
def fn_w_counter()->(int, dict[str, int]):
29+
"""
30+
This funtion counts the how many times it is called and from which caller it is called.
31+
32+
:return: Returning integer is the total number of calls and Returning dictionary with string keys and integer values includes the caller ( _ _name_ _ ) as key, the number of call coming from this caller as value.
33+
:rtype: tuple(int, dict[str, int])
34+
"""
35+
if not hasattr(fn_w_counter, "count"):
36+
setattr(fn_w_counter,"count",0)
37+
setattr(fn_w_counter,"caller_dict",{})
38+
39+
40+
fn_w_counter.count += 1
41+
caller= __name__
42+
43+
if caller not in fn_w_counter.caller_dict:
44+
fn_w_counter.caller_dict[caller] = 0
45+
46+
fn_w_counter.caller_dict[caller] += 1
47+
48+
return (int(fn_w_counter.count),dict(fn_w_counter.caller_dict))
49+
50+
51+
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import asyncio
2+
3+
def awaitme(func):
4+
async def wrapper(*args, **kwargs):
5+
result = func(*args, **kwargs)
6+
7+
if asyncio.iscoroutine(result):
8+
return await result
9+
else:
10+
return result
11+
return wrapper

0 commit comments

Comments
 (0)