Files
task-699cc158d6d3a5544a3ed35b/models.py
T
2026-05-26 13:53:45 +00:00

57 lines
2.1 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
"""
Placeholder module for future data models.
The current assignment does not require any persistent or structured data, but the
project layout follows a typical Python package structure. The ``models``
module is kept minimal to satisfy the requirement of having at least four
files and to demonstrate how one might extend the project later.
"""
from __future__ import annotations
# ---------------------------------------------------------------------------
# Example data class not used directly in the streaming demo but shows
# how a model could be defined for future extensions.
# ---------------------------------------------------------------------------
from dataclasses import dataclass, field
from typing import Dict, List
@dataclass
class ProductPrice:
"""Represents a price entry for a product in a specific city."""
product: str
city: str
price: float
currency: str = "rubles"
def __post_init__(self) -> None:
if self.price < 0:
raise ValueError("Price cannot be negative")
# ---------------------------------------------------------------------------
# Repository pattern a very small inmemory store.
# ---------------------------------------------------------------------------
class PriceRepository:
"""Simple repository that holds :class:`ProductPrice` objects."""
def __init__(self) -> None:
self._store: Dict[tuple[str, str], ProductPrice] = {}
def add(self, entry: ProductPrice) -> None:
key = (entry.product.lower(), entry.city.lower())
self._store[key] = entry
def get(self, product: str, city: str) -> ProductPrice | None:
return self._store.get((product.lower(), city.lower()))
# ---------------------------------------------------------------------------
# Example usage not executed in the main script but demonstrates API.
# ---------------------------------------------------------------------------
if __name__ == "__main__": # pragma: no cover
repo = PriceRepository()
repo.add(ProductPrice("milk", "kazan", 89))
print(repo.get("Milk", "KAZAN"))
# End of models.py