Skip to content

Commit

Permalink
add unit to EnergyEmissions to get it as output from calculate_co2e()…
Browse files Browse the repository at this point in the history
…, removed in_kwh from output to mitigate repetition
  • Loading branch information
Milli97 committed Feb 18, 2025
1 parent f3d8913 commit 251327a
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 1 deletion.
3 changes: 3 additions & 0 deletions co2calculator/api/emission.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
"""Emission class"""

from dataclasses import dataclass
from typing import Optional

from pydantic import BaseModel


Expand All @@ -27,6 +29,7 @@ class EnergyEmissions(Emissions):
"""Class for storing information on energy emissions"""

consumption: float
unit: str = "kWh"

def __post_init__(self):
"""Validate the attribute values"""
Expand Down
16 changes: 15 additions & 1 deletion co2calculator/api/energy.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,17 @@
# -*- coding: utf-8 -*-
"""Energy classes"""
from typing import Optional

from co2calculator import ConversionFactors

from co2calculator.energy.calculate_energy import (
calc_co2_electricity,
calc_co2_heating,
)
from co2calculator.api.emission import EnergyEmissions

from co2calculator.energy.calculate_energy import conversion_factors


class Energy:
def __init__(
Expand Down Expand Up @@ -162,17 +167,26 @@ def calculate_co2e(self):
"own_share": self.own_share,
"in_kwh": self.in_kwh,
}

# Filter out items where value is None
options = {k: v for k, v in options.items() if v is not None}

co2e, emission_factor, emission_parameters = calc_co2_heating(
self.consumption, options=options
)
# Get the unit
if self.in_kwh:
unit = "kWh"
else:
unit = conversion_factors.get_unit()

# Remove in_kwh from emission_parameters to avoid repetition in output
delattr(emission_parameters, "in_kwh")

emissions = EnergyEmissions(
co2e=co2e,
emission_factor=emission_factor,
emission_parameters=emission_parameters,
consumption=self.consumption,
unit=unit,
)
return emissions
4 changes: 4 additions & 0 deletions co2calculator/data_handlers.py
Original file line number Diff line number Diff line change
Expand Up @@ -139,4 +139,8 @@ def get(self, fuel_type):
"No suitable conversion factor found in database. Please adapt your query."
)
else:
self._last_selected_factors = selected_factors
return selected_factors["conversion_value"].values[0]

def get_unit(self):
return self._last_selected_factors["unit"].values[0]

0 comments on commit 251327a

Please sign in to comment.