From 2539fdd87a95a66b250fa638ec221c73618658f4 Mon Sep 17 00:00:00 2001 From: Antony Lee Date: Mon, 18 Mar 2019 18:42:23 +0100 Subject: [PATCH] Deduplicate methods shared between Container and Artist. Container does not inherit from Artist, but still shares some of its methods. Instead of duplicating the implementation, just directly use the Artist methods. We could also move these shared methods to a mixin that both Artist and Container inherit, but that would just confuse the matters IMO. --- lib/matplotlib/container.py | 70 +++++-------------------------------- 1 file changed, 8 insertions(+), 62 deletions(-) diff --git a/lib/matplotlib/container.py b/lib/matplotlib/container.py index 3a3783d7332a..4f91e3824d03 100644 --- a/lib/matplotlib/container.py +++ b/lib/matplotlib/container.py @@ -1,5 +1,5 @@ +from matplotlib.artist import Artist import matplotlib.cbook as cbook -import matplotlib.artist as martist class Container(tuple): @@ -18,84 +18,30 @@ def __new__(cls, *args, **kwargs): return tuple.__new__(cls, args[0]) def __init__(self, kl, label=None): - self.eventson = False # fire events only if eventson self._oid = 0 # an observer id self._propobservers = {} # a dict from oids to funcs - self._remove_method = None - self.set_label(label) def remove(self): for c in cbook.flatten( - self, scalarp=lambda x: isinstance(x, martist.Artist)): + self, scalarp=lambda x: isinstance(x, Artist)): if c is not None: c.remove() if self._remove_method: self._remove_method(self) - def get_label(self): - """ - Get the label used for this artist in the legend. - """ - return self._label - - def set_label(self, s): - """ - Set the label to *s* for auto legend. - - Parameters - ---------- - s : object - Any object other than None gets converted to its `str`. - """ - if s is not None: - self._label = str(s) - else: - self._label = None - self.pchanged() - - def add_callback(self, func): - """ - Adds a callback function that will be called whenever one of - the :class:`Artist`'s properties changes. - - Returns an *id* that is useful for removing the callback with - :meth:`remove_callback` later. - """ - oid = self._oid - self._propobservers[oid] = func - self._oid += 1 - return oid - - def remove_callback(self, oid): - """ - Remove a callback based on its *id*. - - .. seealso:: - - :meth:`add_callback` - For adding callbacks - - """ - try: - del self._propobservers[oid] - except KeyError: - pass - - def pchanged(self): - """ - Fire an event when property changed, calling all of the - registered callbacks. - """ - for oid, func in list(self._propobservers.items()): - func(self) - def get_children(self): return [child for child in cbook.flatten(self) if child is not None] + get_label = Artist.get_label + set_label = Artist.set_label + add_callback = Artist.add_callback + remove_callback = Artist.remove_callback + pchanged = Artist.pchanged + class BarContainer(Container): """