Skip to content

Commit

Permalink
Merge #1114
Browse files Browse the repository at this point in the history
1114: Add `__contains__` magic to UnitRegistry r=hgrecco a=clarkgwillison

- [x] Closes #1806
- [x] Executed ``black -t py36 . && isort -rc . && flake8`` with no errors
- [x] The change is fully covered by automated unit tests
- [x] Documented in docs/ as appropriate
- [x] Added an entry to the CHANGES file

This change allows someone to check if a unit (even a prefixed unit) is defined in a `UnitRegistry` using the python `in` keyword like so:

```python
>>> 'MHz' in ureg
True
>>> 'gigatrees' in ureg
False
```

Co-authored-by: Clark Willison <clarkgwillison@gmail.com>
  • Loading branch information
bors[bot] and clarkgwillison authored Jun 16, 2020
2 parents cda2a40 + 82fdd3c commit f8ec2ca
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 1 deletion.
3 changes: 2 additions & 1 deletion CHANGES
Original file line number Diff line number Diff line change
Expand Up @@ -36,13 +36,14 @@ Pint Changelog
(Issue #1108, Thanks Guido Imperiale)
- Fixed crash when some specific combinations of contexts were enabled
(Issue #1112, Thanks Guido Imperiale)
- Added support for checking prefixed units using `in` keyword (Issue #1086)


0.12 (2020-05-29)
-----------------

- Add full support for Decimal and Fraction at the registry level.
**BREAKING CHANGE**:
**BREAKING CHANGE**:
`use_decimal` is deprecated. Use `non_int_type=Decimal` when instantiating
the registry.
- Fixed bug where numpy.pad didn't work without specifying constant_values or
Expand Down
14 changes: 14 additions & 0 deletions docs/defining.rst
Original file line number Diff line number Diff line change
Expand Up @@ -152,3 +152,17 @@ leading underscore:
>>> ureg.define('mpg = 1 * mile / gallon')
>>> fuel_ec_europe = 5 * ureg.L / ureg._100km
>>> fuel_ec_us = (1 / fuel_ec_europe).to(ureg.mpg)


Checking if a unit is already defined
-------------------------------------

The python ``in`` keyword works as expected with unit registries. Check if
a unit has been defined with the following:

.. doctest::

>>> 'MHz' in ureg
True
>>> 'gigatrees' in ureg
False
9 changes: 9 additions & 0 deletions pint/registry.py
Original file line number Diff line number Diff line change
Expand Up @@ -289,6 +289,15 @@ def __getitem__(self, item):
)
return self.parse_expression(item)

def __contains__(self, item):
"""Support checking prefixed units with the `in` operator
"""
try:
self.__getattr__(item)
return True
except UndefinedUnitError:
return False

def __dir__(self):
#: Calling dir(registry) gives all units, methods, and attributes.
#: Also used for autocompletion in IPython.
Expand Down
11 changes: 11 additions & 0 deletions pint/testsuite/test_issues.py
Original file line number Diff line number Diff line change
Expand Up @@ -702,6 +702,17 @@ def test_issue1062_issue1097(self):
q = ureg.Quantity(1, "nm")
q.to("J")

def test_issue1086(self):
# units with prefixes should correctly test as 'in' the registry
assert "bits" in ureg
assert "gigabits" in ureg
assert "meters" in ureg
assert "kilometers" in ureg
# unknown or incorrect units should test as 'not in' the registry
assert "magicbits" not in ureg
assert "unknownmeters" not in ureg
assert "gigatrees" not in ureg

def test_issue1112(self):
ureg = UnitRegistry(
"""
Expand Down

0 comments on commit f8ec2ca

Please sign in to comment.