summaryrefslogtreecommitdiff
path: root/venv/lib/python3.11/site-packages/faker/providers/sbn/sbn.py
blob: 070f799b989d8a322e536712f26fe2e10e41085f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
"""
This module is responsible for generating the check digit and formatting
SBN numbers.
"""
from typing import Any, Optional


class SBN:
    MAX_LENGTH = 9

    def __init__(
        self,
        registrant: Optional[str] = None,
        publication: Optional[str] = None,
    ) -> None:
        self.registrant = registrant
        self.publication = publication


class SBN9(SBN):
    def __init__(self, *args: Any, **kwargs: Any) -> None:
        super().__init__(*args, **kwargs)
        self.check_digit = self._check_digit()

    def _check_digit(self) -> str:
        """Calculate the check digit for SBN-9.
        SBNs use the same check digit calculation as ISBN. See
        https://en.wikipedia.org/wiki/International_Standard_Book_Number
                for calculation. Only modification is weights range from 1 to 9
                instead of 1 to 10.
        """
        weights = range(1, 9)
        body = "".join([part for part in [self.registrant, self.publication] if part is not None])
        remainder = sum(int(b) * w for b, w in zip(body, weights)) % 11
        check_digit = "X" if remainder == 10 else str(remainder)
        return str(check_digit)

    def format(self, separator: str = "") -> str:
        return separator.join(
            [
                part
                for part in [
                    self.registrant,
                    self.publication,
                    self.check_digit,
                ]
                if part is not None
            ]
        )