Skip to content

Commit

Permalink
Merge branch 'master' of https://github.com/hgrecco/pint
Browse files Browse the repository at this point in the history
  • Loading branch information
andrewgsavage committed Jun 27, 2024
2 parents 3c9a4da + 62046f0 commit ad02b87
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 8 deletions.
13 changes: 13 additions & 0 deletions docs/advanced/currencies.rst
Original file line number Diff line number Diff line change
Expand Up @@ -84,3 +84,16 @@ currency on its own dimension, and then implement transformations::
More sophisticated formulas, e.g. dealing with flat fees and thresholds, can be
implemented with arbitrary python code by programmatically defining a context (see
:ref:`contexts`).

Currency Symbols
----------------

Many common currency symbols are not supported by the pint parser. A preprocessor can be used as a workaround:

.. doctest::

>>> import pint
>>> ureg = pint.UnitRegistry(preprocessors = [lambda s: s.replace("", "EUR")])
>>> ureg.define("euro = [currency] = € = EUR")
>>> print(ureg.Quantity("1 €"))
1 euro
23 changes: 20 additions & 3 deletions pint/testsuite/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -128,9 +128,26 @@ def requires_numpy_at_least(version):
)


requires_babel = pytest.mark.skipif(
not HAS_BABEL, reason="Requires Babel with units support"
)
def requires_babel(tested_locales=[]):
if not HAS_BABEL:
return pytest.mark.skip("Requires Babel with units support")

import locale

default_locale = locale.getlocale(locale.LC_NUMERIC)
locales_unavailable = False
try:
for loc in tested_locales:
locale.setlocale(locale.LC_NUMERIC, loc)
except locale.Error:
locales_unavailable = True
locale.setlocale(locale.LC_NUMERIC, default_locale)

return pytest.mark.skipif(
locales_unavailable, reason="Tested locales not available."
)


requires_not_babel = pytest.mark.skipif(
HAS_BABEL, reason="Requires Babel not to be installed"
)
Expand Down
8 changes: 4 additions & 4 deletions pint/testsuite/test_babel.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ def test_no_babel(func_registry):
distance.format_babel(locale="fr_FR", length="long")


@helpers.requires_babel()
@helpers.requires_babel(["fr_FR", "ro_RO"])
def test_format(func_registry):
ureg = func_registry
dirname = os.path.dirname(__file__)
Expand All @@ -36,7 +36,7 @@ def test_format(func_registry):
assert mks.format_babel(locale="fr_FR") == "métrique"


@helpers.requires_babel()
@helpers.requires_babel(["fr_FR", "ro_RO"])
def test_registry_locale():
ureg = UnitRegistry(fmt_locale="fr_FR")
dirname = os.path.dirname(__file__)
Expand All @@ -60,7 +60,7 @@ def test_registry_locale():
assert mks.format_babel(locale="fr_FR") == "métrique"


@helpers.requires_babel()
@helpers.requires_babel(["fr_FR"])
def test_unit_format_babel():
ureg = UnitRegistry(fmt_locale="fr_FR")
volume = ureg.Unit("ml")
Expand All @@ -85,7 +85,7 @@ def test_no_registry_locale(func_registry):
distance.format_babel()


@helpers.requires_babel()
@helpers.requires_babel(["fr_FR"])
def test_str(func_registry):
ureg = func_registry
d = 24.1 * ureg.meter
Expand Down
2 changes: 1 addition & 1 deletion pint/testsuite/test_issues.py
Original file line number Diff line number Diff line change
Expand Up @@ -913,7 +913,7 @@ def test_issue1674(self, module_registry):
arr_of_q * q_arr, np.array([Q_(2, "m^2"), Q_(8, "m s")], dtype="object")
)

@helpers.requires_babel()
@helpers.requires_babel(["es_ES"])
def test_issue_1400(self, sess_registry):
q1 = 3.1 * sess_registry.W
q2 = 3.1 * sess_registry.W / sess_registry.cm
Expand Down

0 comments on commit ad02b87

Please sign in to comment.