Skip to content

Commit

Permalink
Update quantity repr
Browse files Browse the repository at this point in the history
- Update CHANGES
- Add test for repr
- Update repr function to include magnitude repr & units
  • Loading branch information
jules-ch committed Sep 18, 2022
1 parent 8125d93 commit 3e393ff
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 5 deletions.
2 changes: 2 additions & 0 deletions CHANGES
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ Pint Changelog
(Issue #1030, #574)
- Added angular frequency documentation page.
- Move ASV benchmarks to dedicated folder. (Issue #1542)
- Change Quantity repr to include magnitude repr & units.


0.19.2 (2022-04-23)
-------------------
Expand Down
5 changes: 1 addition & 4 deletions pint/facets/plain/quantity.py
Original file line number Diff line number Diff line change
Expand Up @@ -267,10 +267,7 @@ def __bytes__(self) -> bytes:
return str(self).encode(locale.getpreferredencoding())

def __repr__(self) -> str:
if isinstance(self._magnitude, float):
return f"<Quantity({self._magnitude:.9}, '{self._units}')>"
else:
return f"<Quantity({self._magnitude}, '{self._units}')>"
return f"<Quantity({self._magnitude!r}, units='{self._units}')>"

def __hash__(self) -> int:
self_base = self.to_base_units()
Expand Down
13 changes: 12 additions & 1 deletion pint/testsuite/test_quantity.py
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,18 @@ def test_quantity_comparison_convert(self):
def test_quantity_repr(self):
x = self.Q_(4.2, UnitsContainer(meter=1))
assert str(x) == "4.2 meter"
assert repr(x) == "<Quantity(4.2, 'meter')>"
assert repr(x) == "<Quantity(4.2, units='meter')>"
x=self.Q_(2.9325156461464651456456545649)
assert repr(x) == "<Quantity(2.932515646146465, units='dimensionless')>"

@helpers.requires_numpy
def test_quantity_repr_numpy(self):
x = self.Q_(4.2)
assert repr(x) == "<Quantity(4.2, units='dimensionless')>"
x = self.Q_(np.array(2.))
assert repr(x) == "<Quantity(array(2.), units='dimensionless')>"
x = self.Q_(np.array(2., dtype=np.float32))
assert repr(x) == "<Quantity(array(2., dtype=float32), units='dimensionless')>"

def test_quantity_hash(self):
x = self.Q_(4.2, "meter")
Expand Down

0 comments on commit 3e393ff

Please sign in to comment.