Skip to content

Commit f6dba1e

Browse files
authored
📝 Refactor Documentation for ISBN and MAC address modules (#124)
* 📝 Refactor Documentation for ISBN and MAC address modules * 📝 add more documentation * 📝 fix all docstring from review
1 parent 319b0d7 commit f6dba1e

File tree

2 files changed

+57
-23
lines changed

2 files changed

+57
-23
lines changed

pydantic_extra_types/isbn.py

Lines changed: 35 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
"""
2-
The `pydantic_extra_types.isbn` module provides functionality to recieve and validate ISBN
3-
(International Standard Book Number) in 10-digit and 13-digit formats. The output is always ISBN-13.
2+
The `pydantic_extra_types.isbn` module provides functionality to recieve and validate ISBN.
3+
4+
ISBN (International Standard Book Number) is a numeric commercial book identifier which is intended to be unique. This module provides a ISBN type for Pydantic models.
45
"""
56

67
from __future__ import annotations
@@ -12,14 +13,13 @@
1213

1314

1415
def isbn10_digit_calc(isbn: str) -> str:
15-
"""
16-
Calc a ISBN-10 last digit from the provided str value. More information of validation algorithm on [Wikipedia](https://en.wikipedia.org/wiki/ISBN#Check_digits)
16+
"""Calc a ISBN-10 last digit from the provided str value. More information of validation algorithm on [Wikipedia](https://en.wikipedia.org/wiki/ISBN#Check_digits)
1717
1818
Args:
1919
isbn: The str value representing the ISBN in 10 digits.
2020
2121
Returns:
22-
The calculated last digit.
22+
The calculated last digit of the ISBN-10 value.
2323
"""
2424
total = sum(int(digit) * (10 - idx) for idx, digit in enumerate(isbn[:9]))
2525

@@ -31,14 +31,13 @@ def isbn10_digit_calc(isbn: str) -> str:
3131

3232

3333
def isbn13_digit_calc(isbn: str) -> str:
34-
"""
35-
Calc a ISBN-13 last digit from the provided str value. More information of validation algorithm on [Wikipedia](https://en.wikipedia.org/wiki/ISBN#Check_digits)
34+
"""Calc a ISBN-13 last digit from the provided str value. More information of validation algorithm on [Wikipedia](https://en.wikipedia.org/wiki/ISBN#Check_digits)
3635
3736
Args:
3837
isbn: The str value representing the ISBN in 13 digits.
3938
4039
Returns:
41-
The calculated last digit.
40+
The calculated last digit of the ISBN-13 value.
4241
"""
4342
total = sum(int(digit) * (1 if idx % 2 == 0 else 3) for idx, digit in enumerate(isbn[:12]))
4443

@@ -67,27 +66,50 @@ class Book(BaseModel):
6766

6867
@classmethod
6968
def __get_pydantic_core_schema__(cls, source: type[Any], handler: GetCoreSchemaHandler) -> core_schema.CoreSchema:
69+
"""
70+
Return a Pydantic CoreSchema with the ISBN validation.
71+
72+
Args:
73+
source: The source type to be converted.
74+
handler: The handler to get the CoreSchema.
75+
76+
Returns:
77+
A Pydantic CoreSchema with the ISBN validation.
78+
79+
"""
7080
return core_schema.with_info_before_validator_function(
7181
cls._validate,
7282
core_schema.str_schema(),
7383
)
7484

7585
@classmethod
7686
def _validate(cls, __input_value: str, _: Any) -> str:
87+
"""
88+
Validate a ISBN from the provided str value.
89+
90+
Args:
91+
__input_value: The str value to be validated.
92+
_: The source type to be converted.
93+
94+
Returns:
95+
The validated ISBN.
96+
97+
Raises:
98+
PydanticCustomError: If the ISBN is not valid.
99+
"""
77100
cls.validate_isbn_format(__input_value)
78101

79102
return cls.convert_isbn10_to_isbn13(__input_value)
80103

81104
@staticmethod
82105
def validate_isbn_format(value: str) -> None:
83-
"""
84-
Validate a ISBN format from the provided str value.
106+
"""Validate a ISBN format from the provided str value.
85107
86108
Args:
87109
value: The str value representing the ISBN in 10 or 13 digits.
88110
89111
Raises:
90-
PydanticCustomError: If the value is not a valid ISBN.
112+
PydanticCustomError: If the ISBN is not valid.
91113
"""
92114

93115
isbn_length = len(value)
@@ -113,11 +135,10 @@ def validate_isbn_format(value: str) -> None:
113135

114136
@staticmethod
115137
def convert_isbn10_to_isbn13(value: str) -> str:
116-
"""
117-
Convert an ISBN-10 to ISBN-13.
138+
"""Convert an ISBN-10 to ISBN-13.
118139
119140
Args:
120-
value: The str value representing the ISBN.
141+
value: The ISBN-10 value to be converted.
121142
122143
Returns:
123144
The converted ISBN or the original value if no conversion is necessary.

pydantic_extra_types/mac_address.py

Lines changed: 22 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
"""
2-
The `pydantic_extra_types.mac_address` module provides functionality to parse and validate MAC addresses in different
2+
The MAC address module provides functionality to parse and validate MAC addresses in different
33
formats, such as IEEE 802 MAC-48, EUI-48, EUI-64, or a 20-octet format.
44
"""
55

@@ -32,28 +32,41 @@ class Network(BaseModel):
3232

3333
@classmethod
3434
def __get_pydantic_core_schema__(cls, source: type[Any], handler: GetCoreSchemaHandler) -> core_schema.CoreSchema:
35+
"""
36+
Return a Pydantic CoreSchema with the MAC address validation.
37+
38+
Args:
39+
source: The source type to be converted.
40+
handler: The handler to get the CoreSchema.
41+
42+
Returns:
43+
A Pydantic CoreSchema with the MAC address validation.
44+
45+
"""
3546
return core_schema.with_info_before_validator_function(
3647
cls._validate,
3748
core_schema.str_schema(),
3849
)
3950

4051
@classmethod
4152
def _validate(cls, __input_value: str, _: Any) -> str:
42-
return cls.validate_mac_address(__input_value.encode())
43-
44-
@staticmethod
45-
def validate_mac_address(value: bytes) -> str:
4653
"""
47-
Validate a MAC Address from the provided byte value.
54+
Validate a MAC Address from the provided str value.
4855
4956
Args:
50-
value: The byte value representing the MAC address.
57+
__input_value: The str value to be validated.
58+
_: The source type to be converted.
5159
5260
Returns:
5361
str: The parsed MAC address.
5462
55-
Raises:
56-
PydanticCustomError: If the value is not a valid MAC address.
63+
"""
64+
return cls.validate_mac_address(__input_value.encode())
65+
66+
@staticmethod
67+
def validate_mac_address(value: bytes) -> str:
68+
"""
69+
Validate a MAC Address from the provided byte value.
5770
"""
5871
if len(value) < 14:
5972
raise PydanticCustomError(

0 commit comments

Comments
 (0)