From 271d310f9b45b8058775b2895ee19b9837c9f86d Mon Sep 17 00:00:00 2001 From: David GAILLETON Date: Fri, 20 Mar 2026 18:13:59 +0100 Subject: [PATCH] WIP: module 10 ex4 --- 10/ex4/decorator_mastery.py | 47 +++++++++++++++++++++++++++++++++++++ 1 file changed, 47 insertions(+) create mode 100644 10/ex4/decorator_mastery.py diff --git a/10/ex4/decorator_mastery.py b/10/ex4/decorator_mastery.py new file mode 100644 index 0000000..d4b6724 --- /dev/null +++ b/10/ex4/decorator_mastery.py @@ -0,0 +1,47 @@ +from typing import Callable, Any +import time +from functools import wraps + + +def spell_timer(func: Callable) -> Callable: + @wraps(func) + def print_time(*args, **kwargs) -> Any: + print(f"Casting {func.__name__}") + start = time.time() + res = func(args, kwargs) + print(f"Spell completed int {time.time() - start} seconds") + return res + + return print_time + + +def power_validator(min_power: int) -> Callable: + def check_power(power: int, func: Callable) -> str | Any: + try: + if power < min_power: + return "Insufficient power for this spell" + return func() + except Exception: + return "invalid input" + + return check_power + + +def retry_spell(max_attempts: int) -> Callable: + def try_spell(func: Callable) -> str | Any: + for i in range(max_attempts): + try: + return func() + except Exception: + print(f"Spell failed, retrying... ({i + 1}/{max_attempts})") + continue + return f"Spell casting failed after {max_attempts} attempts" + + return try_spell + + +class MageGuild: + @staticmethod + def validate_mage_name(name: str) -> bool: ... + + def cast_spell(self, spell_name: str, power: int) -> str: ...