From 8106c900860eef149cb96216accf2d84d14cd379 Mon Sep 17 00:00:00 2001 From: JohnVonNeumann Date: Tue, 16 Jul 2024 11:07:26 +1000 Subject: [PATCH] UPDATE carbon_footprint.py - add exempted_emissions_description attribute to CarbonFootprint --- .../carbon_footprint/carbon_footprint.py | 5 +++++ tests/carbon_footprint/test_carbon_footprint.py | 13 +++++++++++++ tests/product_footprint/test_product_footprint.py | 1 + 3 files changed, 19 insertions(+) diff --git a/pathfinder_framework/carbon_footprint/carbon_footprint.py b/pathfinder_framework/carbon_footprint/carbon_footprint.py index 036f633..b81a75d 100644 --- a/pathfinder_framework/carbon_footprint/carbon_footprint.py +++ b/pathfinder_framework/carbon_footprint/carbon_footprint.py @@ -32,6 +32,7 @@ class CarbonFootprint: ipcc_characterization_factors_sources (list[str]): The characterization factors from one or more IPCC Assessment Reports used in the calculation of the PCF. cross_sectoral_standards_used (list[CrossSectoralStandard]): The cross-sectoral standards applied for calculating or allocating GHG emissions. exempted_emissions_percent (float): The Percentage of emissions excluded from PCF, expressed as a decimal number between 0.0 and 5 including. + exempted_emissions_description (str): Rationale behind exclusion of specific PCF emissions, can be the empty string if no emissions were excluded. reference_period (ReferencePeriod): The period over which the data was recorded for the Carbon Footprint_ packaging_emissions_included (bool): A boolean flag indicating whether packaging emissions are included in the PCF (pCfExcludingBiogenic, pCfIncludingBiogenic). geographical_scope (CarbonFootprintGeographicalScope): The geographical scope of the carbon footprint. @@ -66,6 +67,7 @@ def __init__( cross_sectoral_standards_used, boundary_processes_description, exempted_emissions_percent, + exempted_emissions_description, reference_period, packaging_emissions_included, geographical_scope, @@ -120,6 +122,8 @@ def __init__( raise ValueError("boundary_processes_description must not be empty") if not 0.0 <= exempted_emissions_percent <= 5.0: raise ValueError("exempted_emissions_percent must be between 0.0 and 5.0") + if not isinstance(exempted_emissions_description, str): + raise ValueError("exempted_emissions_description must be a string") if not isinstance(reference_period, ReferencePeriod): raise ValueError("reference_period must be an instance of ReferencePeriod") if not isinstance(packaging_emissions_included, bool): @@ -214,6 +218,7 @@ def __init__( self.cross_sectoral_standards_used = cross_sectoral_standards_used self.boundary_processes_description = boundary_processes_description self.exempted_emissions_percent = exempted_emissions_percent + self.exempted_emissions_description = exempted_emissions_description self.reference_period = reference_period self.packaging_emissions_included = packaging_emissions_included self.geographical_scope = geographical_scope diff --git a/tests/carbon_footprint/test_carbon_footprint.py b/tests/carbon_footprint/test_carbon_footprint.py index 04f571d..45eb5a4 100644 --- a/tests/carbon_footprint/test_carbon_footprint.py +++ b/tests/carbon_footprint/test_carbon_footprint.py @@ -43,6 +43,7 @@ def valid_carbon_footprint_data(): "cross_sectoral_standards_used": [CrossSectoralStandard.GHG_PROTOCOL], "boundary_processes_description": "boundary processes description", "exempted_emissions_percent": 1.0, + "exempted_emissions_description": "Rationale for exclusion", "reference_period": ReferencePeriod(start=DateTime.now(), end=DateTime.now()), "packaging_emissions_included": True, "geographical_scope": CarbonFootprintGeographicalScope( @@ -141,6 +142,10 @@ def test_carbon_footprint_attributes(valid_carbon_footprint_data): carbon_footprint.exempted_emissions_percent == valid_carbon_footprint_data["exempted_emissions_percent"] ) + assert ( + carbon_footprint.exempted_emissions_description + == valid_carbon_footprint_data["exempted_emissions_description"] + ) assert isinstance(carbon_footprint.reference_period, ReferencePeriod) assert ( carbon_footprint.packaging_emissions_included @@ -310,6 +315,14 @@ def test_carbon_footprint_invalid_exempted_emissions_percent( ) +def test_carbon_footprint_invalid_exempted_emissions_description(valid_carbon_footprint_data): + invalid_data = valid_carbon_footprint_data.copy() + invalid_data["exempted_emissions_description"] = 123 # Invalid type + with pytest.raises(ValueError) as excinfo: + CarbonFootprint(**invalid_data) + assert str(excinfo.value) == "exempted_emissions_description must be a string" + + def test_carbon_footprint_invalid_reference_period(valid_carbon_footprint_data): invalid_data = valid_carbon_footprint_data.copy() invalid_data["reference_period"] = "invalid reference period" diff --git a/tests/product_footprint/test_product_footprint.py b/tests/product_footprint/test_product_footprint.py index f8b26e1..d427166 100644 --- a/tests/product_footprint/test_product_footprint.py +++ b/tests/product_footprint/test_product_footprint.py @@ -74,6 +74,7 @@ def valid_carbon_footprint_data(): "cross_sectoral_standards_used": [CrossSectoralStandard.GHG_PROTOCOL], "boundary_processes_description": "boundary processes description", "exempted_emissions_percent": 1.0, + "exempted_emissions_description": "Rationale for exclusion", "reference_period": ReferencePeriod(start=DateTime.now(), end=DateTime.now()), "packaging_emissions_included": True, "geographical_scope": CarbonFootprintGeographicalScope(