def test_basic_parsing(): raw = "vladmodels katya y117 47 154" model = parse_vladmodels_spec(raw) assert model == VladModel( brand="vladmodels", name="katya", code="y117", width_mm=47, height_mm=154, ) assert model.area_mm2 == 47 * 154
area = parse_vladmodels_spec("vladmodels katya y117 47 154").area_mm2 print(area) # → 7238
# ------------------------------------------------------------------------- # Example usage (you can delete or comment this block in production code) # ------------------------------------------------------------------------- if __name__ == "__main__": example = "vladmodels katya y117 47 154" model = parse_vladmodels_spec(example)
brand, name, code, width_str, height_str = tokens vladmodels katya y117 47 154
if len(tokens) != 5: raise ValueError( f"Expected 5 whitespace‑separated parts, got len(tokens): tokens" )
def test_invalid_brand(): with pytest.raises(ValueError, match="Brand must be 'vladmodels'"): parse_vladmodels_spec("othermodels katya y117 47 154")
@property def dimensions_str(self) -> str: """Human‑readable dimensions, e.g. “47 mm × 154 mm”.""" return f"self.width_mm mm × self.height_mm} mm" def test_basic_parsing(): raw = "vladmodels katya y117 47
The code is written as a ( parse_vladmodels_spec ) together with a tiny helper class ( VladModel ). You can drop it into any Python project (or copy‑paste it into a Jupyter notebook) and start using it right away. 1️⃣ What the feature does | Step | Action | |------|--------| | 1️⃣ Parse | Splits the input string into its logical parts: brand , model name , model code , width and height . | | 2️⃣ Validate | Checks that the numeric parts are actually numbers and that the brand is the expected one ( vladmodels ). | | 3️⃣ Enrich | Computes a derived metric – area ( width × height ) – which is often useful for sizing, shipping, UI layout, etc. | | 4️⃣ Return | Gives you a clean, typed object ( VladModel ) that you can query like model.brand , model.area , etc. | | 5️⃣ Extend | The implementation is deliberately short but documented and type‑annotated, so you can easily add more derived fields (volume, aspect‑ratio, …) later. | 2️⃣ The code from __future__ import annotations from dataclasses import dataclass from typing import Tuple, List
if brand != "vladmodels": raise ValueError(f"Brand must be 'vladmodels', got 'brand'")
Parameters ---------- spec: str Raw specification text. 1️⃣ What the feature does | Step |
try: width = int(width_str) height = int(height_str) except ValueError as exc: raise ValueError( f"Width and height must be integer numbers; got 'width_str' and 'height_str'" ) from exc
Returns ------- VladModel A frozen dataclass with all fields populated.
@dataclass(frozen=True, slots=True) class VladModel: """A tiny data‑class representing a single VladModels product.""" brand: str # e.g. "vladmodels" name: str # e.g. "katya" code: str # e.g. "y117" width_mm: int # first numeric value (mm) height_mm: int # second numeric value (mm)