import asyncio
from typing import List, Dict, Optional
from functools import wraps
def greet(name: str) -> str:
return f"Hello, {name}!"
def add(a: int, b: int) -> int:
return a + b
async def fetch_data(url: str) -> Dict:
await asyncio.sleep(0.1)
return {"url": url, "data": "response"}
def count_up(limit: int):
current = 0
while current < limit:
yield current
current += 1
async def async_generator(n: int):
for i in range(n):
await asyncio.sleep(0.01)
yield i
def outer(x: int):
def inner(y: int):
return x + y
return inner
def cache_decorator(func):
cache = {}
@wraps(func)
def wrapper(*args):
if args not in cache:
cache[args] = func(*args)
return cache[args]
return wrapper
@cache_decorator
def cached(n: int) -> int:
return n * n
def timer_decorator(func):
@wraps(func)
def wrapper(*args, **kwargs):
result = func(*args, **kwargs)
return result
return wrapper
@timer_decorator
def timed(duration: float):
return f"Running for {duration}s"
square = lambda x: x * x
multiply = lambda x, y: x * y
def calculate(values: List[float], operation: str = "sum") -> float:
if operation == "sum":
return sum(values)
elif operation == "avg":
return sum(values) / len(values)
return 0.0
def create_user(
name: str,
email: str,
age: int = 0,
active: bool = True
) -> Dict[str, any]:
return {
"name": name,
"email": email,
"age": age,
"active": active
}
def flexible(*args, **kwargs) -> Dict:
return {
"args": args,
"kwargs": kwargs
}
@cache_decorator
@timer_decorator
def double_decorated(x: int, y: int) -> int:
return x + y
def factorial(n: int) -> int:
if n <= 1:
return 1
return n * factorial(n - 1)
class Helper:
@staticmethod
def static_method() -> str:
return "static"
@classmethod
def class_method(cls) -> str:
return "class"
def instance_method(self) -> str:
return "instance"