mirror of
https://github.com/kristoferssolo/LU-bookstore.git
synced 2025-10-21 18:00:34 +00:00
feat(csv): read/write inventory from/to csv file
This commit is contained in:
parent
0b135407e3
commit
692b07e79a
3
src/main.py → main.py
Normal file → Executable file
3
src/main.py → main.py
Normal file → Executable file
@ -1,3 +1,6 @@
|
|||||||
|
#!/usr/bin/env python3
|
||||||
|
|
||||||
|
|
||||||
def main() -> None:
|
def main() -> None:
|
||||||
pass
|
pass
|
||||||
|
|
||||||
@ -13,10 +13,14 @@ def _check_stock_value(instance, attribute, value):
|
|||||||
raise ValueError("Stock must be larger or equal to 0!")
|
raise ValueError("Stock must be larger or equal to 0!")
|
||||||
|
|
||||||
|
|
||||||
|
def _to_isbn(number: str):
|
||||||
|
return ISBN(number)
|
||||||
|
|
||||||
|
|
||||||
@define
|
@define
|
||||||
class Book:
|
class Book:
|
||||||
title: str = field()
|
title: str = field()
|
||||||
author: str = field() # TODO: add default author as "Unknown"
|
author: str = field() # TODO: add default author as "Unknown"
|
||||||
isbn: ISBN = field(on_setattr=setters.frozen, repr=lambda value: f"'{value}'")
|
isbn: ISBN = field(converter=_to_isbn, validator=validators.instance_of(ISBN), on_setattr=setters.frozen, repr=lambda value: f"'{value}'")
|
||||||
price: float = field(converter=float, validator=[validators.instance_of(float), _check_price_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])
|
stock: int = field(converter=int, validator=[validators.instance_of(int), _check_stock_value])
|
||||||
|
|||||||
@ -1,3 +1,4 @@
|
|||||||
|
import csv
|
||||||
from pathlib import Path
|
from pathlib import Path
|
||||||
|
|
||||||
from .book import Book
|
from .book import Book
|
||||||
@ -5,14 +6,29 @@ from .isbn import ISBN
|
|||||||
|
|
||||||
|
|
||||||
class Inventory:
|
class Inventory:
|
||||||
def __init__(self, books: list[Book]) -> None:
|
def __init__(self, books: list[Book] | None) -> None:
|
||||||
self.books: dict[ISBN, Book] = {book.isbn: book for book in books}
|
self.books: dict[ISBN, Book] = {book.isbn: book for book in books} if books else {}
|
||||||
|
|
||||||
def read_from_file(self, path: Path) -> None:
|
def read_from_file(self, path: Path) -> None:
|
||||||
"""Read `Inventory` from `path` csv file."""
|
"""Read `Inventory` from `path` csv file."""
|
||||||
|
with open(path, mode="r") as file:
|
||||||
|
reader = csv.reader(file)
|
||||||
|
|
||||||
|
next(reader, None) # Skip header
|
||||||
|
|
||||||
|
for title, author, isbn, price, stock in reader:
|
||||||
|
book = Book(title, author, isbn, price, stock)
|
||||||
|
self.add(book)
|
||||||
|
|
||||||
def save_to_file(self, path: Path) -> None:
|
def save_to_file(self, path: Path) -> None:
|
||||||
"""Save `Inventory` to `path` csv file."""
|
"""Save `Inventory` to `path` csv file."""
|
||||||
|
with open(path, mode="w") as file:
|
||||||
|
writer = csv.writer(file)
|
||||||
|
|
||||||
|
writer.writerow(["Title", "Auther", "ISBN", "Price", "Stock"])
|
||||||
|
|
||||||
|
for book in self.books.values():
|
||||||
|
writer.writerow([book.title, book.author, book.isbn, book.price, book.stock])
|
||||||
|
|
||||||
def add(self, book: Book) -> None:
|
def add(self, book: Book) -> None:
|
||||||
"""Add `Book` to the `Inventory`. `Book`s ISBN must be unique."""
|
"""Add `Book` to the `Inventory`. `Book`s ISBN must be unique."""
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user