Added to_tuple and from_tuple method to Quantity

This facilitate serializing and deserializing. If you have two function: serialize and deserialize,
then your code will be as simple as this

>>> serialized = serialize(q1.to_tuple())
>>> q2 = ureg.Quantity.from_tuple(deserialize(serialized))
This commit is contained in:
Hernan Grecco
2016-01-07 20:02:23 -03:00
parent 1ea51ca9b3
commit a837f1f003

View File

@@ -209,6 +209,13 @@ class _Quantity(SharedRegistryObject):
return self._dimensionality
@classmethod
def from_tuple(cls, tup):
return cls(tup[0], UnitsContainer(tup[1]))
def to_tuple(self):
return self.m, tuple(self._units.items())
def compatible_units(self, *contexts):
if contexts:
with self._REGISTRY.context(*contexts):