Hone logo
Hone
Problems

Building Classes with attrs: A Data Class Challenge

attrs is a powerful Python library that simplifies the creation of classes, particularly those primarily used to hold data. It reduces boilerplate code and provides features like automatic generation of __init__, __repr__, __eq__, and more. This challenge will guide you through creating classes using attrs, focusing on defining attributes, default values, validators, and converters.

Problem Description

Your task is to create a class called Product using the attrs library. The Product class should represent a product with the following attributes:

  • name: A string representing the product's name (required).
  • price: A float representing the product's price (required, must be positive).
  • quantity: An integer representing the quantity in stock (optional, defaults to 0).
  • description: A string representing the product's description (optional, defaults to an empty string).
  • category: A string representing the product's category (optional, defaults to "Uncategorized").

You need to implement the following:

  1. Define the Product class using attrs.
  2. Make name and price required attributes.
  3. Set a default value of 0 for the quantity attribute.
  4. Set a default value of an empty string ("") for the description attribute.
  5. Set a default value of "Uncategorized" for the category attribute.
  6. Implement a validator for the price attribute to ensure it is always positive. Raise a ValueError if the price is not positive.
  7. Implement a converter for the quantity attribute to ensure it is an integer. If a non-integer value is provided, convert it to an integer.

Examples

Example 1:

Input: Product(name="Laptop", price=1200.00)
Output: Product(name='Laptop', price=1200.0, quantity=0, description='', category='Uncategorized')
Explanation: Creates a Product object with only the required name and price. Quantity, description, and category take their default values.

Example 2:

Input: Product(name="Mouse", price=25.50, quantity=100, description="Wireless mouse", category="Electronics")
Output: Product(name='Mouse', price=25.5, quantity=100, description='Wireless mouse', category='Electronics')
Explanation: Creates a Product object with all attributes specified.

Example 3:

Input: Product(name="Keyboard", price=-50.00)
Output: ValueError: Price must be positive.
Explanation: Demonstrates the price validator in action. A negative price raises a ValueError.

Example 4:

Input: Product(name="Monitor", price=300.00, quantity="50")
Output: Product(name='Monitor', price=300.0, quantity=50, description='', category='Uncategorized')
Explanation: Demonstrates the quantity converter. The string "50" is converted to the integer 50.

Constraints

  • The name attribute must be a string.
  • The price attribute must be a float.
  • The quantity attribute must be an integer.
  • The description attribute must be a string.
  • The category attribute must be a string.
  • The price must be a positive number.
  • You must use the attrs library.

Notes

  • Install attrs using pip install attrs.
  • Consider using the @attr.s decorator to simplify class definition.
  • Use attr.validators.instance_of to check the type of an attribute.
  • Use attr.converters.convert to convert an attribute to a specific type.
  • Focus on clean and readable code. Proper error handling is important.
Loading editor...
python