mirror of
https://github.com/kristoferssolo/LU-bookstore.git
synced 2025-10-21 18:00:34 +00:00
feat(isbn): create isbn class
This commit is contained in:
parent
cd5154c757
commit
4dc764072d
1
requirements.txt
Normal file
1
requirements.txt
Normal file
@ -0,0 +1 @@
|
||||
attrs==23.1.0
|
||||
12
src/bookstore/isbn.py
Normal file
12
src/bookstore/isbn.py
Normal file
@ -0,0 +1,12 @@
|
||||
from attrs import define, field, validators
|
||||
|
||||
|
||||
# TODO: create checksum method
|
||||
@define(frozen=True)
|
||||
class ISBN(str):
|
||||
number: str = field(converter=str, validator=validators.matches_re(r"^\d{10}$|^\d{13}$"))
|
||||
|
||||
def __str__(self):
|
||||
if len(self.number) == 10:
|
||||
return f"{self.number[:9]}-{self.number[9:]}"
|
||||
return f"{self.number[:3]}-{self.number[3:4]}-{self.number[4:6]}-{self.number[6:12]}-{self.number[12:]}"
|
||||
Loading…
Reference in New Issue
Block a user