Skip to content

Units

This page contains the API for the physical units, accessed via the physipy.units dict (e.g. physipy.units["m"]).

_units

Define unit dictionnaries.

This module defines dictionnaries of units
  • SI_units : all the seven base SI units
  • SI_units_prefixed : same, with the prefixed version
  • SI_derived_units : other accepted units
  • SI_derived_units_prefixed : same, with the prefixed version
  • other_units : other various units
  • units : all of the above
TODO
  • [ ] : should 'SI_units_derived' have another name ?
  • [X] : deal with prefixed kg
  • [ ] : add other dicts of units : imperial, astrophys
  • [ ] : add scipy.constants.physical_constants units
Questions
  • should the definition of other packages units be fixed or relative ?
  • should other packages units/constants be in the same dict ?
  • should make a proper data structure, where a new unit added is checked if already taken ?

Define non-SI-units.

This module provides for now 2 dictionnaries of units
  • custom_units : for user-defined units
  • imperial_units : retard units
TODO
  • create a function wrapper for dict creation ?
  • Should custom units and constants be in the same module ?

prefix_units

prefix_units(
    prefix_dic: dict, unit_dict: dict, extend: bool = False
) -> dict

Return a dict of unit with all combination between the input unit dict and the prefix dict.

Parameters

prefix_dic : dict Dict with keys the string representation of a prefix, and values the corresponding value. unit_dict : dict Dict with keys the string of units, and value the corresponding Quantity. extend : bool Weither to extend the input unit dict, or return the prefixed units in a separate dict

Returns

dict Dict with keys the string of prefixed units, and values the corresponding Quantity. Extends the input unit_dict if extend is True.

Source code in physipy/quantity/_units.py
def prefix_units(
    prefix_dic: dict, unit_dict: dict, extend: bool = False
) -> dict:
    """Return a dict of unit with all combination between the input unit dict and the prefix dict.

    Parameters
    ----------
    prefix_dic : dict
        Dict with keys the string representation of a prefix, and values the corresponding value.
    unit_dict : dict
        Dict with keys the string of units, and value the corresponding Quantity.
    extend : bool
        Weither to extend the input unit dict, or return the prefixed units in a separate dict

    Returns
    -------
    dict
        Dict with keys the string of prefixed units, and values the corresponding
        Quantity. Extends the input unit_dict if extend is True.
    """
    prefixed_dict = {}
    for prefix_symbol, prefix_value in prefix_dic.items():
        for unit_symbol, unit_quantity in unit_dict.items():
            prefixed_symbol = prefix_symbol + str(unit_quantity.symbol)
            prefixed_dict[prefixed_symbol] = make_quantity(
                prefix_value * unit_quantity, symbol=prefixed_symbol
            )
    return prefixed_dict if not extend else {**unit_dict, **prefix_dic}