Skip to content

Commit

Permalink
remove unnecessary json.dumps, _json_string cached property
Browse files Browse the repository at this point in the history
  • Loading branch information
tylerflex authored and momchil-flex committed Feb 16, 2023
1 parent 4eeb898 commit e06f679
Showing 1 changed file with 32 additions and 31 deletions.
63 changes: 32 additions & 31 deletions tidy3d/components/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,34 @@
JSON_TAG = "JSON_STRING"


def cache(prop):
"""Decorates a property to cache the first computed value and return it on subsequent calls."""

# note, we could also just use `prop` as dict key, but hashing property might be slow
prop_name = prop.__name__

@wraps(prop)
def cached_property_getter(self):
"""The new property method to be returned by decorator."""

stored_value = self._cached_properties.get(prop_name) # pylint:disable=protected-access

if stored_value is not None:
return stored_value

computed_value = prop(self)
self._cached_properties[prop_name] = computed_value # pylint:disable=protected-access
return computed_value

return cached_property_getter


def cached_property(cached_property_getter):
"""Shortcut for property(cache()) of a getter."""

return property(cache(cached_property_getter))


class Tidy3dBaseModel(pydantic.BaseModel):
"""Base pydantic model that all Tidy3d components inherit from.
Defines configuration for handling data structures
Expand Down Expand Up @@ -489,7 +517,7 @@ def __ge__(self, other):
"""define >= for getting unique indices based on hash."""
return hash(self) >= hash(other)

@property
@cached_property
def _json_string(self) -> str:
"""Returns string representation of a :class:`Tidy3dBaseModel`.
Expand All @@ -509,9 +537,10 @@ def make_json_compatible(json_string: str) -> str:

json_string = self.json(indent=INDENT, exclude_unset=False)
json_string = make_json_compatible(json_string)
json_dict = json.loads(json_string)
return json_string
# json_dict = json.loads(json_string)

return json.dumps(json_dict)
# return json.dumps(json_dict)

@classmethod
def add_type_field(cls) -> None:
Expand Down Expand Up @@ -596,31 +625,3 @@ def generate_docstring(cls) -> str:

doc += "\n"
cls.__doc__ = doc


def cache(prop):
"""Decorates a property to cache the first computed value and return it on subsequent calls."""

# note, we could also just use `prop` as dict key, but hashing property might be slow
prop_name = prop.__name__

@wraps(prop)
def cached_property_getter(self):
"""The new property method to be returned by decorator."""

stored_value = self._cached_properties.get(prop_name) # pylint:disable=protected-access

if stored_value is not None:
return stored_value

computed_value = prop(self)
self._cached_properties[prop_name] = computed_value # pylint:disable=protected-access
return computed_value

return cached_property_getter


def cached_property(cached_property_getter):
"""Shortcut for property(cache()) of a getter."""

return property(cache(cached_property_getter))

0 comments on commit e06f679

Please sign in to comment.