3
\$\begingroup\$

I would like to get the simple carbon emissions calculations below written in the most Pythonic / straightforward way.

FUEL_FACTORS = {'diesel': {'energy_factor': 38.6,
                           'co2_emission_factor': 69.9,
                           'ch4_emission_factor': 0.1,
                           'n2o_emission_factor': 0.5
                          },
                'petrol': {'energy_factor': 34.2,
                           'co2_emission_factor': 67.4,
                           'ch4_emission_factor': 0.5,
                           'n2o_emission_factor': 1.8
                           },                
                'avgas':  {'energy_factor': 33.1,
                           'co2_emission_factor': 67.0,
                           'ch4_emission_factor': 0.05,
                           'n2o_emission_factor': 0.7}
                }

FIRE_FACTORS = {'gwp_co2': 1,
                'gwp_ch4': 25,
                'gwp_n2o': 298,
                'ch4_co2_mass_ratio': 7.182e-3,
                'n2o_co2_mass_ratio': 1.329e-5,
                }

def get_fuel_emission_factor(fuel):
    """Get fuel total CO2 emission factor."""
    factors = FUEL_FACTORS[fuel]
    energy_f = factors['energy_factor'] 
    fuel_emission_factor = 0
    for i in ['co2', 'ch4', 'n2o']:
        gas_emission_factor = factors[i + '_emission_factor']
        fuel_emission_factor += energy_f * gas_emission_factor * 1e-3
    return fuel_emission_factor

def fuel_to_co2(fuel, quantity):
    """Get total tCO2 emission by kL of fuel."""
    fuel_emission_factor = get_fuel_emission_factor(fuel)
    return fuel_emission_factor * quantity     


def burned_c_to_co2(tC):
    """Get CH4 and N2O emissions in tCO2 from tonnes of C burned"""
    ch4_emissions = tC * FIRE_FACTORS['gwp_ch4'] * FIRE_FACTORS['ch4_co2_mass_ratio']
    n2o_emissions = tC * FIRE_FACTORS['gwp_n2o'] * FIRE_FACTORS['n2o_co2_mass_ratio']
    return ch4_emissions + n2o_emissions

fuel_emissions = fuel_to_co2('petrol', 2.7395)
fire_emissions = burned_c_to_co2(5.74)
\$\endgroup\$

2 Answers 2

3
\$\begingroup\$

Define a constant for greenhouse gases GHG to avoid hard-coding them in separate functions:

GHG = ('co2', 'ch4', 'n2o')  # greenhouse gases
  • get_fuel_emission_factor function.
    The for loop with accumulation is concisely replaced with builtin sum function on generator expression:

    def get_fuel_emission_factor(fuel):
        """Get fuel total CO2 emission factor."""
        factors = FUEL_FACTORS[fuel]
        return sum(factors['energy_factor'] * factors[f'{gas}_emission_factor'] / 1000
                   for gas in GHG)
    
  • fuel_to_co2 function.
    Inline variable technique can be applied:

    def fuel_to_co2(fuel, quantity):
        """Get total tCO2 emission by kL of fuel."""
        return get_fuel_emission_factor(fuel) * quantity
    
\$\endgroup\$
0
\$\begingroup\$

The biggest issue I see is your functions relying heavily on the structure of the dicts. This may end up not being an issue, for example if these functions are part of a script that you don't expect to change or re-use in future. My suggestion for how you can improve if you wish to is to use TDD as your functions are a perfect fit for TDD. As you cycle through TDD steps you'll improve your design repeatedly. I think it'll be a worthwhile exercise. In the end you can compare the two solutions and judge how they're different. I suggest you put the code out of sight for a while before doing the final comparison.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.