Skip to content

bookops_worldcat.utils

Shared utilities module.

prep_oclc_number_str

prep_oclc_number_str(oclcNumber: str) -> str

Checks for OCLC prefixes and removes them.

PARAMETER DESCRIPTION
oclcNumber

OCLC record number as string

TYPE: str

RETURNS DESCRIPTION
str

oclcNumber as str

RAISES DESCRIPTION
InvalidOclcNumber

If oclcNumber argument does not match

Source code in bookops_worldcat\utils.py
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
def prep_oclc_number_str(oclcNumber: str) -> str:
    """
    Checks for OCLC prefixes and removes them.

    Args:
        oclcNumber:
            OCLC record number as string

    Returns:
        `oclcNumber` as str

    Raises:
        InvalidOclcNumber: If `oclcNumber` argument does not match
        OCLC number formatting rules.
    """

    if re.match(r"^ocm[0-9]{,8}$", oclcNumber.strip()) or re.match(
        r"^ocn[0-9]{9}$", oclcNumber.strip()
    ):
        oclcNumber = oclcNumber.strip()[3:]
    elif re.match(r"^on[0-9]{10,}$", oclcNumber.strip()):
        oclcNumber = oclcNumber.strip()[2:]
    elif re.match(r"^\(OCoLC\)[0-9]{8,}$", oclcNumber.strip()):
        oclcNumber = oclcNumber.strip()[7:]

    try:
        oclcNumber = str(int(oclcNumber))
        return oclcNumber
    except ValueError:
        raise InvalidOclcNumber("Argument 'oclcNumber' does not look like real OCLC #.")

verify_oclc_number

verify_oclc_number(oclcNumber: Union[int, str]) -> str

Verifies a valid looking OCLC number is passed and normalize it as integer.

PARAMETER DESCRIPTION
oclcNumber

OCLC record number as string or integer

TYPE: Union[int, str]

RETURNS DESCRIPTION
str

oclcNumber as str

RAISES DESCRIPTION
InvalidOclcNumber

If oclcNumber argument is not a str or int or is missing.

Source code in bookops_worldcat\utils.py
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
def verify_oclc_number(oclcNumber: Union[int, str]) -> str:
    """
    Verifies a valid looking OCLC number is passed and normalize it as integer.

    Args:
        oclcNumber:
            OCLC record number as string or integer

    Returns:
        `oclcNumber` as str

    Raises:
        InvalidOclcNumber: If `oclcNumber` argument is not a str or int or is missing.
    """
    if not oclcNumber:
        raise InvalidOclcNumber("Argument 'oclcNumber' is missing.")

    elif isinstance(oclcNumber, int):
        return str(oclcNumber)

    elif isinstance(oclcNumber, str):
        return prep_oclc_number_str(oclcNumber)

    else:
        raise InvalidOclcNumber("Argument 'oclcNumber' is of invalid type.")

verify_oclc_numbers

verify_oclc_numbers(
    oclcNumbers: Union[int, str, List[Union[str, int]]]
) -> List[str]

Parses and verifies list of oclcNumbers

PARAMETER DESCRIPTION
oclcNumbers

List of OCLC control numbers. Control numbers can be integers or strings with or without OCLC # prefix. If str, the numbers must be separated by commas. If int, only one number will be parsed. Lists may contain strings or integers or a combination of both.

TYPE: Union[int, str, List[Union[str, int]]]

RETURNS DESCRIPTION
List[str]

oclcNumbers as a list of strings

RAISES DESCRIPTION
InvalidOclcNumber

If oclcNumbers argument is not a list, str, or int.

Source code in bookops_worldcat\utils.py
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
def verify_oclc_numbers(
    oclcNumbers: Union[int, str, List[Union[str, int]]]
) -> List[str]:
    """
    Parses and verifies list of oclcNumbers

    Args:
        oclcNumbers:
            List of OCLC control numbers. Control numbers can be integers or strings
            with or without OCLC # prefix. If str, the numbers must be separated
            by commas. If int, only one number will be parsed. Lists may contain strings
            or integers or a combination of both.

    Returns:
        `oclcNumbers` as a list of strings

    Raises:
        InvalidOclcNumber: If `oclcNumbers` argument is not a list, str, or int.
    """
    if isinstance(oclcNumbers, str):
        oclcNumbers_lst = _str2list(oclcNumbers)
    elif isinstance(oclcNumbers, int):
        oclcNumbers_lst = _str2list(str(oclcNumbers))
    elif isinstance(oclcNumbers, list):
        oclcNumbers_lst = [str(n) for n in oclcNumbers]
    else:
        raise InvalidOclcNumber(
            "Argument 'oclcNumbers' must be a single integer, a list or a "
            "comma separated string of valid OCLC #s."
        )
    if not oclcNumbers_lst:
        raise InvalidOclcNumber(
            "Argument 'oclcNumbers' must be a single integer, a list or a "
            "comma separated string of valid OCLC #s."
        )

    vetted_numbers = [verify_oclc_number(n) for n in oclcNumbers_lst]
    return vetted_numbers