From 42d881dbb40c170a1ea7c96bde9c3d75eef6c201 Mon Sep 17 00:00:00 2001 From: Guille Date: Fri, 24 Nov 2023 06:58:01 +0100 Subject: [PATCH] Add default values when there are no consumption and therefore no emissions can be calculated --- co2_emission/co2_emission.py | 16 +++++++++++++++- tests/unit_tests.py | 1 + 2 files changed, 16 insertions(+), 1 deletion(-) diff --git a/co2_emission/co2_emission.py b/co2_emission/co2_emission.py index 6739ca7..afa250d 100644 --- a/co2_emission/co2_emission.py +++ b/co2_emission/co2_emission.py @@ -22,6 +22,8 @@ class Co2Emission: cte.RENEWABLE: 0} self._emissions_factor = emissions_factor self._building = building + self._year = [0] + self._month = [0 for _ in range(12)] @property def building(self) -> Building: @@ -63,11 +65,23 @@ class Co2Emission: values = [v * emission_factor for v in self._building.lighting_electrical_demand[time_period]] results_by_time_period[time_period] = values results[cte.LIGHTING] = results_by_time_period - results_by_time_period = {} for time_period in self._building.appliances_electrical_demand: values = [v * emission_factor for v in self._building.appliances_electrical_demand[time_period]] results_by_time_period[time_period] = values results[cte.APPLIANCES] = results_by_time_period + if cte.HEATING not in results: + results[cte.HEATING] = {cte.YEAR: self._year, cte.MONTH: self._month} + if cte.COOLING not in results: + results[cte.COOLING] = {cte.YEAR: self._year, cte.MONTH: self._month} + if cte.ELECTRICITY not in results: + results[cte.ELECTRICITY] = {cte.YEAR: self._year, cte.MONTH: self._month} + if cte.LIGHTING not in results: + results[cte.LIGHTING] = {cte.YEAR: self._year, cte.MONTH: self._month} + if cte.APPLIANCES not in results: + results[cte.APPLIANCES] = {cte.YEAR: self._year, cte.MONTH: self._month} + if cte.DOMESTIC_HOT_WATER not in results: + results[cte.DOMESTIC_HOT_WATER] = {cte.YEAR: self._year, cte.MONTH: self._month} + return results diff --git a/tests/unit_tests.py b/tests/unit_tests.py index e4fbcb7..7e5c876 100644 --- a/tests/unit_tests.py +++ b/tests/unit_tests.py @@ -62,6 +62,7 @@ class UnitTests(unittest.TestCase): for building in self.init.city.buildings: result = Co2Emission(building).operational_co2 self.assertIsNotNone(result) + print(result) def tearDown(self): files = glob.glob('output/[!.]*')