-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcomplex_class.py
More file actions
188 lines (152 loc) · 5.32 KB
/
Copy pathcomplex_class.py
File metadata and controls
188 lines (152 loc) · 5.32 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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
"""
Complex OOP Example for Code Translator
Demonstrates handling of advanced Python patterns
This example shows:
- Abstract base classes
- Multiple inheritance (mixins)
- Decorators (property, classmethod, staticmethod)
- Context managers
- Type hints with generics
- Dataclasses
Expected translations should handle paradigm differences appropriately.
"""
from abc import ABC, abstractmethod
from dataclasses import dataclass, field
from typing import TypeVar, Generic, Iterator, Optional
from contextlib import contextmanager
from datetime import datetime
import logging
# Generic type variable
T = TypeVar('T')
# Logging mixin
class LoggingMixin:
"""Mixin that provides logging capabilities."""
@property
def logger(self) -> logging.Logger:
return logging.getLogger(self.__class__.__name__)
def log_info(self, message: str) -> None:
self.logger.info(f"[{self.__class__.__name__}] {message}")
def log_error(self, message: str, exc: Optional[Exception] = None) -> None:
self.logger.error(f"[{self.__class__.__name__}] {message}", exc_info=exc)
# Abstract base class
class Repository(ABC, Generic[T]):
"""Abstract repository pattern for data access."""
@abstractmethod
def get(self, id: int) -> Optional[T]:
"""Retrieve an item by ID."""
pass
@abstractmethod
def save(self, item: T) -> T:
"""Save an item and return it."""
pass
@abstractmethod
def delete(self, id: int) -> bool:
"""Delete an item by ID."""
pass
@abstractmethod
def list_all(self) -> list[T]:
"""List all items."""
pass
# Dataclass for entity
@dataclass
class Product:
"""Product entity with automatic __init__, __repr__, etc."""
id: int
name: str
price: float
category: str
tags: list[str] = field(default_factory=list)
created_at: datetime = field(default_factory=datetime.now)
@property
def display_price(self) -> str:
return f"${self.price:.2f}"
def apply_discount(self, percentage: float) -> 'Product':
"""Return new product with discounted price."""
new_price = self.price * (1 - percentage / 100)
return Product(
id=self.id,
name=self.name,
price=new_price,
category=self.category,
tags=self.tags.copy(),
created_at=self.created_at
)
# Concrete implementation with mixin
class InMemoryProductRepository(Repository[Product], LoggingMixin):
"""In-memory implementation of product repository."""
def __init__(self):
self._store: dict[int, Product] = {}
self._next_id = 1
def get(self, id: int) -> Optional[Product]:
product = self._store.get(id)
if product:
self.log_info(f"Retrieved product {id}: {product.name}")
return product
def save(self, item: Product) -> Product:
if item.id == 0:
item = Product(
id=self._next_id,
name=item.name,
price=item.price,
category=item.category,
tags=item.tags,
created_at=item.created_at
)
self._next_id += 1
self._store[item.id] = item
self.log_info(f"Saved product {item.id}: {item.name}")
return item
def delete(self, id: int) -> bool:
if id in self._store:
del self._store[id]
self.log_info(f"Deleted product {id}")
return True
return False
def list_all(self) -> list[Product]:
return list(self._store.values())
@classmethod
def create_with_sample_data(cls) -> 'InMemoryProductRepository':
"""Factory method to create repository with sample data."""
repo = cls()
samples = [
Product(0, "Laptop", 999.99, "Electronics", ["tech", "computer"]),
Product(0, "Headphones", 149.99, "Electronics", ["audio", "tech"]),
Product(0, "Coffee Mug", 12.99, "Kitchen", ["home"]),
]
for product in samples:
repo.save(product)
return repo
@staticmethod
def validate_price(price: float) -> bool:
"""Static method to validate price."""
return price >= 0
@contextmanager
def transaction(self) -> Iterator[None]:
"""Context manager for transaction-like behavior."""
backup = self._store.copy()
try:
yield
self.log_info("Transaction committed")
except Exception as e:
self._store = backup
self.log_error("Transaction rolled back", e)
raise
# Usage example
def main():
# Create repository with sample data
repo = InMemoryProductRepository.create_with_sample_data()
# List all products
products = repo.list_all()
print(f"Total products: {len(products)}")
for product in products:
print(f" - {product.name}: {product.display_price}")
# Use context manager for safe operations
with repo.transaction():
laptop = repo.get(1)
if laptop:
discounted = laptop.apply_discount(10)
repo.save(discounted)
print(f"Applied 10% discount to {laptop.name}")
if __name__ == "__main__":
logging.basicConfig(level=logging.INFO)
main()