Skip to content

Product or Sector Specific Rule

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.

ProductOrSectorSpecificRule

Represents a product or sector specific rule for calculating carbon footprints.

This class represents rules and methodologies published by specific operators that must be followed when calculating product carbon footprints. These rules can come from various sources like PEF (Product Environmental Footprint) or custom operators.

Attributes:

Name Type Description
operator ProductOrSectorSpecificRuleOperator

The operator that published the specific rule. Must be a valid ProductOrSectorSpecificRuleOperator enum value.

rule_names list[str]

A list of names of the rules applied from the specified operator. Each rule name must be a string.

other_operator_name str | None

The name of the operator if the operator is 'Other'. Required when operator is ProductOrSectorSpecificRuleOperator.OTHER, must be None otherwise.

Examples:

Create a rule for PEF methodology:

>>> rule = ProductOrSectorSpecificRule(
...     operator=ProductOrSectorSpecificRuleOperator.PEF,
...     rule_names=["PEFCR Guidance v6.3"]
... )

Create a rule with a custom operator:

>>> custom_rule = ProductOrSectorSpecificRule(
...     operator=ProductOrSectorSpecificRuleOperator.OTHER,
...     rule_names=["Custom Rule 1", "Custom Rule 2"],
...     other_operator_name="My Organization"
... )

Convert rule to dictionary format:

>>> rule.to_dict()
{'operator': 'PEF', 'ruleNames': ['PEFCR Guidance v6.3']}

Raises:

Type Description
ValueError

If operator is not a valid ProductOrSectorSpecificRuleOperator, if rule_names is not a list of strings, or if other_operator_name is provided incorrectly based on the operator value.

TypeError

If required arguments are missing during initialization.

Note

The other_operator_name is only valid and required when operator is set to ProductOrSectorSpecificRuleOperator.OTHER. For all other operators, it must be None.

Source code in pact_methodology/carbon_footprint/product_or_sector_specific_rule.py
  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
 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
 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
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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
class ProductOrSectorSpecificRule:
    """Represents a product or sector specific rule for calculating carbon footprints.

    This class represents rules and methodologies published by specific operators that must be followed
    when calculating product carbon footprints. These rules can come from various sources like PEF
    (Product Environmental Footprint) or custom operators.

    Attributes:
        operator (ProductOrSectorSpecificRuleOperator): The operator that published the specific rule.
            Must be a valid ProductOrSectorSpecificRuleOperator enum value.
        rule_names (list[str]): A list of names of the rules applied from the specified operator.
            Each rule name must be a string.
        other_operator_name (str | None): The name of the operator if the operator is 'Other'.
            Required when operator is ProductOrSectorSpecificRuleOperator.OTHER, must be None otherwise.

    Examples:
        Create a rule for PEF methodology:
        >>> rule = ProductOrSectorSpecificRule(
        ...     operator=ProductOrSectorSpecificRuleOperator.PEF,
        ...     rule_names=["PEFCR Guidance v6.3"]
        ... )

        Create a rule with a custom operator:
        >>> custom_rule = ProductOrSectorSpecificRule(
        ...     operator=ProductOrSectorSpecificRuleOperator.OTHER,
        ...     rule_names=["Custom Rule 1", "Custom Rule 2"],
        ...     other_operator_name="My Organization"
        ... )

        Convert rule to dictionary format:
        >>> rule.to_dict()
        {'operator': 'PEF', 'ruleNames': ['PEFCR Guidance v6.3']}

    Raises:
        ValueError: If operator is not a valid ProductOrSectorSpecificRuleOperator,
            if rule_names is not a list of strings, or if other_operator_name is provided
            incorrectly based on the operator value.
        TypeError: If required arguments are missing during initialization.

    Note:
        The other_operator_name is only valid and required when operator is set to
        ProductOrSectorSpecificRuleOperator.OTHER. For all other operators, it must be None.
    """

    def __init__(
        self,
        operator: ProductOrSectorSpecificRuleOperator,
        rule_names: list[str],
        other_operator_name: str | None = None
    ):
        """Initialize a ProductOrSectorSpecificRule instance.

        Args:
            operator: The operator publishing the rule (e.g. PEF, GHG Protocol)
            rule_names: List of specific rule names from the operator
            other_operator_name: Name of custom operator if operator is "Other"

        Raises:
            ValueError: If arguments are invalid
            TypeError: If required arguments are missing
        """
        self.operator = operator
        self.rule_names = rule_names
        self.other_operator_name = other_operator_name

    @property
    def operator(self) -> ProductOrSectorSpecificRuleOperator:
        """Get the rule operator.

        Returns:
            The ProductOrSectorSpecificRuleOperator enum value
        """
        return self._operator

    @operator.setter
    def operator(self, value: ProductOrSectorSpecificRuleOperator):
        """Set the rule operator.

        Args:
            value: The operator enum value to set

        Raises:
            ValueError: If value is not a ProductOrSectorSpecificRuleOperator
        """
        if not isinstance(value, ProductOrSectorSpecificRuleOperator):
            raise ValueError("operator must be an instance of ProductOrSectorSpecificRuleOperator")
        self._operator = value

    @property
    def rule_names(self) -> list[str]:
        """Get the list of rule names.

        Returns:
            List of rule name strings
        """
        return self._rule_names

    @rule_names.setter
    def rule_names(self, value: list[str]):
        """Set the list of rule names.

        Args:
            value: List of rule name strings

        Raises:
            ValueError: If value is not a list of strings
        """
        if not isinstance(value, list) or not all(isinstance(rule, str) for rule in value):
            raise ValueError("rule_names must be a list of strings")
        self._rule_names = value

    @property
    def other_operator_name(self) -> str | None:
        """Get the custom operator name if operator is "Other".

        Returns:
            The custom operator name string or None
        """
        return self._other_operator_name

    @other_operator_name.setter
    def other_operator_name(self, value: str | None):
        """Set the custom operator name.

        Args:
            value: The custom operator name string or None

        Raises:
            ValueError: If value is provided incorrectly based on operator
        """
        if self.operator == ProductOrSectorSpecificRuleOperator.OTHER and not value:
            raise ValueError("other_operator_name must be provided if operator is 'Other'")
        if self.operator != ProductOrSectorSpecificRuleOperator.OTHER and value:
            raise ValueError("other_operator_name must not be provided if operator is not 'Other'")
        self._other_operator_name = value

    def to_dict(self) -> dict:
        """Convert the rule to a dictionary representation.

        Returns:
            A dictionary with the following structure:
            {
                "operator": str,  # The operator value
                "rule_names": list[str],  # List of rule names
                "other_operator_name": str | None  # Custom operator name if applicable
            }
        """
        rule_dict = {
            "operator": self.operator.value,
            "rule_names": self.rule_names,
        }

        if self.operator == ProductOrSectorSpecificRuleOperator.OTHER:
            rule_dict["other_operator_name"] = self.other_operator_name

        return rule_dict

    def __str__(self) -> str:
        """Get string representation of the rule.

        Returns:
            A human-readable string showing the rule's attributes
        """
        return (
            f"operator={self.operator}, rule_names={self.rule_names}, other_operator_name={self.other_operator_name}"
        )

    def __repr__(self) -> str:
        """Get detailed string representation of the rule.

        Returns:
            A string that could be used to recreate the object
        """
        return (
            f"ProductOrSectorSpecificRule(operator={self.operator!r}, "
            f"rule_names={self.rule_names!r}, other_operator_name={self.other_operator_name!r})"
        )

operator property writable

Get the rule operator.

Returns:

Type Description
ProductOrSectorSpecificRuleOperator

The ProductOrSectorSpecificRuleOperator enum value

other_operator_name property writable

Get the custom operator name if operator is "Other".

Returns:

Type Description
str | None

The custom operator name string or None

rule_names property writable

Get the list of rule names.

Returns:

Type Description
list[str]

List of rule name strings

__init__(operator, rule_names, other_operator_name=None)

Initialize a ProductOrSectorSpecificRule instance.

Parameters:

Name Type Description Default
operator ProductOrSectorSpecificRuleOperator

The operator publishing the rule (e.g. PEF, GHG Protocol)

required
rule_names list[str]

List of specific rule names from the operator

required
other_operator_name str | None

Name of custom operator if operator is "Other"

None

Raises:

Type Description
ValueError

If arguments are invalid

TypeError

If required arguments are missing

Source code in pact_methodology/carbon_footprint/product_or_sector_specific_rule.py
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
def __init__(
    self,
    operator: ProductOrSectorSpecificRuleOperator,
    rule_names: list[str],
    other_operator_name: str | None = None
):
    """Initialize a ProductOrSectorSpecificRule instance.

    Args:
        operator: The operator publishing the rule (e.g. PEF, GHG Protocol)
        rule_names: List of specific rule names from the operator
        other_operator_name: Name of custom operator if operator is "Other"

    Raises:
        ValueError: If arguments are invalid
        TypeError: If required arguments are missing
    """
    self.operator = operator
    self.rule_names = rule_names
    self.other_operator_name = other_operator_name

__repr__()

Get detailed string representation of the rule.

Returns:

Type Description
str

A string that could be used to recreate the object

Source code in pact_methodology/carbon_footprint/product_or_sector_specific_rule.py
173
174
175
176
177
178
179
180
181
182
def __repr__(self) -> str:
    """Get detailed string representation of the rule.

    Returns:
        A string that could be used to recreate the object
    """
    return (
        f"ProductOrSectorSpecificRule(operator={self.operator!r}, "
        f"rule_names={self.rule_names!r}, other_operator_name={self.other_operator_name!r})"
    )

__str__()

Get string representation of the rule.

Returns:

Type Description
str

A human-readable string showing the rule's attributes

Source code in pact_methodology/carbon_footprint/product_or_sector_specific_rule.py
163
164
165
166
167
168
169
170
171
def __str__(self) -> str:
    """Get string representation of the rule.

    Returns:
        A human-readable string showing the rule's attributes
    """
    return (
        f"operator={self.operator}, rule_names={self.rule_names}, other_operator_name={self.other_operator_name}"
    )

to_dict()

Convert the rule to a dictionary representation.

Returns:

Type Description
dict

A dictionary with the following structure:

dict

{ "operator": str, # The operator value "rule_names": list[str], # List of rule names "other_operator_name": str | None # Custom operator name if applicable

dict

}

Source code in pact_methodology/carbon_footprint/product_or_sector_specific_rule.py
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
def to_dict(self) -> dict:
    """Convert the rule to a dictionary representation.

    Returns:
        A dictionary with the following structure:
        {
            "operator": str,  # The operator value
            "rule_names": list[str],  # List of rule names
            "other_operator_name": str | None  # Custom operator name if applicable
        }
    """
    rule_dict = {
        "operator": self.operator.value,
        "rule_names": self.rule_names,
    }

    if self.operator == ProductOrSectorSpecificRuleOperator.OTHER:
        rule_dict["other_operator_name"] = self.other_operator_name

    return rule_dict