diff --git a/pyproject.toml b/pyproject.toml index e3d7999..a93c7af 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -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", @@ -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. @@ -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 = [] diff --git a/src/infrasys/component.py b/src/infrasys/component.py index 5d502d4..5627883 100644 --- a/src/infrasys/component.py +++ b/src/infrasys/component.py @@ -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 @@ -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 diff --git a/src/infrasys/system.py b/src/infrasys/system.py index 146e954..6202d51 100644 --- a/src/infrasys/system.py +++ b/src/infrasys/system.py @@ -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: @@ -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)) diff --git a/src/infrasys/utils/json.py b/src/infrasys/utils/json.py deleted file mode 100644 index 4d27f38..0000000 --- a/src/infrasys/utils/json.py +++ /dev/null @@ -1,29 +0,0 @@ -"""JSON utilities""" - -import enum -import json -from datetime import datetime, date, timedelta -from pathlib import Path -from uuid import UUID - - -class ExtendedJSONEncoder(json.JSONEncoder): - """Encodes additional types into JSON format.""" - - def default(self, obj): - if isinstance(obj, (datetime, date)): - return str(obj) - - if isinstance(obj, UUID): - return str(obj) - - if isinstance(obj, timedelta): - return obj.total_seconds() - - if isinstance(obj, enum.Enum): - return obj.value - - if isinstance(obj, Path): - return str(obj) - - return json.JSONEncoder.default(self, obj) diff --git a/tests/test_json.py b/tests/test_json.py deleted file mode 100644 index 674c914..0000000 --- a/tests/test_json.py +++ /dev/null @@ -1,24 +0,0 @@ -import enum -import json -from datetime import datetime, date, timedelta -from pathlib import Path -from uuid import uuid4 - -from infrasys.utils.json import ExtendedJSONEncoder - - -class Fruit(str, enum.Enum): - APPLE = "apple" - ORANGE = "orange" - - -def test_json_encoder(): - data = { - "datetime": datetime.now(), - "date": date.today(), - "timedelta": timedelta(hours=1), - "uuid": uuid4(), - "path": Path(".").absolute(), - "enum": Fruit.APPLE, - } - json.dumps(data, cls=ExtendedJSONEncoder)