summaryrefslogtreecommitdiff
path: root/pint/compat.py
diff options
context:
space:
mode:
authorHernan <hernan.grecco@gmail.com>2019-12-21 00:57:21 -0300
committerHernan <hernan.grecco@gmail.com>2019-12-21 00:57:21 -0300
commitdf1d2fb26e99ea742d707b524afffe5c8806ee5a (patch)
tree3064d54f0d05f39a4fd2a333b7361de482dee589 /pint/compat.py
parenta7ec41c14691634b57bd3389b3c7fc1ed114515a (diff)
downloadpint-df1d2fb26e99ea742d707b524afffe5c8806ee5a.tar.gz
Improved babel experience
1. UnitRegistry now has an optional keyword argument (`locale`) that can be used to define the default value for `Quantity.format_babel` locale argument. 2. When Babel is not installed, `Quantity.format_babel` display a nicer and more informative exception. Closes #899, #904
Diffstat (limited to 'pint/compat.py')
-rw-r--r--pint/compat.py16
1 files changed, 15 insertions, 1 deletions
diff --git a/pint/compat.py b/pint/compat.py
index 4ec3633..24fa769 100644
--- a/pint/compat.py
+++ b/pint/compat.py
@@ -13,6 +13,18 @@ from io import BytesIO
from numbers import Number
+def missing_dependency(package, display_name=None):
+ display_name = display_name or package
+
+ def _inner(*args, **kwargs):
+ raise Exception(
+ "This feature requires %s. Please install it by running:\n"
+ "pip install %s" % (display_name, package)
+ )
+
+ return _inner
+
+
def tokenizer(input_string):
for tokinfo in tokenize.tokenize(BytesIO(input_string.encode("utf-8")).readline):
if tokinfo.type != tokenize.ENCODING:
@@ -121,12 +133,14 @@ try:
from babel import Locale as Loc
from babel import units as babel_units
+ babel_parse = Loc.parse
+
HAS_BABEL = hasattr(babel_units, "format_unit")
except ImportError:
HAS_BABEL = False
if not HAS_BABEL:
- Loc = babel_units = None # noqa: F811
+ babel_parse = babel_units = missing_dependency("Babel") # noqa: F811
# Define location of pint.Quantity in NEP-13 type cast hierarchy by defining upcast and
# downcast/wrappable types