8000 Deduplicate methods shared between Container and Artist. · matplotlib/matplotlib@2539fdd · GitHub
[go: up one dir, main page]

Skip to content

Commit 2539fdd

Browse files
committed
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.
1 parent b34c605 commit 2539fdd

File tree

1 file changed

+8
-62
lines changed

1 file changed

+8
-62
lines changed

lib/matplotlib/container.py

Lines changed: 8 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1+
from matplotlib.artist import Artist
12
import matplotlib.cbook as cbook
2-
import matplotlib.artist as martist
33

44

55
class Container(tuple):
@@ -18,84 +18,30 @@ def __new__(cls, *args, **kwargs):
1818
return tuple.__new__(cls, args[0])
1919

2020
def __init__(self, kl, label=None):
21-
2221
self.eventson = False # fire events only if eventson
2322
self._oid = 0 # an observer id
2423
self._propobservers = {} # a dict from oids to funcs
25-
2624
self._remove_method = None
27-
2825
self.set_label(label)
2926

3027
def remove(self):
3128
for c in cbook.flatten(
32-
self, scalarp=lambda x: isinstance(x, martist.Artist)):
29+
self, scalarp=lambda x: isinstance(x, Artist)):
3330
if c is not None:
3431
c.remove()
3532

3633
if self._remove_method:
3734
self._remove_method(self)
3835

39-
def get_label(self):
40-
"""
41-
Get the label used for this artist in the legend.
42-
"""
43-
return self._label
44-
45-
def set_label(self, s):
46-
"""
47-
Set the label to *s* for auto legend.
48-
49-
Parameters
50-
----------
51-
s : object
52-
Any object other than None gets converted to its `str`.
53-
"""
54-
if s is not None:
55-
self._label = str(s)
56-
else:
57-
self._label = None
58-
self.pchanged()
59-
60-
def add_callback(self, func):
61-
"""
62-
Adds a callback function that will be called whenever one of
63-
the :class:`Artist`'s properties changes.
64-
65-
Returns an *id* that is useful for removing the callback with
66-
:meth:`remove_callback` later.
67-
"""
68-
oid = self._oid
69-
self._propobservers[oid] = func
70-
self._oid += 1
71-
return oid
72-
73-
def remove_callback(self, oid):
74-
"""
75-
Remove a callback based on its *id*.
76-
77-
.. seealso::
78-
79-
:meth:`add_callback`
80-
For adding callbacks
81-
82-
"""
83-
try:
84-
del self._propobservers[oid]
85-
except KeyError:
86-
pass
87-
88-
def pchanged(self):
89-
"""
90-
Fire an event when property changed, calling all of the
91-
registered callbacks.
92-
"""
93-
for oid, func in list(self._propobservers.items()):
94-
func(self)
95-
9636
def get_children(self):
9737
return [child for child in cbook.flatten(self) if child is not None]
9838

39+
get_label = Artist.get_label
40+
set_label = Artist.set_label
41+
add_callback = Artist.add_callback
42+
remove_callback = Artist.remove_callback
43+
pchanged = Artist.pchanged
44+
9945

10046
class BarContainer(Container):
10147
"""

0 commit comments

Comments
 (0)
0