summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMartin Teichmann <martin.teichmann@xfel.eu>2017-12-05 10:14:47 +0100
committerMartin Teichmann <martin.teichmann@xfel.eu>2017-12-05 10:14:47 +0100
commit32c2a59b6238885b8fad2d9845555e5e43270509 (patch)
tree0d372703a20499733f88f76f0b7e25cd6009624e
parent005f707f26e611fe38eacecfb801f24dcadda95e (diff)
downloadpint-32c2a59b6238885b8fad2d9845555e5e43270509.tar.gz
factor out common IPython code
-rw-r--r--pint/quantity.py23
-rw-r--r--pint/unit.py18
-rw-r--r--pint/util.py23
3 files changed, 28 insertions, 36 deletions
diff --git a/pint/quantity.py b/pint/quantity.py
index 85656ed..e3f384f 100644
--- a/pint/quantity.py
+++ b/pint/quantity.py
@@ -25,7 +25,7 @@ from .errors import (DimensionalityError, OffsetUnitCalculusError,
UndefinedUnitError)
from .definitions import UnitDefinition
from .compat import string_types, ndarray, np, _to_magnitude, long_type
-from .util import (logger, UnitsContainer, SharedRegistryObject,
+from .util import (PrettyIPython, logger, UnitsContainer, SharedRegistryObject,
to_units_container, infer_base_unit,
fix_str_conversions)
from pint.compat import Loc
@@ -66,7 +66,7 @@ def ireduce_dimensions(f):
@fix_str_conversions
-class _Quantity(SharedRegistryObject):
+class _Quantity(PrettyIPython, SharedRegistryObject):
"""Implements a class to describe a physical quantity:
the product of a numerical value and a unit of measurement.
@@ -225,25 +225,6 @@ class _Quantity(SharedRegistryObject):
format(obj.magnitude, remove_custom_flags(spec)),
obj.units.format_babel(spec, **kwspec)).replace('\n', '')
- # IPython related code
- def _repr_html_(self):
- if "~" in self.default_format:
- return "{:~H}".format(self)
- else:
- return "{:H}".format(self)
-
- def _repr_latex_(self):
- if "~" in self.default_format:
- return "${:~L}$".format(self)
- else:
- return "${:L}$".format(self)
-
- def _repr_pretty_(self, p, cycle):
- if "~" in self.default_format:
- p.text("{:~P}".format(self))
- else:
- p.text("{:P}".format(self))
-
@property
def magnitude(self):
"""Quantity's magnitude. Long form for `m`
diff --git a/pint/unit.py b/pint/unit.py
index c8a3495..4f8c35e 100644
--- a/pint/unit.py
+++ b/pint/unit.py
@@ -15,7 +15,8 @@ import copy
import operator
from numbers import Number
-from .util import UnitsContainer, SharedRegistryObject, fix_str_conversions
+from .util import (
+ PrettyIPython, UnitsContainer, SharedRegistryObject, fix_str_conversions)
from .compat import string_types, NUMERIC_TYPES, long_type
from .formatting import siunitx_format_unit
@@ -23,7 +24,7 @@ from .definitions import UnitDefinition
@fix_str_conversions
-class _Unit(SharedRegistryObject):
+class _Unit(PrettyIPython, SharedRegistryObject):
"""Implements a class to describe a unit supporting math operations.
:type units: UnitsContainer, str, Unit or Quantity.
@@ -110,19 +111,6 @@ class _Unit(SharedRegistryObject):
return '%s' % (units.format_babel(spec, **kwspec))
- # IPython related code
- def _repr_html_(self):
- if "~" in self.default_format:
- return "{:~H}".format(self)
- else:
- return "{:H}".format(self)
-
- def _repr_latex_(self):
- if "~" in self.default_format:
- return "${:~L}$".format(self)
- else:
- return "${:L}$".format(self)
-
@property
def dimensionless(self):
"""Return true if the Unit is dimensionless.
diff --git a/pint/util.py b/pint/util.py
index df9a12f..a4b089c 100644
--- a/pint/util.py
+++ b/pint/util.py
@@ -625,6 +625,29 @@ class SharedRegistryObject(object):
else:
return False
+
+class PrettyIPython(object):
+ """Mixin to add pretty-printers for IPython"""
+
+ def _repr_html_(self):
+ if "~" in self.default_format:
+ return "{:~H}".format(self)
+ else:
+ return "{:H}".format(self)
+
+ def _repr_latex_(self):
+ if "~" in self.default_format:
+ return "${:~L}$".format(self)
+ else:
+ return "${:L}$".format(self)
+
+ def _repr_pretty_(self, p, cycle):
+ if "~" in self.default_format:
+ p.text("{:~P}".format(self))
+ else:
+ p.text("{:P}".format(self))
+
+
def to_units_container(unit_like, registry=None):
""" Convert a unit compatible type to a UnitsContainer.