Skip to content

Commit

Permalink
Merge pull request #20 from NREL/bugfix/model_dump_custom
Browse files Browse the repository at this point in the history
fixed a bug in model_dump_custom
  • Loading branch information
KapilDuwadi committed Apr 22, 2024
2 parents a25f38b + 8c508fb commit 7a6c18f
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 76 deletions.
38 changes: 18 additions & 20 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,27 +4,27 @@ build-backend = "hatchling.build"

[project]
name = "infrasys"
version = "0.0.2"
version = "0.0.3"
description = ''
readme = "README.md"
requires-python = ">=3.10, <3.13"
license = "BSD-3-Clause"
keywords = []
authors = [
{ name = "Aadil Latif", email = "aadil.latif@nrel.gov" },
{ name = "Daniel Thom", email = "daniel.thom@nrel.gov" },
{ name = "Kapil Duwadi", email = "kapil.duwadi@nrel.gov" },
{ name = "Pedro Andres Sanchez Perez", email = "pedroandres.sanchezperez@nrel.gov" },
{ name = "Tarek Elgindy", email = "tarek.elgindy@nrel.gov" },
{ name = "Aadil Latif", email = "aadil.latif@nrel.gov" },
{ name = "Daniel Thom", email = "daniel.thom@nrel.gov" },
{ name = "Kapil Duwadi", email = "kapil.duwadi@nrel.gov" },
{ name = "Pedro Andres Sanchez Perez", email = "pedroandres.sanchezperez@nrel.gov" },
{ name = "Tarek Elgindy", email = "tarek.elgindy@nrel.gov" },
]
classifiers = [
"Development Status :: 4 - Beta",
"Programming Language :: Python",
"Programming Language :: Python :: 3.10",
"Programming Language :: Python :: 3.11",
"Programming Language :: Python :: 3.12",
"Programming Language :: Python :: Implementation :: CPython",
"Programming Language :: Python :: Implementation :: PyPy",
"Development Status :: 4 - Beta",
"Programming Language :: Python",
"Programming Language :: Python :: 3.10",
"Programming Language :: Python :: 3.11",
"Programming Language :: Python :: 3.12",
"Programming Language :: Python :: Implementation :: CPython",
"Programming Language :: Python :: Implementation :: PyPy",
]
dependencies = [
"numpy~=1.26.4",
Expand Down Expand Up @@ -60,9 +60,7 @@ Source = "https://github.com/NREL/infrasys"
pythonpath = "src"
minversion = "6.0"
addopts = "-ra"
testpaths = [
"tests",
]
testpaths = ["tests"]

[tool.ruff]
# Exclude a variety of commonly ignored directories.
Expand All @@ -85,12 +83,12 @@ target-version = "py311"
[tool.ruff.lint]
# Enable Pyflakes (`F`) and a subset of the pycodestyle (`E`) codes by default.
select = [
"C901", # McCabe complexity
"E4", # Subset of pycodestyle (E)
"C901", # McCabe complexity
"E4", # Subset of pycodestyle (E)
"E7",
"E9",
"F", # Pyflakes
"W", # pycodestyle warnings
"F", # Pyflakes
"W", # pycodestyle warnings
]
ignore = []

Expand Down
8 changes: 7 additions & 1 deletion src/infrasys/component.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,11 @@ def check_component_addition(self) -> None:

def model_dump_custom(self, *args, **kwargs) -> dict[str, Any]:
"""Custom serialization for this package"""
refs = {x: self._model_dump_field(x) for x in self.model_fields}
refs = {}
for x in self.model_fields:
val = self._model_dump_field(x)
if val is not None:
refs[x] = val
exclude = kwargs.get("exclude", [])
exclude += list(set(exclude).union(refs))
kwargs["exclude"] = exclude
Expand All @@ -52,6 +56,8 @@ def _model_dump_field(self, field) -> Any:
),
).model_dump()
val = data
else:
val = None
# TODO: other composite types may need handling.
# Parent packages can always implement a field_serializer themselves.
return val
Expand Down
3 changes: 1 addition & 2 deletions src/infrasys/system.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@
)
from infrasys.time_series_manager import TimeSeriesManager, TIME_SERIES_KWARGS
from infrasys.time_series_models import SingleTimeSeries, TimeSeriesData, TimeSeriesMetadata
from infrasys.utils.json import ExtendedJSONEncoder


class System:
Expand Down Expand Up @@ -157,7 +156,7 @@ def to_json(self, filename: Path | str, overwrite=False, indent=None, data=None)
raise ISConflictingArguments("data contains the key 'system'")
data["system"] = system_data
with open(filename, "w", encoding="utf-8") as f_out:
json.dump(data, f_out, indent=indent, cls=ExtendedJSONEncoder)
json.dump(data, f_out, indent=indent)
logger.info("Wrote system data to {}", filename)

self._time_series_mgr.serialize(self._make_time_series_directory(filename))
Expand Down
29 changes: 0 additions & 29 deletions src/infrasys/utils/json.py

This file was deleted.

24 changes: 0 additions & 24 deletions tests/test_json.py

This file was deleted.

0 comments on commit 7a6c18f

Please sign in to comment.