-
Notifications
You must be signed in to change notification settings - Fork 85
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
135 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
# BSD 3-Clause License; see https://github.com/scikit-hep/awkward-1.0/blob/main/LICENSE | ||
|
||
from __future__ import annotations | ||
|
||
from threading import RLock | ||
|
||
_lock = RLock() | ||
_unit_registry = None | ||
_checked_for_pint = False | ||
|
||
|
||
def set_unit_registry(registry): | ||
global _unit_registry | ||
with _lock: | ||
_unit_registry = registry | ||
|
||
|
||
def get_unit_registry(): | ||
with _lock: | ||
_register_if_available() | ||
return _unit_registry | ||
|
||
|
||
def _register_if_available(): | ||
global _checked_for_pint, _unit_registry | ||
with _lock: | ||
if _checked_for_pint: | ||
return | ||
|
||
try: | ||
import pint | ||
except ModuleNotFoundError: | ||
return | ||
else: | ||
_unit_registry = pint.UnitRegistry() | ||
finally: | ||
_checked_for_pint = True | ||
|
||
|
||
def register_and_check(): | ||
""" | ||
Build `pint` unit registry | ||
""" | ||
try: | ||
import pint # noqa: F401 | ||
|
||
except ModuleNotFoundError: | ||
raise ModuleNotFoundError( | ||
"""install the 'pint' package with: | ||
python3 -m pip install pint | ||
or | ||
conda install -c conda-forge pint | ||
""" | ||
) from None | ||
|
||
_register_if_available() |