CoolFace
Apppublic

Agents-MCP-Hackathon/Python-Code-to-Diagram-Generator-MCP

sourceHugging Facemitupdated 1y agoView on Hugging Face
6likes
complex_class.py74 linesDownload Raw Back to examples
1"""Complex class example with properties, decorators, and advanced features."""2 3from datetime import datetime4from typing import Optional, List5 6 7class Product:8    """Product class with advanced Python features."""9    10    # Class variable11    total_products = 012    13    def __init__(self, name: str, price: float, category: str):14        self._name = name15        self._price = price16        self._category = category17        self._created_at = datetime.now()18        self._discount = 0.019        Product.total_products += 120    21    @property22    def name(self) -> str:23        """Product name property."""24        return self._name25    26    @name.setter27    def name(self, value: str):28        if not value.strip():29            raise ValueError("Product name cannot be empty")30        self._name = value31    32    @property33    def price(self) -> float:34        """Product price with discount applied."""35        return self._price * (1 - self._discount)36    37    @property38    def original_price(self) -> float:39        """Original price before discount."""40        return self._price41    42    @original_price.setter43    def original_price(self, value: float):44        if value < 0:45            raise ValueError("Price cannot be negative")46        self._price = value47    48    def apply_discount(self, percentage: float):49        """Apply discount percentage."""50        if 0 <= percentage <= 100:51            self._discount = percentage / 10052    53    @staticmethod54    def validate_category(category: str) -> bool:55        """Validate if category is allowed."""56        allowed_categories = ["electronics", "clothing", "books", "food"]57        return category.lower() in allowed_categories58    59    @classmethod60    def create_book(cls, title: str, price: float):61        """Factory method to create a book product."""62        return cls(title, price, "books")63    64    @classmethod65    def get_total_products(cls) -> int:66        """Get total number of products created."""67        return cls.total_products68    69    def __str__(self) -> str:70        return f"{self._name} - ${self.price:.2f}"71    72    def __repr__(self) -> str:73        return f"Product(name='{self._name}', price={self._price}, category='{self._category}')"74