Skip to content

Commit

Permalink
rip out json_encoder since it iss being deprecated, will need to fix …
Browse files Browse the repository at this point in the history
…serialization related functions later
  • Loading branch information
daico007 committed Nov 19, 2023
1 parent 8c8ef4b commit c5e55e6
Show file tree
Hide file tree
Showing 8 changed files with 10 additions and 92 deletions.
2 changes: 1 addition & 1 deletion gmso/abc/__init__.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
from gmso.abc.serialization_utils import GMSOJSONHandler, unyt_to_dict
from gmso.abc.serialization_utils import unyt_to_dict
8 changes: 4 additions & 4 deletions gmso/abc/abstract_potential.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,10 @@ class AbstractPotential(GMSOBase):
PotentialExpression(expression="a*x+b", independent_variables={"x"}),
description="The mathematical expression for the potential",
)
# expected_parameters_dimensions: Dict[str, Any] = Field(
# dict(),
# description="The expected unit dimensions of all the parameters",
# )
expected_parameters_dimensions: Dict[str, Any] = Field(
dict(),
description="The expected unit dimensions of all the parameters",
)
tags: Dict[str, Any] = Field(
{}, description="Tags associated with the potential"
)
Expand Down
36 changes: 4 additions & 32 deletions gmso/abc/abstract_site.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,12 @@ def default_position():

class Site(GMSOBase):
__iterable_attributes__: ClassVar[set] = {
"label",
"name" "label",
"group",
"molecule",
"residue",
"position",
"model_config",
}

__base_doc__: ClassVar[
Expand All @@ -57,9 +59,9 @@ class Site(GMSOBase):

name: str = Field(
"",
validate_default=True,
description="Name of the site, defaults to class name",
)

label: str = Field("", description="Label to be assigned to the site")

group: Optional[StrictStr] = Field(
Expand All @@ -86,36 +88,6 @@ class Site(GMSOBase):
validate_assignment=True,
)

@property
def name(self) -> str:
"""Return the name of the site."""
return self.__dict__.get("name_")

@property
def position(self) -> u.unyt_array:
"""Return the 3D Cartesian coordinates of the site."""
return self.__dict__.get("position_")

@property
def label(self) -> str:
"""Return the label assigned to the site."""
return self.__dict__.get("label_")

@property
def group(self) -> str:
"""Return the group of the site."""
return self.__dict__.get("group_")

@property
def molecule(self) -> tuple:
"""Return the molecule of the site."""
return self.__dict__.get("molecule_")

@property
def residue(self):
"""Return the residue assigned to the site."""
return self.__dict__.get("residue_")

def __repr__(self):
"""Return the formatted representation of the site."""
return (
Expand Down
5 changes: 1 addition & 4 deletions gmso/abc/auto_doc.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,7 @@
from copy import deepcopy
from typing import Any, Dict, List, Optional, Tuple, Type, Union

try:
from pydantic.v1 import BaseModel
except ImportError:
from pydantic import BaseModel
from pydantic import BaseModel

BASE_DOC_ATTR = "__base_doc__"
FIELDS_IN_DOCSTRING = "__alias_to_fields__"
Expand Down
15 changes: 0 additions & 15 deletions gmso/abc/gmso_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@

from pydantic import BaseModel, ConfigDict, validators

from gmso.abc import GMSOJSONHandler
from gmso.abc.auto_doc import apply_docs
from gmso.abc.serialization_utils import dict_to_unyt

Expand All @@ -26,9 +25,7 @@ class GMSOBase(BaseModel, ABC):
# Check https://docs.pydantic.dev/dev-v2/migration/#changes-to-config for more information.
model_config = ConfigDict(
arbitrary_types_allowed=True,
alias_to_fields=dict(),
extra="forbid",
json_encoders=GMSOJSONHandler.json_encoders,
populate_by_name=True,
)

Expand Down Expand Up @@ -90,18 +87,6 @@ def _iter(self, **kwargs) -> "TupleGenerator":

yield from super()._iter(**kwargs)

def json(self, **kwargs):
kwargs["by_alias"] = True
# FIXME: Pydantic>1.8 doesn't recognize json_encoders without this update
self.__config__.json_encoders.update(GMSOJSONHandler.json_encoders)

return super(GMSOBase, self).json(**kwargs)

def json_dict(self, **kwargs):
"""Return a JSON serializable dictionary from the object"""
raw_json = self.json(**kwargs)
return json.loads(raw_json)

@classmethod
def validate(cls, value):
"""Ensure that the object is validated before use."""
Expand Down
18 changes: 0 additions & 18 deletions gmso/abc/serialization_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,21 +33,3 @@ def dict_to_unyt(dict_obj) -> None:
unyt_func = u.unyt_array

dict_obj[key] = unyt_func(np_array, value["unit"])


class JSONHandler:
def __init__(self):
self.json_encoders = {}

def register(self, type_, callable_, override=False):
"""Register a new JSON encoder for an object for serialization in GMSO"""
if type_ not in self.json_encoders:
self.json_encoders[type_] = callable_
else:
if override:
warn(f"Overriding json serializer for {type_}")
self.json_encoders[type_] = callable_


GMSOJSONHandler = JSONHandler()
GMSOJSONHandler.register(u.unyt_array, unyt_to_dict)
15 changes: 0 additions & 15 deletions gmso/utils/decorators.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,6 @@
import functools
import warnings

from gmso.abc import GMSOJSONHandler


class register_pydantic_json(object):
"""Provides a way to register json encoders for a non-JSON serializable class."""

def __init__(self, method="json"):
self.method = method

def __call__(self, cls):
"""Register this class's json encoder method to GMSOJSONHandler."""
json_method = getattr(cls, self.method)
GMSOJSONHandler.register(cls, json_method)
return cls


def deprecate_kwargs(deprecated_kwargs=None):
"""Decorate functions with deprecated/deprecating kwargs."""
Expand Down
3 changes: 0 additions & 3 deletions gmso/utils/expression.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@
import sympy
import unyt as u

from gmso.utils.decorators import register_pydantic_json

__all__ = ["PotentialExpression"]


Expand All @@ -30,7 +28,6 @@ def _are_equal_parameters(u1, u2):
return True


@register_pydantic_json(method="json")
class PotentialExpression:
"""A general Expression class with parameters.
Expand Down

0 comments on commit c5e55e6

Please sign in to comment.