Skip to content

Commit 046b87c

Browse files
Refactor calculations and add type validation
Refactor function to include type checks and simplify code.
1 parent 48749ce commit 046b87c

1 file changed

Lines changed: 8 additions & 18 deletions

File tree

‎Week04/functions_aleyna_gultekin.py‎

Lines changed: 8 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -18,31 +18,21 @@ def custom_equation(x: int = 0, y: int = 0, /, a: int = 1, b: int = 1, *, c: int
1818
:return: The calculated value
1919
:rtype: float
2020
"""
21-
first_term = x ** a
22-
second_term = y ** b
23-
return float((first_term + second_term) / c)
21+
for name, value in (("x", x), ("y", y), ("a", a), ("b", b), ("c", c)):
22+
if type(value) is not int:
23+
raise TypeError(f"{name} must be an int")
24+
return float((x ** a + y ** b) / c)
2425

2526

26-
def fn_w_counter() -> tuple[int, dict[str, int]]:
27+
def fn_w_counter() -> (int, dict[str, int]):
2728
if not hasattr(fn_w_counter, "call_count"):
2829
fn_w_counter.call_count = 0
2930
fn_w_counter.callers = {}
3031

31-
try:
32-
raise Exception
33-
except Exception as error:
34-
caller = error.__traceback__.tb_frame.f_back.f_globals["__name__"]
35-
3632
fn_w_counter.call_count += 1
37-
if caller in fn_w_counter.callers:
38-
fn_w_counter.callers[caller] += 1
33+
if __name__ in fn_w_counter.callers:
34+
fn_w_counter.callers[__name__] += 1
3935
else:
40-
fn_w_counter.callers[caller] = 1
36+
fn_w_counter.callers[__name__] = 1
4137

4238
return fn_w_counter.call_count, fn_w_counter.callers
43-
44-
45-
if __name__ == "__main__":
46-
for _ in range(10):
47-
fn_w_counter()
48-
print(fn_w_counter())

0 commit comments

Comments
 (0)