from abc import ABC, abstractmethod
from dataclasses import dataclass
from typing import Optional, List
class User:
def __init__(self, name: str, email: str):
self.name = name
self.email = email
def greet(self) -> str:
return f"Hello, {self.name}"
class Product:
category: str = "general"
def __init__(self, name: str, price: float):
self.name = name
self.price = price
class Employee(User):
def __init__(self, name: str, email: str, employee_id: int):
super().__init__(name, email)
self.employee_id = employee_id
def get_id(self) -> int:
return self.employee_id
class Shape(ABC):
@abstractmethod
def area(self) -> float:
pass
@abstractmethod
def perimeter(self) -> float:
pass
class Circle(Shape):
def __init__(self, radius: float):
self.radius = radius
def area(self) -> float:
return 3.14159 * self.radius ** 2
def perimeter(self) -> float:
return 2 * 3.14159 * self.radius
@dataclass
class Config:
host: str
port: int
timeout: float = 30.0
debug: bool = False
class Container:
def __init__(self, value: int):
self.value = value
class Inner:
def __init__(self, data: str):
self.data = data
def singleton(cls):
instances = {}
def wrapper(*args, **kwargs):
if cls not in instances:
instances[cls] = cls(*args, **kwargs)
return instances[cls]
return wrapper
@singleton
class Singleton:
def __init__(self):
self.value = 0
class Mixin:
def log(self, message: str):
print(f"LOG: {message}")
class DatabaseModel(Mixin, User):
def __init__(self, name: str, email: str, db_id: int):
super().__init__(name, email)
self.db_id = db_id
def save(self):
self.log(f"Saving {self.name}")
class EntityMeta(type):
def __new__(mcs, name, bases, attrs):
attrs['entity_type'] = 'auto'
return super().__new__(mcs, name, bases, attrs)
class Entity(metaclass=EntityMeta):
def __init__(self, id: int):
self.id = id