Python parser & interface for UCUM (Unified Code for Units of Measure).
Maintained. Last release 3 weeks ago, commits in 6 of the last 26 weeks. Weaker on contributor spread.
pip install ucumvertEasier access to UCUM from Python
Feedback welcome! Currently only the conversion direction from UCM to pint is supported. Please review the definitions before you trust them. While we have many tests in place and reviewed the mappings carefully, bugs may still be present.
UCUM (Unified Code for Units of Measure) is a code system intended to cover all units of measures.
It provides a formalism to express units in an unambiguous way suitable for electronic communication.
Note that UCUM does not provide a canonical representation, e.g. m/s and m.s-1 are expressing the same unit in two ways.
ucumvert is a pip-installable Python package. Features:
- Parser for UCUM unit strings that implements the full grammar.
- Converter for creating pint units from UCUM unit strings.
- A pint unit definition file pint_ucum_defs.txt that extends pint´s default units with UCUM units. All UCUM units from the new version 2.2 of the specification (June 2024) are included.
ucumvert generates the UCUM grammar by filling a template with unit codes, prefixes etc. from the official ucum-essence.xml file (a copy is included in this repo). So updating the parser for new UCUM releases is straight forward. The parser is built with the great lark parser toolkit. The generated lark grammar file for case-sensitive UCUM codes is included in the repository, see ucum_grammar.lark.
Some of the UCUM unit atoms are invalid unit names in pint, for example cal_[15], m[H2O], 10*, [in_i'H2O].
For all of them we define mappings to valid pint unit names in ucum_pint.py, e.g. .
