recipe_grid.units: Unit conversions

Recipe Grid has a simple understanding of various cooking-related units and can perform simple conversions between units of the same kind. The complete list of units is:

  • Mass:

    • g, gram, grams

    • kg, kilo, kilos, kilogram, kilograms

    • lb, lbs, pound, pounds

    • oz, ozs, ounce, ounces

  • Volume:

    • l, litre

    • ml, mill, mills, milliliter, milliliters

    • tsp, tsps, teaspoons, teaspoon, tea spoon, tea spoons

    • tbsp, tbsps, tablespoon, tablespoons, table spoon, table spoons

    • cup, cups

    • pint, pints

  • Clove:

    • clove, cloves

  • Bulb:

    • bulb, bulbs

  • Can:

    • can, cans, tin, tins

  • Pinch:

    • pinch, pinches

  • Knob:

    • knob, knobs

  • Packet:

    • packet, packets, pack, packs

  • Box:

    • box, boxes, boxen

  • Bag:

    • bag, bags

  • Sack:

    • sack, sacks

  • Sachet:

    • sachet, sachets

  • Rasher:

    • rasher, rashers

  • Strip:

    • strip, strips

Conversions are possible betweeen units in the same sublist and names listed together are aliases for one another.

Note

This system is much less sophisticated than things like pint but consequently easier to understand and use for non-unit-conversion-related tasks and also for less conventional measures (e.g. ‘cloves’).

Internal API

The Unit system is available in the recipe_grid.units.UNIT_SYSTEM which is an instance of the following class:

class recipe_grid.units.UnitSystem(unit_sets: Mapping[str, RelatedUnitSet])

A container of unit conversions.

iter_names() Iterator[str]

Iterate over all defined unit names and aliases.

iter_conversions_from(from_unit: str) Iterator[Tuple[int | float | Fraction, str]]

Iterate over all possible conversions from the provided unit to all other compatible units. Iterates over pairs of multiplicative conversion factor and other unit names.

convert_between(from_unit: str, to_unit: str) int | float | Fraction

Find the multiplicative conversion factor to convert from one unit to another.