Skip to content

Commit

Permalink
Merge pull request #98 from NREL/PyYaml_Update
Browse files Browse the repository at this point in the history
Migrated the PyYAML update from FASTsim
  • Loading branch information
calbaker authored Nov 5, 2024
2 parents 49381bd + 10ac8f4 commit b65fee3
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 4 deletions.
1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ dependencies = [
"polars==0.20.25",
"pyarrow",
"requests",
"PyYAML==6.0.2",
]

[project.urls]
Expand Down
13 changes: 9 additions & 4 deletions python/altrios/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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]:
"""
Expand Down

0 comments on commit b65fee3

Please sign in to comment.