API Reference¶
Quantity¶
- class energyunits.Quantity(value, unit, substance=None, basis=None, reference_year=None)[source]¶
Bases:
objectPhysical quantity with value, unit, and optional metadata.
Supports: - Unit conversions (e.g., MWh to GJ) - Substance-based conversions (e.g., coal mass to CO2 emissions) - Heating value basis conversions (HHV/LHV) - Inflation adjustments for cost quantities - Arithmetic operations with dimensional analysis
- Parameters:
- classmethod list_units(dimension=None)[source]¶
List all available units, optionally filtered by dimension.
- Parameters:
dimension – Filter by dimension (e.g., “ENERGY”, “POWER”, “MASS”).
- Returns:
Sorted list of unit strings.
Examples
Quantity.list_units() # All units Quantity.list_units(“ENERGY”) # Energy units only
- classmethod list_dimensions()[source]¶
List all available dimensions.
- Returns:
Sorted list of dimension strings.
- classmethod list_substances(has_property=None)[source]¶
List all available substances.
- Parameters:
has_property – Filter to substances with this property (e.g., “hhv”).
- Returns:
Sorted list of substance names.
Examples
Quantity.list_substances() # All substances Quantity.list_substances(“density”) # Substances with density
- classmethod list_currencies()[source]¶
List all available currencies.
- Returns:
List of currency codes.
- to(target_unit=None, basis=None, substance=None, reference_year=None)[source]¶
Convert to another unit, basis, substance, and/or reference year.
This is the unified conversion method. Conversions are applied in sequence: substance → basis → unit → inflation.
Special case: When combining currency conversion with year change: - Order becomes: substance → basis → inflation → unit - Uses “inflate first, then convert” convention - Issues warning about economic assumptions
Examples
energy = Quantity(100, “MWh”) energy.to(“GJ”) # → 360 GJ
coal = Quantity(1000, “kg”, “coal”) coal.to(“MWh”) # Coal mass → energy content coal.to(“kg”, substance=”CO2”) # Coal mass → CO2 emissions
capex_2020 = Quantity(1000, “USD/kW”, reference_year=2020) capex_2025 = capex_2020.to(reference_year=2025)
cost_eur_2015 = Quantity(50, “EUR/MWh”, reference_year=2015) cost_usd_2024 = cost_eur_2015.to(“USD/MWh”, reference_year=2024) # Inflates EUR 2015→2024, then converts using 2024 exchange rate
Unit Registry¶
- class energyunits.registry.UnitRegistry[source]¶
Bases:
objectRegistry of units, dimensions, and conversion factors.
- get_conversion_factor(from_unit, to_unit)[source]¶
Get conversion factor between compatible units (cached for performance).
- convert_between_dimensions(value, from_unit, to_unit, substance=None, **kwargs)[source]¶
Convert between related dimensions.
- get_corresponding_unit(unit, target_dimension)[source]¶
Get corresponding unit in another dimension (e.g., MW → MWh).
- get_multiplication_result(dim1, dim2)[source]¶
Get result dimension and source dimension from multiplying two dimensions.
Returns: (result_dimension, source_dimension) or None Example: (POWER, TIME) -> (“ENERGY”, “POWER”)
- get_division_result(numerator_dim, denominator_dim)[source]¶
Get result dimension from dividing two dimensions.
Returns: result_dimension or None Example: (ENERGY, TIME) -> “POWER”
Substance Registry¶
- class energyunits.substance.SubstanceRegistry[source]¶
Bases:
objectRegistry of substances and their properties.
- load_substances(file_path)[source]¶
Load custom substances from JSON file.
- Parameters:
file_path (str)
- list_substances(has_property=None)[source]¶
List all available substances, optionally filtered by property.
- Parameters:
has_property – Filter to substances that have this property defined (e.g., “hhv”, “density”, “carbon_content”).
- Returns:
Sorted list of substance names.
Inflation¶
- class energyunits.inflation.InflationRegistry[source]¶
Bases:
objectRegistry for inflation data with historical rates.
- load_inflation(file_path)[source]¶
Load custom inflation data from JSON file.
- Parameters:
file_path (str)
Exchange Rates¶
- class energyunits.exchange_rate.ExchangeRateRegistry[source]¶
Bases:
objectRegistry for historical exchange rate data.
Provides year-dependent exchange rates for currency conversions. All rates are expressed as: 1 unit of currency = X USD.
- load_exchange_rates(file_path)[source]¶
Load custom exchange rate data from JSON file.
- Parameters:
file_path (str)
- get_exchange_rate(currency, year=None)[source]¶
Get exchange rate for a currency in a specific year.
- Parameters:
- Returns:
Exchange rate (1 currency unit = X USD)
- Raises:
ValueError – If currency not supported or year not available
- Return type:
- convert_currency(value, from_currency, to_currency, year=None)[source]¶
Convert amount from one currency to another using rates from a specific year.
- get_conversion_factor(from_currency, to_currency, year=None)[source]¶
Get conversion factor between two currencies for a specific year.
Returns the factor such that: amount_to = amount_from * factor
Unit Constants¶
Unit constants for IDE-friendly autocompletion.
Instead of using raw strings, you can import unit constants for better IDE support and typo prevention:
from energyunits.units import MWh, GJ, MW, USD, EUR
energy = Quantity(100, MWh) energy.to(GJ)
These are simple string constants — they work identically to passing the string directly.