Skip to content

Deduplicate methods shared between Container and Artist. #13702

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

Merged
merged 1 commit into from
Sep 3, 2019
Merged
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
70 changes: 8 additions & 62 deletions lib/matplotlib/container.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from matplotlib.artist import Artist
import matplotlib.cbook as cbook
import matplotlib.artist as martist


class Container(tuple):
Expand All @@ -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):
"""
Expand Down