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:
- Define the
Productclass usingattrs. - Make
nameandpricerequired attributes. - Set a default value of 0 for the
quantityattribute. - Set a default value of an empty string ("") for the
descriptionattribute. - Set a default value of "Uncategorized" for the
categoryattribute. - Implement a validator for the
priceattribute to ensure it is always positive. Raise aValueErrorif the price is not positive. - Implement a converter for the
quantityattribute 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
nameattribute must be a string. - The
priceattribute must be a float. - The
quantityattribute must be an integer. - The
descriptionattribute must be a string. - The
categoryattribute must be a string. - The price must be a positive number.
- You must use the
attrslibrary.
Notes
- Install
attrsusingpip install attrs. - Consider using the
@attr.sdecorator to simplify class definition. - Use
attr.validators.instance_ofto check the type of an attribute. - Use
attr.converters.convertto convert an attribute to a specific type. - Focus on clean and readable code. Proper error handling is important.