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
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
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_ids

verify_ids(
    ids: Union[str, int, list[str], list[int], None]
) -> Union[str, None]

Parses list of registry IDs or OCLC symbols. IDs will be joined and returned as a comma-separated string if more than one id is passed.

PARAMETER DESCRIPTION
ids

one or more institution registry ID(s) or OCLC symbol(s)

EXAMPLES:

  • "58122"
  • "58122,13437"
  • "58122, 13437"
  • ["58122", 13437]
  • "BKL"
  • "BKL,NYP"
  • ["BKL"]
  • ["BKL", "NYP"]

TYPE: Union[str, int, list[str], list[int], None]

RETURNS DESCRIPTION
Union[str, None]

ids as a string or None

Source code in bookops_worldcat/utils.py
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
def verify_ids(
    ids: Union[str, int, list[str], list[int], None],
) -> Union[str, None]:
    """
    Parses list of registry IDs or OCLC symbols. IDs will be joined
    and returned as a comma-separated string if more than one id
    is passed.

    Args:
        ids:
            one or more institution registry ID(s) or OCLC symbol(s)

            **EXAMPLES:**

            - "58122"
            - "58122,13437"
            - "58122, 13437"
            - ["58122", 13437]
            - "BKL"
            - "BKL,NYP"
            - ["BKL"]
            - ["BKL", "NYP"]

    Returns:
        ids as a string or None
    """
    if isinstance(ids, int):
        return str(ids)
    elif isinstance(ids, str):
        return ",".join([str(i.strip()) for i in ids.split(",")])
    elif isinstance(ids, list):
        return ",".join([str(i) for i in ids])
    else:
        return ids

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
 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
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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
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