Skip to content

Commit

Permalink
Use get_application_registry when possible
Browse files Browse the repository at this point in the history
  • Loading branch information
dalito committed Jan 15, 2024
1 parent 6c48db5 commit eebc9e7
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 9 deletions.
Binary file modified parse_tree.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
21 changes: 12 additions & 9 deletions src/ucumvert/ucum_pint.py
Original file line number Diff line number Diff line change
Expand Up @@ -104,11 +104,12 @@
class UcumToPintTransformer(Transformer):
def __init__(self, ureg=None):
if ureg is None:
self.ureg = pint.UnitRegistry(on_redefinition="raise")
# Append the local definitions for ucum units to the default registry
self.ureg.load_definitions(
Path(__file__).resolve().parent / "pint_ucum_defs.txt"
)
self.ureg = pint.get_application_registry()
# Append definitions for ucum units to the registry if not already done.
if not hasattr(ureg, "peripheral_vascular_resistance_unit"):
self.ureg.load_definitions(
Path(__file__).resolve().parent / "pint_ucum_defs.txt"
)
else:
self.ureg = ureg

Expand Down Expand Up @@ -216,7 +217,7 @@ def ucum_preprocessor(unit_input):
Usage:
>>> import pint
>>> ureg = pint.UnitRegistry()
>>> ureg = pint.get_application_registry()
>>> ureg.preprocessors.append(ucum_preprocessor)
"""
ucum_parser = get_ucum_parser()
Expand Down Expand Up @@ -334,9 +335,11 @@ def find_matching_pint_definitions(report_file: Path | None = None) -> None:
def get_pint_registry(ureg=None):
"""Return a pint registry with the UCUM definitions loaded."""
if ureg is None:
ureg = pint.UnitRegistry(on_redefinition="raise")
ureg.preprocessors.append(ucum_preprocessor)
ureg.load_definitions(Path(__file__).resolve().parent / "pint_ucum_defs.txt")
ureg = pint.get_application_registry()
if not hasattr(ureg, "peripheral_vascular_resistance_unit"): # UCUM already loaded?
ureg.load_definitions(Path(__file__).resolve().parent / "pint_ucum_defs.txt")
if ucum_preprocessor not in ureg.preprocessors:
ureg.preprocessors.append(ucum_preprocessor)
return ureg


Expand Down

0 comments on commit eebc9e7

Please sign in to comment.