Skip to content

Commit 0d1d9db

Browse files
authored
Add custom equation and call counter functions
This module defines a custom power function and a counter function. The custom_equation function calculates a specific mathematical equation, while fn_w_counter tracks how many times it has been called and logs the caller.
1 parent ca339b2 commit 0d1d9db

File tree

1 file changed

+86
-0
lines changed

1 file changed

+86
-0
lines changed

Week04/functions_furkan_bulut.py

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
custom_power = lambda x=0, /, e=1: x ** e
2+
3+
def custom_equation(x: int = 0, y: int = 0, /, a: int = 1, b: int = 1, *, c: int = 1) -> float:
4+
"""
5+
Calculate a custom equation based on the parameters provided.
6+
7+
This function computes the result of the equation:
8+
(x^a + y^b) / c, where:
9+
- `x` and `y` are the bases for exponentiation.
10+
- `a` and `b` are the exponents for `x` and `y`, respectively.
11+
- `c` serves as the divisor, which cannot be zero.
12+
13+
:param x:
14+
The base for exponentiation (positional only). Default is 0.
15+
16+
:param y:
17+
The base for exponentiation (positional only). Default is 0.
18+
19+
:param a:
20+
The exponent for `x` (positional or keyword). Default is 1.
21+
This defines the power to which `x` is raised.
22+
23+
:param b:
24+
The exponent for `y` (positional or keyword). Default is 1.
25+
This defines the power to which `y` is raised.
26+
27+
:param c:
28+
A keyword-only parameter representing the divisor. Default is 1.
29+
If set to zero, a division by zero exception is raised.
30+
31+
:raises ValueError:
32+
If `c` is zero, a `ValueError` is raised indicating that division by zero
33+
is not allowed.
34+
35+
:returns:
36+
The result of the equation (x^a + y^b) / c.
37+
This will return a float value representing the computed result.
38+
39+
:example:
40+
41+
#>>> custom_equation(2, 3, a=2, b=2, c=1)
42+
13.0
43+
#>>> custom_equation(2, 3, a=2, b=2, c=0)
44+
Traceback (most recent call last):
45+
...
46+
ValueError: Division by Zero Exception
47+
"""
48+
if c == 0:
49+
raise ValueError("Division by Zero Exception")
50+
51+
return (x ** a + y ** b) / c
52+
53+
def fn_w_counter() -> (int, dict[str, int]):
54+
"""
55+
A function that counts how many times it has been called and tracks callers.
56+
57+
Each time this function is invoked, it increments a call counter and logs
58+
the name of the caller (the module name). This can be useful for debugging
59+
purposes or monitoring how frequently this function is used in different
60+
parts of the application.
61+
62+
:returns:
63+
A tuple containing:
64+
- The total number of times the function has been called across all instances.
65+
- A dictionary mapping caller names (module names) to the number of times
66+
they have invoked this function.
67+
68+
:example:
69+
70+
#>>> fn_w_counter()
71+
(1, {'__main__': 1})
72+
#>>> fn_w_counter()
73+
(2, {'__main__': 2})
74+
"""
75+
if not hasattr(fn_w_counter, 'call_count'):
76+
fn_w_counter.call_count = 0
77+
fn_w_counter.callers = {}
78+
fn_w_counter.call_count += 1
79+
80+
caller_name = __name__
81+
82+
if caller_name in fn_w_counter.callers:
83+
fn_w_counter.callers[caller_name] += 1
84+
else:
85+
fn_w_counter.callers[caller_name] = 1
86+
return fn_w_counter.call_count, fn_w_counter.callers

0 commit comments

Comments
 (0)