-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathexample.py
More file actions
70 lines (58 loc) · 1.64 KB
/
Copy pathexample.py
File metadata and controls
70 lines (58 loc) · 1.64 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
from typing import List, Optional
from dataclasses import dataclass
from abc import ABC, abstractmethod
import asyncio
import logging
# Decorator example
def log_execution(func):
def wrapper(*args, **kwargs):
logging.info(f"Executing {func.__name__}")
result = func(*args, **kwargs)
logging.info(f"Finished {func.__name__}")
return result
return wrapper
# Abstract base class
class Animal(ABC):
def __init__(self, name: str):
self.name = name
@abstractmethod
def make_sound(self) -> str:
pass
# Class implementation
class Dog(Animal):
def __init__(self, name: str, breed: str):
super().__init__(name)
self.breed = breed
def make_sound(self) -> str:
return f"{self.name} says Woof!"
# Dataclass example
@dataclass
class Person:
name: str
age: int
email: Optional[str] = None
# Async function example
async def process_data(data: List[str]) -> List[str]:
processed = []
for item in data:
# Simulate async processing
await asyncio.sleep(0.1)
processed.append(item.upper())
return processed
# Main execution
@log_execution
def main():
# Create instances
dog = Dog("Rex", "German Shepherd")
person = Person("John Doe", 30, "john@example.com")
# List comprehension
numbers = [x * 2 for x in range(5)]
# Dictionary comprehension
square_map = {x: x**2 for x in range(5)}
# Async execution
async def run_async():
data = ["hello", "world", "async"]
result = await process_data(data)
print(f"Processed data: {result}")
# Run async code
asyncio.run(run_async())