Mastering attrs Classes: Building a Product Catalog
The attrs library in Python simplifies the creation of classes by automating the generation of common methods like __init__, __repr__, and __eq__. This challenge will test your ability to define and utilize attrs classes to represent real-world data, specifically for a product catalog.
Problem Description
Your task is to create an attrs class to represent a Product in an e-commerce system. This class should store information about each product and provide essential functionalities.
Requirements:
- Define a
Productclass usingattrs.define. - The
Productclass should have the following attributes:product_id: A unique identifier for the product (string).name: The name of the product (string).price: The price of the product (float, must be non-negative).quantity_in_stock: The current stock level (integer, must be non-negative).is_available: A boolean indicating if the product is currently available for purchase. This should be automatically derived fromquantity_in_stock(True ifquantity_in_stock> 0, False otherwise).
- Ensure that
priceandquantity_in_stockcannot be negative. If an invalid value is provided during initialization, raise aValueError. - Implement a method
calculate_total_value()that returns the total value of the current stock for this product (price * quantity_in_stock). - The
Productclass should automatically generate__init__,__repr__, and__eq__methods.
Examples
Example 1:
Input:
product_id="SKU123"
name="Wireless Mouse"
price=25.99
quantity_in_stock=150
Output:
<Product(product_id='SKU123', name='Wireless Mouse', price=25.99, quantity_in_stock=150, is_available=True)>
Explanation: A Product object is created with the given details. The is_available attribute is correctly set to True because quantity_in_stock is greater than 0. The __repr__ method is used to display the object's state.
Example 2:
Input:
product_id="SKU456"
name="Mechanical Keyboard"
price=75.50
quantity_in_stock=0
Output:
<Product(product_id='SKU456', name='Mechanical Keyboard', price=75.50, quantity_in_stock=0, is_available=False)>
Explanation: A Product object is created. The is_available attribute is False as quantity_in_stock is 0.
Example 3:
Input:
product_id="SKU789"
name="USB-C Cable"
price=10.00
quantity_in_stock=50
Calling:
product_instance.calculate_total_value()
Output:
500.0
Explanation: The calculate_total_value method correctly multiplies the price and quantity_in_stock to return the total value of the stock.
Example 4 (Edge Case - Invalid Input):
Input:
product_id="SKU001"
name="Gaming Chair"
price=-50.00 # Invalid price
quantity_in_stock=10
Expected Behavior:
Raises a ValueError with a message like "Price cannot be negative."
Explanation: Attempting to initialize a Product with a negative price should raise a ValueError.
Constraints
- The
product_idandnamemust be strings. - The
pricemust be a float and cannot be negative. - The
quantity_in_stockmust be an integer and cannot be negative. - The
is_availableattribute must be a boolean. - The solution must use the
attrslibrary.
Notes
- You'll need to install the
attrslibrary:pip install attrs. - Consider using
attrs.fieldto define attributes with specific validation or default values. - For the
is_availableattribute, you can define it as a computed attribute that depends onquantity_in_stock. - The
ValueErrormessages for invalid price and quantity should be informative.