[yt-svn] commit/yt: jzuhone: Merged in ngoldbaum/yt (pull request #1948)
commits-noreply at bitbucket.org
commits-noreply at bitbucket.org
Tue Jan 26 11:51:14 PST 2016
1 new commit in yt:
https://bitbucket.org/yt_analysis/yt/commits/f4b8c48fae30/
Changeset: f4b8c48fae30
Branch: yt
User: jzuhone
Date: 2016-01-26 19:51:05+00:00
Summary: Merged in ngoldbaum/yt (pull request #1948)
Implement to_json and from_json for the UnitRegistry object. Closes #1151
Affected #: 2 files
diff -r 3659de2335c886b52105e081a596992cc597bf8d -r f4b8c48fae306ae64e17e5cad882bda4520f077e yt/units/tests/test_units.py
--- a/yt/units/tests/test_units.py
+++ b/yt/units/tests/test_units.py
@@ -25,6 +25,7 @@
from yt.testing import \
fake_random_ds, assert_allclose_units, \
assert_almost_equal
+from yt.units.unit_registry import UnitRegistry
# dimensions
from yt.units.dimensions import \
@@ -474,3 +475,10 @@
test_unit = Unit('cm**-3', base_value=1.0, registry=ds.unit_registry)
assert_equal(test_unit.latex_repr, '\\frac{1}{\\rm{cm}^{3}}')
+
+def test_registry_json():
+ reg = UnitRegistry()
+ json_reg = reg.to_json()
+ unserialized_reg = UnitRegistry.from_json(json_reg)
+
+ assert_equal(reg.lut, unserialized_reg.lut)
diff -r 3659de2335c886b52105e081a596992cc597bf8d -r f4b8c48fae306ae64e17e5cad882bda4520f077e yt/units/unit_registry.py
--- a/yt/units/unit_registry.py
+++ b/yt/units/unit_registry.py
@@ -12,8 +12,22 @@
# The full license is in the file COPYING.txt, distributed with this software.
#-----------------------------------------------------------------------------
+import json
+import re
+
+from distutils.version import LooseVersion
from yt.units.unit_lookup_table import \
default_unit_symbol_lut
+from yt.extern import six
+from sympy import \
+ sympify, \
+ srepr, \
+ __version__ as sympy_version
+
+SYMPY_VERSION = LooseVersion(sympy_version)
+
+def positive_symbol_replacer(match):
+ return match.group().replace(')\')', ')\', positive=True)')
class SymbolNotFoundError(Exception):
pass
@@ -102,3 +116,34 @@
"""
return self.lut.keys()
+
+ def to_json(self):
+ """
+ Returns a json-serialized version of the unit registry
+ """
+ sanitized_lut = {}
+ for k, v in six.iteritems(self.lut):
+ san_v = list(v)
+ repr_dims = srepr(v[1])
+ if SYMPY_VERSION < LooseVersion("1.0.0"):
+ # see https://github.com/sympy/sympy/issues/6131
+ repr_dims = re.sub("Symbol\('\([a-z_]*\)'\)",
+ positive_symbol_replacer, repr_dims)
+ san_v[1] = repr_dims
+ sanitized_lut[k] = tuple(san_v)
+
+ return json.dumps(sanitized_lut)
+
+ @classmethod
+ def from_json(cls, json_text):
+ """
+ Returns a UnitRegistry object from a json-serialized unit registry
+ """
+ data = json.loads(json_text)
+ lut = {}
+ for k, v in six.iteritems(data):
+ unsan_v = list(v)
+ unsan_v[1] = sympify(v[1])
+ lut[k] = tuple(unsan_v)
+
+ return cls(lut=lut, add_default_symbols=False)
Repository URL: https://bitbucket.org/yt_analysis/yt/
--
This is a commit notification from bitbucket.org. You are receiving
this because you have the service enabled, addressing the recipient of
this email.
More information about the yt-svn
mailing list