docs
evals
scenario-1
scenario-10
scenario-2
scenario-3
scenario-4
scenario-5
scenario-6
scenario-7
scenario-8
scenario-9
A system for managing product inventory that supports serialization and deserialization of complex collection-based data structures.
Create a Product class with the following attributes:
id: integer product identifiername: string product nameprice: float price valuetags: set of string tagsCreate an Inventory class that manages:
The Inventory class should provide methods to:
Inventory object with proper typesfrom typing import List, Dict, Set
from dataclasses import dataclass
@dataclass
class Product:
"""Represents a product with id, name, price, and tags."""
id: int
name: str
price: float
tags: Set[str]
class Inventory:
"""Manages a collection of products organized by categories."""
def __init__(self):
"""Initialize an empty inventory."""
pass
def add_product(self, product: Product, category: str) -> None:
"""Add a product to the inventory under a specific category."""
pass
def to_dict(self) -> dict:
"""Serialize the inventory to a dictionary."""
pass
@staticmethod
def from_dict(data: dict) -> 'Inventory':
"""Deserialize an inventory from a dictionary."""
pass
def get_products(self) -> List[Product]:
"""Return all products in the inventory."""
pass
def get_categories(self) -> Dict[str, List[Product]]:
"""Return the category mappings."""
pass
def get_all_tags(self) -> Set[str]:
"""Return all unique tags across all products."""
pass# Create products
p1 = Product(id=1, name="Laptop", price=999.99, tags={"electronics", "computers"})
p2 = Product(id=2, name="Mouse", price=25.50, tags={"electronics", "accessories"})
# Create inventory
inv = Inventory()
inv.add_product(p1, "Electronics")
inv.add_product(p2, "Electronics")
# Serialize
data = inv.to_dict()
# Deserialize
restored_inv = Inventory.from_dict(data)
# Verify products are restored correctly
products = restored_inv.get_products()
assert len(products) == 2
assert products[0].id == 1
assert products[0].name == "Laptop"
assert products[0].price == 999.99
assert products[0].tags == {"electronics", "computers"}# Create inventory with multiple categories
inv = Inventory()
inv.add_product(Product(1, "Laptop", 999.99, {"electronics"}), "Electronics")
inv.add_product(Product(2, "Desk", 299.99, {"furniture"}), "Furniture")
# Serialize and deserialize
data = inv.to_dict()
restored_inv = Inventory.from_dict(data)
# Verify categories
categories = restored_inv.get_categories()
assert "Electronics" in categories
assert "Furniture" in categories
assert len(categories["Electronics"]) == 1
assert categories["Electronics"][0].name == "Laptop"# Create inventory
inv = Inventory()
inv.add_product(Product(1, "Laptop", 999.99, {"electronics", "computers"}), "Electronics")
inv.add_product(Product(2, "Phone", 599.99, {"electronics", "mobile"}), "Electronics")
# Serialize and deserialize
data = inv.to_dict()
restored_inv = Inventory.from_dict(data)
# Verify all unique tags are present
all_tags = restored_inv.get_all_tags()
assert all_tags == {"electronics", "computers", "mobile"}Provides serialization and deserialization support for Python collections and custom objects.