Hone logo
Hone
Problems

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:

  1. Define a Product class using attrs.define.
  2. The Product class 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 from quantity_in_stock (True if quantity_in_stock > 0, False otherwise).
  3. Ensure that price and quantity_in_stock cannot be negative. If an invalid value is provided during initialization, raise a ValueError.
  4. Implement a method calculate_total_value() that returns the total value of the current stock for this product (price * quantity_in_stock).
  5. The Product class 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_id and name must be strings.
  • The price must be a float and cannot be negative.
  • The quantity_in_stock must be an integer and cannot be negative.
  • The is_available attribute must be a boolean.
  • The solution must use the attrs library.

Notes

  • You'll need to install the attrs library: pip install attrs.
  • Consider using attrs.field to define attributes with specific validation or default values.
  • For the is_available attribute, you can define it as a computed attribute that depends on quantity_in_stock.
  • The ValueError messages for invalid price and quantity should be informative.
Loading editor...
python