mirror of
https://github.com/kristoferssolo/LU-bookstore.git
synced 2025-10-21 18:00:34 +00:00
feat(book): create book class
This commit is contained in:
parent
4dc764072d
commit
06762fea13
22
src/bookstore/book.py
Normal file
22
src/bookstore/book.py
Normal file
@ -0,0 +1,22 @@
|
|||||||
|
from attrs import define, field, setters, validators
|
||||||
|
|
||||||
|
from .isbn import ISBN
|
||||||
|
|
||||||
|
|
||||||
|
def _check_price_value(instance, attribute, value):
|
||||||
|
if value < 0:
|
||||||
|
raise ValueError("Price must be larger or equal to 0!")
|
||||||
|
|
||||||
|
|
||||||
|
def _check_stock_value(instance, attribute, value):
|
||||||
|
if value < 0:
|
||||||
|
raise ValueError("Stock must be larger or equal to 0!")
|
||||||
|
|
||||||
|
|
||||||
|
@define
|
||||||
|
class Book:
|
||||||
|
title: str = field()
|
||||||
|
author: str = field() # TODO: add default author as "Unknown"
|
||||||
|
isbn: ISBN = field(on_setattr=setters.frozen, repr=lambda value: f"'{value}'")
|
||||||
|
price: float = field(converter=float, validator=[validators.instance_of(float), _check_price_value])
|
||||||
|
stock: int = field(converter=int, validator=[validators.instance_of(int), _check_stock_value])
|
||||||
Loading…
Reference in New Issue
Block a user