fix(ex4): add wraps to every functions;

mypy compliance
This commit is contained in:
2026-03-29 13:33:56 +02:00
parent 1dfeee81e3
commit 1cf669e6f8
6 changed files with 60 additions and 268 deletions
+41 -31
View File
@@ -5,7 +5,7 @@ from functools import wraps
def spell_timer(func: Callable) -> Callable:
@wraps(func)
def print_time(*args, **kwargs) -> Any:
def print_time(*args: Any, **kwargs: Any) -> Any:
print(f"Casting {func.__name__}")
start = time.time()
res = func(*args, **kwargs)
@@ -16,26 +16,37 @@ def spell_timer(func: Callable) -> Callable:
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"
def check_power(func: Callable) -> str | Any:
@wraps(func)
def fn(*args: Any) -> str | Any:
try:
if args[2] < min_power:
return "Insufficient power for this spell"
return func(*args)
except Exception as err:
print(err)
return "invalid input"
return fn
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"
@wraps(func)
def fn(*args: Any) -> Any:
for i in range(max_attempts):
try:
return func(*args)
except Exception:
print(
f"Spell failed, retrying... ({i + 1}/{max_attempts})"
)
continue
return f"Spell casting failed after {max_attempts} attempts"
return fn
return try_spell
@@ -45,34 +56,34 @@ class MageGuild:
def validate_mage_name(name: str) -> bool:
return len(name) >= 3 and all(x.isalpha() or x.isspace() for x in name)
@power_validator(10)
def cast_spell(self, spell_name: str, power: int) -> str:
def cast() -> str:
return f"Successfully cast {spell_name} with {power} power"
validator = power_validator(10)
return validator(power, cast)
return f"Successfully cast {spell_name} with {power} power"
def main() -> None:
import time
@spell_timer
def fireball() -> str:
time.sleep(0.5)
return "Fireball cast!"
def make_exception() -> None:
raise Exception
@retry_spell(3)
def make_exception(exception: Exception) -> None:
raise exception
guild = MageGuild()
print("===spell_timer===\n")
timer = spell_timer(fireball)
print(f"Result: {timer()}")
print(f"Result: {fireball()}")
print("\n===power_validator===\n")
validator = power_validator(10)
print(f"Valid: {validator(15, fireball)}")
print(f"Invalid: {validator(5, fireball)}")
print(f"Valid: {guild.cast_spell('meteorite de caca', 15)}")
print(f"INvalid: {guild.cast_spell('meteorite de caca', 5)}")
print("\n===retry_spell===\n")
retry = retry_spell(3)
print(f"Valid: {retry(fireball)}")
print(f"Invalid: {retry(make_exception)}")
print(f"Invalid: {make_exception(Exception())}")
print("\n======MageGuild======\n")
print("\n===validate_mage_name===\n")
@@ -84,7 +95,6 @@ def main() -> None:
)
print("\n===cast_spell===\n")
guild = MageGuild()
print(f"Valid: {guild.cast_spell('Lightning', 15)}")
print(f"Invalid: {guild.cast_spell('Lightning', 5)}")