Skip to content

CPC

This part of the project documentation focuses on an information-oriented approach. Use it as a reference for the technical implementation of the pact_methodology project code.

Represents a Central Product Classification (CPC) code.

Attributes:

Name Type Description
code str

The CPC code.

title str

The title of the CPC code.

section str

The section of the CPC code.

division str

The division of the CPC code.

group str

The group of the CPC code.

class_ str

The class of the CPC code.

subclass str

The subclass of the CPC code.

Example

cpc = CPC("0111", "Wheat") cpc.code '0111' cpc.title 'Wheat' cpc.section '0'

Source code in pact_methodology/product_footprint/cpc.py
 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
50
51
52
53
54
55
56
class CPC:
    """Represents a Central Product Classification (CPC) code.

    Attributes:
        code (str): The CPC code.
        title (str): The title of the CPC code.
        section (str): The section of the CPC code.
        division (str): The division of the CPC code.
        group (str): The group of the CPC code.
        class_ (str): The class of the CPC code.
        subclass (str): The subclass of the CPC code.

    Example:
        >>> cpc = CPC("0111", "Wheat")
        >>> cpc.code
        '0111'
        >>> cpc.title
        'Wheat'
        >>> cpc.section
        '0'
    """

    def __init__(self, code: str, title: str):
        """Initializes a CPC object.

        Args:
            code (str): The CPC code.
            title (str): The title of the CPC code.
        """
        self.code = code
        self.title = title
        self.section = code[0]
        self.division = code[:2]
        self.group = code[:3]
        self.class_ = code[:4]
        self.subclass = code[:5]

    def __eq__(self, other):
        """Checks equality with another CPC object.

        Example:
            >>> cpc1 = CPC("0111", "Wheat")
            >>> cpc2 = CPC("0111", "Wheat")
            >>> cpc3 = CPC("0112", "Maize (corn)")
            >>> cpc1 == cpc2
            True
            >>> cpc1 == cpc3
            False
        """
        if not isinstance(other, CPC):
            return False
        return self.code == other.code

__eq__(other)

Checks equality with another CPC object.

Example

cpc1 = CPC("0111", "Wheat") cpc2 = CPC("0111", "Wheat") cpc3 = CPC("0112", "Maize (corn)") cpc1 == cpc2 True cpc1 == cpc3 False

Source code in pact_methodology/product_footprint/cpc.py
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
def __eq__(self, other):
    """Checks equality with another CPC object.

    Example:
        >>> cpc1 = CPC("0111", "Wheat")
        >>> cpc2 = CPC("0111", "Wheat")
        >>> cpc3 = CPC("0112", "Maize (corn)")
        >>> cpc1 == cpc2
        True
        >>> cpc1 == cpc3
        False
    """
    if not isinstance(other, CPC):
        return False
    return self.code == other.code

__init__(code, title)

Initializes a CPC object.

Parameters:

Name Type Description Default
code str

The CPC code.

required
title str

The title of the CPC code.

required
Source code in pact_methodology/product_footprint/cpc.py
27
28
29
30
31
32
33
34
35
36
37
38
39
40
def __init__(self, code: str, title: str):
    """Initializes a CPC object.

    Args:
        code (str): The CPC code.
        title (str): The title of the CPC code.
    """
    self.code = code
    self.title = title
    self.section = code[0]
    self.division = code[:2]
    self.group = code[:3]
    self.class_ = code[:4]
    self.subclass = code[:5]

A lookup table for CPC codes.

Attributes:

Name Type Description
filename str

The name of the CSV file containing the CPC codes.

cpc_codes dict[str, CPC]

A dictionary mapping CPC codes to CPC objects.

Example

lookup = CPCCodeLookup() cpc = lookup.lookup("0111") cpc.title 'Wheat'

Source code in pact_methodology/product_footprint/cpc.py
 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
 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
114
115
116
117
118
119
120
121
122
123
124
class CPCCodeLookup:
    """A lookup table for CPC codes.

    Attributes:
        filename (str): The name of the CSV file containing the CPC codes.
        cpc_codes (dict[str, CPC]): A dictionary mapping CPC codes to CPC objects.

    Example:
        >>> lookup = CPCCodeLookup()
        >>> cpc = lookup.lookup("0111")
        >>> cpc.title
        'Wheat'
    """

    def __init__(self):
        """Initializes a CPCCodeLookup object."""
        self.filename = os.path.join(os.path.dirname(__file__), "cpc_v21.csv")
        self.cpc_codes = {}
        self.load_cpc_codes()

    def load_cpc_codes(self) -> None:
        """Loads the CPC codes from the CSV file.

        Raises:
            FileNotFoundError: If the CSV file does not exist.
            csv.Error: If there is an error reading the CSV file.
        """
        try:
            with open(self.filename, "r") as file:
                reader = csv.DictReader(file)
                for row in reader:
                    code = row["CPC21code"]
                    title = row["CPC21title"]
                    self.cpc_codes[code] = CPC(code, title)
        except FileNotFoundError:
            raise FileNotFoundError(f"File not found: {self.filename}")
        except csv.Error as e:
            raise RuntimeError(f"Error reading CSV file: {e}")

    def lookup(self, cpc_code: str) -> CPC | None:
        """Looks up a CPC code.

        Args:
            cpc_code (str): The CPC code to look up. Must be a string of 2-5 digits.

        Returns:
            CPC | None: The CPC object corresponding to the CPC code, or None if not found.

        Raises:
            ValueError: If the CPC code is not a numerical string with a maximum length of 5.

        Example:
            >>> lookup = CPCCodeLookup()
            >>> cpc = lookup.lookup("0111")
            >>> cpc.title
            'Wheat'
            >>> lookup.lookup("99999") is None
            True
        """
        if not cpc_code:
            raise ValueError("CPC code cannot be empty")
        if len(cpc_code) > 5:
            raise ValueError("CPC code cannot be longer than 5 digits")
        if not cpc_code.isdigit():
            raise ValueError("CPC code must contain only digits")
        return self.cpc_codes.get(cpc_code, None)

__init__()

Initializes a CPCCodeLookup object.

Source code in pact_methodology/product_footprint/cpc.py
73
74
75
76
77
def __init__(self):
    """Initializes a CPCCodeLookup object."""
    self.filename = os.path.join(os.path.dirname(__file__), "cpc_v21.csv")
    self.cpc_codes = {}
    self.load_cpc_codes()

load_cpc_codes()

Loads the CPC codes from the CSV file.

Raises:

Type Description
FileNotFoundError

If the CSV file does not exist.

Error

If there is an error reading the CSV file.

Source code in pact_methodology/product_footprint/cpc.py
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
def load_cpc_codes(self) -> None:
    """Loads the CPC codes from the CSV file.

    Raises:
        FileNotFoundError: If the CSV file does not exist.
        csv.Error: If there is an error reading the CSV file.
    """
    try:
        with open(self.filename, "r") as file:
            reader = csv.DictReader(file)
            for row in reader:
                code = row["CPC21code"]
                title = row["CPC21title"]
                self.cpc_codes[code] = CPC(code, title)
    except FileNotFoundError:
        raise FileNotFoundError(f"File not found: {self.filename}")
    except csv.Error as e:
        raise RuntimeError(f"Error reading CSV file: {e}")

lookup(cpc_code)

Looks up a CPC code.

Parameters:

Name Type Description Default
cpc_code str

The CPC code to look up. Must be a string of 2-5 digits.

required

Returns:

Type Description
CPC | None

CPC | None: The CPC object corresponding to the CPC code, or None if not found.

Raises:

Type Description
ValueError

If the CPC code is not a numerical string with a maximum length of 5.

Example

lookup = CPCCodeLookup() cpc = lookup.lookup("0111") cpc.title 'Wheat' lookup.lookup("99999") is None True

Source code in pact_methodology/product_footprint/cpc.py
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
def lookup(self, cpc_code: str) -> CPC | None:
    """Looks up a CPC code.

    Args:
        cpc_code (str): The CPC code to look up. Must be a string of 2-5 digits.

    Returns:
        CPC | None: The CPC object corresponding to the CPC code, or None if not found.

    Raises:
        ValueError: If the CPC code is not a numerical string with a maximum length of 5.

    Example:
        >>> lookup = CPCCodeLookup()
        >>> cpc = lookup.lookup("0111")
        >>> cpc.title
        'Wheat'
        >>> lookup.lookup("99999") is None
        True
    """
    if not cpc_code:
        raise ValueError("CPC code cannot be empty")
    if len(cpc_code) > 5:
        raise ValueError("CPC code cannot be longer than 5 digits")
    if not cpc_code.isdigit():
        raise ValueError("CPC code must contain only digits")
    return self.cpc_codes.get(cpc_code, None)