Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit f28c7c7

Browse files
committed
ENH : make container broadcast {get,set}_* functions
This uses magic in the `__getattribute__` method which _works_ but does not play nice with IDE's, documentation, or tab-complete. Some Artist properties we don't want to broadcast, like the label.
1 parent 21a80f3 commit f28c7c7

File tree

1 file changed

+20
-0
lines changed

1 file changed

+20
-0
lines changed

lib/matplotlib/container.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ class Container(tuple, Artist):
1010
"""
1111
Base class for containers.
1212
"""
13+
_no_broadcast = ['label', ]
1314

1415
def __repr__(self):
1516
return "<Container object of %d artists>" % (len(self))
@@ -18,7 +19,9 @@ def __new__(cls, *kl, **kwargs):
1819
return tuple.__new__(cls, kl[0])
1920

2021
def __init__(self, kl, label=None, **kwargs):
22+
# set up the artist details
2123
Artist.__init__(self, **kwargs)
24+
# for some reason we special case label
2225
self.set_label(label=label)
2326

2427
def remove(self):
@@ -31,6 +34,23 @@ def remove(self):
3134
def get_children(self):
3235
return list(cbook.flatten(self))
3336

37+
def __getattribute__(self, key):
38+
39+
# broadcast set_* and get_* methods across members
40+
# except for these explicitly not.
41+
if (('set' in key or 'get' in key) and
42+
all(k not in key for k in self._no_broadcast)):
43+
44+
def inner(*args, **kwargs):
45+
return [getattr(a, key)(*args, **kwargs)
46+
for a in self]
47+
inner.__name__ = key
48+
doc = getattr(self[0], key).__doc__
49+
inner.__doc__ = doc
50+
return inner
51+
else:
52+
return super(Container, self).__getattribute__(key)
53+
3454
def draw(self, renderer, *args, **kwargs):
3555
# just broadcast the draw down to children
3656
for a in self:

0 commit comments

Comments
 (0)