Skip to content

Commit

Permalink
json fails comparison, which may or may not be problematic
Browse files Browse the repository at this point in the history
  • Loading branch information
calbaker committed Jan 6, 2025
1 parent 1bac9bc commit b950233
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 2 deletions.
8 changes: 7 additions & 1 deletion python/fastsim/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -236,7 +236,7 @@ def history_path_list(self, element_as_list: bool = False) -> List[str]:
'yaml',
'msg_pack',
# 'toml',
# 'json',
'json',
]


Expand All @@ -260,6 +260,9 @@ def to_pydict(self, flatten: bool = False, data_fmt: str = "msg_pack") -> Dict:
except ImportError:
from yaml import Loader
pydict = load(self.to_yaml(), Loader=Loader)
case "json":
from json import loads
pydict = loads(self.to_json())

if not flatten:
return pydict
Expand Down Expand Up @@ -289,6 +292,9 @@ def from_pydict(cls, pydict: Dict, data_fmt: str = "msg_pack") -> Self:
print(
f"{err}\nThis is a known bug in interactive python sessions. Reverting to YAML.")
obj = cls.from_pydict(pydict, data_fmt="yaml")
case "json":
from json import dumps
obj = cls.from_json(dumps(pydict))

return obj

Expand Down
11 changes: 10 additions & 1 deletion python/fastsim/tests/test_serde.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,11 +41,20 @@ def test_pydict():
t1 = time.perf_counter_ns()
t_yaml = t1 - t0
print(f"Elapsed time for YAML: {t_yaml:.3e} ns ")

print(f"YAML time per MessagePack time: {(t_yaml / t_msg):.3e} ")

t0 = time.perf_counter_ns()
sd_dict_json = sd.to_pydict(flatten=False, data_fmt="json")
sd_json = fsim.SimDrive.from_pydict(sd_dict_json, data_fmt="json")
t1 = time.perf_counter_ns()
t_json = t1 - t0
print(f"Elapsed time for json: {t_json:.3e} ns ")
print(f"JSON time per MessagePack time: {(t_json / t_msg):.3e} ")

assert sd_msg == sd
assert sd_yaml == sd
# TODO: uncomment and investigate
# assert sd_json == sd

def test_dataframe():
get_solved_sd().to_dataframe()

0 comments on commit b950233

Please sign in to comment.