From e1b228e4fda27c7d47bce541d4f316668471c714 Mon Sep 17 00:00:00 2001 From: Aaron Ayres Date: Thu, 18 Mar 2021 11:16:00 -0500 Subject: [PATCH 1/2] remove Serializable mixin, related code --- chaco/abstract_mapper.py | 5 -- chaco/array_data_source.py | 7 -- chaco/barplot.py | 5 -- chaco/base_xy_plot.py | 8 -- chaco/data_range_1d.py | 7 -- chaco/grid.py | 10 --- chaco/serializable.py | 116 ---------------------------- chaco/tests/serializable_base.py | 26 ------- chaco/tests/test_arraydatasource.py | 17 ---- chaco/tests/test_serializable.py | 44 ----------- 10 files changed, 245 deletions(-) delete mode 100644 chaco/serializable.py delete mode 100644 chaco/tests/serializable_base.py delete mode 100644 chaco/tests/test_serializable.py diff --git a/chaco/abstract_mapper.py b/chaco/abstract_mapper.py index 8483bb741..b2ec09695 100644 --- a/chaco/abstract_mapper.py +++ b/chaco/abstract_mapper.py @@ -60,9 +60,4 @@ def __getstate__(self): return state - def _post_load(self): - self._cache_valid = False - self._range_changed(None, self.range) - return - # EOF diff --git a/chaco/array_data_source.py b/chaco/array_data_source.py index 1df4e1dbe..13887ef2e 100644 --- a/chaco/array_data_source.py +++ b/chaco/array_data_source.py @@ -313,11 +313,4 @@ def __getstate__(self): state.pop("_max_index", None) return state - def _post_load(self): - super(ArrayDataSource, self)._post_load() - self._cached_bounds = () - self._cached_mask = None - return - - # EOF diff --git a/chaco/barplot.py b/chaco/barplot.py index dd0292a5c..adb17267f 100644 --- a/chaco/barplot.py +++ b/chaco/barplot.py @@ -329,11 +329,6 @@ def _render_icon(self, gc, x, y, width, height): gc.rect(x+width/4, y+height/4, width/2, height/2) gc.draw_path(FILL_STROKE) - def _post_load(self): - super(BarPlot, self)._post_load() - return - - #------------------------------------------------------------------------ # Properties #------------------------------------------------------------------------ diff --git a/chaco/base_xy_plot.py b/chaco/base_xy_plot.py index 5e8c99a61..10a341a90 100644 --- a/chaco/base_xy_plot.py +++ b/chaco/base_xy_plot.py @@ -496,14 +496,6 @@ def _draw_default_axes(self, gc): gc.stroke_path() return - def _post_load(self): - super(BaseXYPlot, self)._post_load() - self._update_mappers() - self.invalidate_draw() - self._cache_valid = False - self._screen_cache_valid = False - return - def _update_subdivision(self): return diff --git a/chaco/data_range_1d.py b/chaco/data_range_1d.py index e28b14ac7..61fbf1013 100644 --- a/chaco/data_range_1d.py +++ b/chaco/data_range_1d.py @@ -387,13 +387,6 @@ def _sources_changed(self, old, new): for source in new: source.on_trait_change(self.refresh, "data_changed") - #------------------------------------------------------------------------ - # Serialization interface - #------------------------------------------------------------------------ - - def _post_load(self): - self._sources_changed(None, self.sources) - ###### method to calculate bounds for a given 1-dimensional set of data def calc_bounds(low_set, high_set, mins, maxes, epsilon, tight_bounds, diff --git a/chaco/grid.py b/chaco/grid.py index 8cee2ecc1..64063c4d8 100644 --- a/chaco/grid.py +++ b/chaco/grid.py @@ -407,8 +407,6 @@ def _orientation_changed(self): ### Persistence ########################################################### - #_pickles = ("orientation", "line_color", "line_style", "line_weight", - # "grid_interval", "mapper") def __getstate__(self): state = super(PlotGrid,self).__getstate__() @@ -418,12 +416,4 @@ def __getstate__(self): return state - def _post_load(self): - super(PlotGrid, self)._post_load() - self._mapper_changed(None, self.mapper) - self._reset_cache() - self._cache_valid = False - return - - # EOF diff --git a/chaco/serializable.py b/chaco/serializable.py deleted file mode 100644 index 3896daad5..000000000 --- a/chaco/serializable.py +++ /dev/null @@ -1,116 +0,0 @@ -""" Defines the Serializable mix-in class. -""" - - - - -class Serializable(object): - """ - Mix-in class to help serialization. Serializes just the attributes in - **_pickles**. - - This mix-in works best when all the classes in a hierarchy subclass - from it. It solves the problem of allowing each class to specify - its own set of attributes to pickle and attributes to ignore, without - having to also implement __getstate__ and __setstate__. - """ - - # The basic list of attributes to save. These get set without firing - # any trait events. - _pickles = None - - # A list of the parents of this class that will be searched for their - # list of _pickles. Only the parents in this list that inherit from - # Serialized will be pickled. The process stops at the first item in - # __pickle_parents that is not a subclass of Serialized. - # - # This is a double-underscore variable so that Python's attribute name - # will shield base class -# __pickle_parents = None - - def _get_pickle_parents(self): - """ - Subclasses can override this method to return the list of base - classes they want to have the serializer look at. - """ - bases = [] - for cls in self.__class__.__mro__: - if cls is Serializable: - # don't add Serializable to the list of parents - continue - elif issubclass(cls, Serializable): - bases.append(cls) - else: - break - return bases - - def _pre_save(self): - """ - Called before __getstate__ to give the object a chance to tidy up - and get ready to be saved. This usually also calls the superclass. - """ - return - - def _post_load(self): - """ - Called after __setstate__ finishes restoring the state on the object. - This method usually needs to include a call to super(cls, self)._post_load(). - Avoid explicitly calling a parent class by name, because in general - you want post_load() to happen in the same order as MRO, which super() - does automatically. - """ - print('Serializable._post_load') - pass - - def _do_setstate(self, state): - """ - Called by __setstate__ to allow the subclass to set its state in a - special way. - - Subclasses should override this instead of Serializable.__setstate__ - because we need Serializable's implementation to call _post_load() after - all the _do_setstate() have returned.) - """ - # Quietly set all the attributes - self.trait_setq(**state) - return - - #------------------------------------------------------------------------ - # Private methods - #------------------------------------------------------------------------ - -# def __getstate__(self): -# #idstring = self.__class__.__name__ + " id=" + str(id(self)) -# # Give the object a chance to tidy up before saving -# self._pre_save() -# -# # Get the attributes that this class needs to serialize. We do this by -# # marching up the list of parent classes in _pickle_parents and getting -# # their lists of _pickles. -# all_pickles = Set() -# pickle_parents = self._get_pickle_parents() -# for parent_class in pickle_parents: -# all_pickles.update(parent_class._pickles) -# -# if self._pickles is not None: -# all_pickles.update(self._pickles) -# -# state = {} -# for attrib in all_pickles: -# state[attrib] = getattr(self, attrib) -# -# print('<<<<<<<<<<<<<', self) -# for key,value in state.items(): -# print(key, type(value)) -# print '>>>>>>>>>>>>>' -# -# return state - - #~ def __setstate__(self, state): - #~ idstring = self.__class__.__name__ + " id=" + str(id(self)) - #~ self._do_setstate(state) - #~ self._post_load() - #~ return - - -# EOF diff --git a/chaco/tests/serializable_base.py b/chaco/tests/serializable_base.py deleted file mode 100644 index 117335c01..000000000 --- a/chaco/tests/serializable_base.py +++ /dev/null @@ -1,26 +0,0 @@ - - -from traits.api import Bool, HasTraits, Str, Float, Enum, List, Int -from chaco.serializable import Serializable - -class Root(HasTraits): - name = Str - x = Float(0.0) - y = Float(0.0) - -class Shape(Serializable, Root): - color = Enum("red", "green", "blue") - filled = Bool(True) - tools = List - _pickles = ("tools", "filled", "color", "x") - -class Circle(Shape): - radius = Float(10.0) - _pickles = ("radius",) - -class Poly(Shape): - numsides = Int(5) - length = Float(5.0) - _pickles = ("numsides", "length") - -# EOF diff --git a/chaco/tests/test_arraydatasource.py b/chaco/tests/test_arraydatasource.py index ee9abd169..f64779371 100644 --- a/chaco/tests/test_arraydatasource.py +++ b/chaco/tests/test_arraydatasource.py @@ -239,23 +239,6 @@ def test_serialization_state_no_persist(self): "_max_index"]: self.assertIn(key, state) - @unittest.skip("I think this is just broken") - def test_serialization_post_load(self): - self.data_source.set_mask(self.mymask) - - pickled_data_source = pickle.dumps(self.data_source) - unpickled_data_source = pickle.loads(pickled_data_source) - unpickled_data_source._post_load() - - self.assertEqual(unpickled_data_source._cached_bounds, ()) - self.assertEqual(unpickled_data_source._cached_mask, None) - - assert_array_equal(self.data_source.get_data(), - unpickled_data_source.get_data()) - - mask = unpickled_data_source.get_data_mask()[1] - assert_array_equal(mask, ones(10)) - class PointDataTestCase(unittest.TestCase): # Since PointData is mostly the same as ScalarData, the key things to diff --git a/chaco/tests/test_serializable.py b/chaco/tests/test_serializable.py deleted file mode 100644 index 3602ef3a9..000000000 --- a/chaco/tests/test_serializable.py +++ /dev/null @@ -1,44 +0,0 @@ -import pickle -import unittest -import warnings - - -# pickling child classes doesn't work well in the unittest framework unless -# the classes to be pickled are in a different file -from .serializable_base import Circle, Poly - -class SimpleSerializationTestCase(unittest.TestCase): - - def compare_traits(self, a, b, trait_names=None): - "Checks the traits of objects 'a' and 'b' and makes sure they all match." - if trait_names is None: - trait_names = a.trait_names() - for name in trait_names: - if name in ("trait_added", "trait_modified"): - continue - o1 = getattr(a,name) - o2 = getattr(b,name) - if isinstance(o1, list) or isinstance(o1, tuple): - raise RuntimeError( - "Warning: Cowardly refusing to do deep compares" - ) - else: - self.assertTrue(o1 == o2) - - def test_basic_save(self): - c = Circle(radius=5.0, name="c1", x=1.0, y=2.0) - c2 = pickle.loads(pickle.dumps(c)) - for attrib in ("tools", "filled", "color", "x", "radius"): - self.assertTrue(getattr(c, attrib) == getattr(c2, attrib)) - self.assertEqual(c2.y, 2.0) - - def test_basic_save2(self): - p = Poly(numside=3, name="poly", x=3.0, y=4.0) - p2 = pickle.loads(pickle.dumps(p)) - for attrib in ("tools", "filled", "color", "x", "numsides", "length"): - self.assertTrue(getattr(p, attrib) == getattr(p2, attrib)) - self.assertEqual(p2.y, 4.0) - - -class PlotSerializationTestCase(unittest.TestCase): - pass From db79fdb71298e4045036c17d2195584111dc9b02 Mon Sep 17 00:00:00 2001 From: Aaron Ayres Date: Fri, 2 Apr 2021 09:15:27 -0500 Subject: [PATCH 2/2] accidentally got undeleted while merging master --- chaco/serializable.py | 111 ------------------------------- chaco/tests/serializable_base.py | 24 ------- 2 files changed, 135 deletions(-) delete mode 100644 chaco/serializable.py delete mode 100644 chaco/tests/serializable_base.py diff --git a/chaco/serializable.py b/chaco/serializable.py deleted file mode 100644 index 4228c12aa..000000000 --- a/chaco/serializable.py +++ /dev/null @@ -1,111 +0,0 @@ -""" Defines the Serializable mix-in class. -""" - - - - -class Serializable(object): - """ - Mix-in class to help serialization. Serializes just the attributes in - **_pickles**. - - This mix-in works best when all the classes in a hierarchy subclass - from it. It solves the problem of allowing each class to specify - its own set of attributes to pickle and attributes to ignore, without - having to also implement __getstate__ and __setstate__. - """ - - # The basic list of attributes to save. These get set without firing - # any trait events. - _pickles = None - - # A list of the parents of this class that will be searched for their - # list of _pickles. Only the parents in this list that inherit from - # Serialized will be pickled. The process stops at the first item in - # __pickle_parents that is not a subclass of Serialized. - # - # This is a double-underscore variable so that Python's attribute name - # will shield base class -# __pickle_parents = None - - def _get_pickle_parents(self): - """ - Subclasses can override this method to return the list of base - classes they want to have the serializer look at. - """ - bases = [] - for cls in self.__class__.__mro__: - if cls is Serializable: - # don't add Serializable to the list of parents - continue - elif issubclass(cls, Serializable): - bases.append(cls) - else: - break - return bases - - def _pre_save(self): - """ - Called before __getstate__ to give the object a chance to tidy up - and get ready to be saved. This usually also calls the superclass. - """ - - def _post_load(self): - """ - Called after __setstate__ finishes restoring the state on the object. - This method usually needs to include a call to super(cls, self)._post_load(). - Avoid explicitly calling a parent class by name, because in general - you want post_load() to happen in the same order as MRO, which super() - does automatically. - """ - print('Serializable._post_load') - pass - - def _do_setstate(self, state): - """ - Called by __setstate__ to allow the subclass to set its state in a - special way. - - Subclasses should override this instead of Serializable.__setstate__ - because we need Serializable's implementation to call _post_load() after - all the _do_setstate() have returned.) - """ - # Quietly set all the attributes - self.trait_setq(**state) - - #------------------------------------------------------------------------ - # Private methods - #------------------------------------------------------------------------ - -# def __getstate__(self): -# #idstring = self.__class__.__name__ + " id=" + str(id(self)) -# # Give the object a chance to tidy up before saving -# self._pre_save() -# -# # Get the attributes that this class needs to serialize. We do this by -# # marching up the list of parent classes in _pickle_parents and getting -# # their lists of _pickles. -# all_pickles = Set() -# pickle_parents = self._get_pickle_parents() -# for parent_class in pickle_parents: -# all_pickles.update(parent_class._pickles) -# -# if self._pickles is not None: -# all_pickles.update(self._pickles) -# -# state = {} -# for attrib in all_pickles: -# state[attrib] = getattr(self, attrib) -# -# print('<<<<<<<<<<<<<', self) -# for key,value in state.items(): -# print(key, type(value)) -# print '>>>>>>>>>>>>>' -# -# return state - - #~ def __setstate__(self, state): - #~ idstring = self.__class__.__name__ + " id=" + str(id(self)) - #~ self._do_setstate(state) - #~ self._post_load() - #~ return diff --git a/chaco/tests/serializable_base.py b/chaco/tests/serializable_base.py deleted file mode 100644 index 7f752b62a..000000000 --- a/chaco/tests/serializable_base.py +++ /dev/null @@ -1,24 +0,0 @@ - - -from traits.api import Bool, HasTraits, Str, Float, Enum, List, Int -from chaco.serializable import Serializable - -class Root(HasTraits): - name = Str - x = Float(0.0) - y = Float(0.0) - -class Shape(Serializable, Root): - color = Enum("red", "green", "blue") - filled = Bool(True) - tools = List - _pickles = ("tools", "filled", "color", "x") - -class Circle(Shape): - radius = Float(10.0) - _pickles = ("radius",) - -class Poly(Shape): - numsides = Int(5) - length = Float(5.0) - _pickles = ("numsides", "length")