Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make limits cache private for serialization #474

Merged
merged 2 commits into from
Nov 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 12 additions & 12 deletions glue_jupyter/common/state3d.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
# clip_data = CallbackProperty(False)
native_aspect = CallbackProperty(False)

limits_cache = CallbackProperty()
_limits_cache = CallbackProperty()

# def _update_priority(self, name):
# if name == 'layers':
Expand All @@ -48,34 +48,34 @@

super(ViewerState3D, self).__init__(**kwargs)

if self.limits_cache is None:
self.limits_cache = {}
if self._limits_cache is None:
self._limits_cache = {}

self.x_lim_helper = StateAttributeLimitsHelper(self, attribute='x_att',
lower='x_min', upper='x_max',
cache=self.limits_cache)
cache=self._limits_cache)

self.y_lim_helper = StateAttributeLimitsHelper(self, attribute='y_att',
lower='y_min', upper='y_max',
cache=self.limits_cache)
cache=self._limits_cache)

self.z_lim_helper = StateAttributeLimitsHelper(self, attribute='z_att',
lower='z_min', upper='z_max',
cache=self.limits_cache)
cache=self._limits_cache)

# TODO: if limits_cache is re-assigned to a different object, we need to
# update the attribute helpers. However if in future we make limits_cache
# TODO: if _limits_cache is re-assigned to a different object, we need to
# update the attribute helpers. However if in future we make _limits_cache
# into a smart dictionary that can call callbacks when elements are
# changed then we shouldn't always call this. It'd also be nice to
# avoid this altogether and make it more clean.
self.add_callback('limits_cache', nonpartial(self._update_limits_cache))
self.add_callback('_limits_cache', nonpartial(self._update_limits_cache))

def _update_limits_cache(self):
self.x_lim_helper._cache = self.limits_cache
self.x_lim_helper._cache = self._limits_cache

Check warning on line 74 in glue_jupyter/common/state3d.py

View check run for this annotation

Codecov / codecov/patch

glue_jupyter/common/state3d.py#L74

Added line #L74 was not covered by tests
self.x_lim_helper._update_attribute()
self.y_lim_helper._cache = self.limits_cache
self.y_lim_helper._cache = self._limits_cache

Check warning on line 76 in glue_jupyter/common/state3d.py

View check run for this annotation

Codecov / codecov/patch

glue_jupyter/common/state3d.py#L76

Added line #L76 was not covered by tests
self.y_lim_helper._update_attribute()
self.z_lim_helper._cache = self.limits_cache
self.z_lim_helper._cache = self._limits_cache

Check warning on line 78 in glue_jupyter/common/state3d.py

View check run for this annotation

Codecov / codecov/patch

glue_jupyter/common/state3d.py#L78

Added line #L78 was not covered by tests
self.z_lim_helper._update_attribute()

@property
Expand Down
Empty file.
35 changes: 35 additions & 0 deletions glue_jupyter/common/tests/test_state3d.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import traitlets

from glue_jupyter.state_traitlets_helpers import GlueState
from glue_jupyter.common.state3d import ViewerState3D


class Widget(traitlets.HasTraits):

state = GlueState()

latest_json = None

# The following two methods mimic the behavior of ipywidgets

@traitlets.observe('state')
def on_state_change(self, change):
to_json = self.trait_metadata('state', 'to_json')
self.latest_json = to_json(self.state, self)

def set_state_from_json(self, json):
from_json = self.trait_metadata('state', 'from_json')
from_json(json, self)

Check warning on line 22 in glue_jupyter/common/tests/test_state3d.py

View check run for this annotation

Codecov / codecov/patch

glue_jupyter/common/tests/test_state3d.py#L21-L22

Added lines #L21 - L22 were not covered by tests


def test_json_serializable():
widget = Widget()
assert widget.latest_json is None
widget.state = ViewerState3D()
assert widget.latest_json == {
"x_att": None, "x_min": 0, "x_max": 1,
"y_att": None, "y_min": 0, "y_max": 1,
"z_att": None, "z_min": 0, "z_max": 1,
"visible_axes": True, "native_aspect": False,
"layers": [], "title": None,
}
Loading