Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

populate dimensions for restored json unit registries from dimensions namespace #81

Merged
merged 1 commit into from
Mar 27, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 12 additions & 1 deletion unyt/tests/test_unit_registry.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

import pytest

from unyt.dimensions import length
from unyt.dimensions import length, energy
from unyt.exceptions import SymbolNotFoundError, UnitParseError
from unyt.unit_object import Unit
from unyt.unit_registry import UnitRegistry
Expand Down Expand Up @@ -74,3 +74,14 @@ def test_registry_contains():
assert "Merg" in ureg
assert "foobar" not in ureg
assert Unit("m", registry=ureg) in ureg


def test_registry_json():
reg = UnitRegistry()
json_reg = reg.to_json()
unserialized_reg = UnitRegistry.from_json(json_reg)

assert reg.lut == unserialized_reg.lut

assert reg.lut["m"][1] is length
assert reg.lut["erg"][1] is energy
8 changes: 0 additions & 8 deletions unyt/tests/test_units.py
Original file line number Diff line number Diff line change
Expand Up @@ -545,14 +545,6 @@ def test_latitude_longitude():
assert_equal((lon * 180.0).in_units("deg"), deg * 360)


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)


def test_creation_from_ytarray():
from unyt import electrostatic_unit, elementary_charge_cgs

Expand Down
7 changes: 4 additions & 3 deletions unyt/unit_registry.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,12 @@

import json

from unyt import dimensions as unyt_dims
from unyt.exceptions import SymbolNotFoundError, UnitParseError
from unyt._unit_lookup_table import default_unit_symbol_lut, unit_prefixes
from unyt.unit_systems import mks_unit_system, _split_prefix, unit_system_registry
from hashlib import md5
from sympy import sympify, srepr
from sympy import sympify


def _sanitize_unit_system(unit_system, obj):
Expand Down Expand Up @@ -228,7 +229,7 @@ def to_json(self):
sanitized_lut = {}
for k, v in self.lut.items():
san_v = list(v)
repr_dims = srepr(v[1])
repr_dims = str(v[1])
san_v[1] = repr_dims
sanitized_lut[k] = tuple(san_v)

Expand All @@ -249,7 +250,7 @@ def from_json(cls, json_text):
lut = {}
for k, v in data.items():
unsan_v = list(v)
unsan_v[1] = sympify(v[1])
unsan_v[1] = sympify(v[1], locals=vars(unyt_dims))
lut[k] = tuple(unsan_v)

return cls(lut=lut, add_default_symbols=False)
Expand Down