From 10ac8f45ebf89e9ae1f7589c46b85d05e84e0d06 Mon Sep 17 00:00:00 2001 From: Steven Shi Date: Tue, 5 Nov 2024 11:24:45 -0600 Subject: [PATCH] Migrate the PyYAML update from FASTsim --- pyproject.toml | 1 + python/altrios/__init__.py | 13 +++++++++---- 2 files changed, 10 insertions(+), 4 deletions(-) diff --git a/pyproject.toml b/pyproject.toml index 6237d609..0034ef7b 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -46,6 +46,7 @@ dependencies = [ "polars==0.20.25", "pyarrow", "requests", + "PyYAML==6.0.2", ] [project.urls] diff --git a/python/altrios/__init__.py b/python/altrios/__init__.py index b221f2cc..d048f235 100644 --- a/python/altrios/__init__.py +++ b/python/altrios/__init__.py @@ -125,16 +125,21 @@ def to_pydict(self) -> Dict: """ Returns self converted to pure python dictionary with no nested Rust objects """ - import json - return json.loads(self.to_json()) + from yaml import load + try: + from yaml import CLoader as Loader + except ImportError: + from yaml import Loader + pydict = load(self.to_yaml(), Loader = Loader) + return pydict @classmethod def from_pydict(cls, pydict: Dict) -> Self: """ Instantiates Self from pure python dictionary """ - import json - return cls.from_json(json.dumps(pydict)) + import yaml + return cls.from_yaml(yaml.dump(pydict),skip_init=False) def to_dataframe(self, pandas:bool=False) -> [pd.DataFrame, pl.DataFrame, pl.LazyFrame]: """