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

Skip to content

Commit 9303465

Browse files
committed
Remove the need for a fixed set of signal names in the CallbackRegistry
1 parent 35071df commit 9303465

File tree

6 files changed

+18
-31
lines changed

6 files changed

+18
-31
lines changed

lib/matplotlib/axes.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -826,8 +826,7 @@ def cla(self):
826826
spine.cla()
827827

828828
self.ignore_existing_data_limits = True
829-
self.callbacks = cbook.CallbackRegistry(('xlim_changed',
830-
'ylim_changed'))
829+
self.callbacks = cbook.CallbackRegistry()
831830

832831
if self._sharex is not None:
833832
# major and minor are class instances with

lib/matplotlib/axis.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -619,7 +619,7 @@ def __init__(self, axes, pickradius=15):
619619
self.axes = axes
620620
self.major = Ticker()
621621
self.minor = Ticker()
622-
self.callbacks = cbook.CallbackRegistry(('units', 'units finalize'))
622+
self.callbacks = cbook.CallbackRegistry()
623623

624624
#class dummy:
625625
# locator = None
@@ -714,7 +714,7 @@ def cla(self):
714714
self.isDefault_label = True
715715

716716
# Clear the callback registry for this axis, or it may "leak"
717-
self.callbacks = cbook.CallbackRegistry(('units', 'units finalize'))
717+
self.callbacks = cbook.CallbackRegistry()
718718

719719
# whether the grids are on
720720
self._gridOnMajor = rcParams['axes.grid']

lib/matplotlib/backend_bases.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1386,7 +1386,7 @@ def __init__(self, figure):
13861386
figure.set_canvas(self)
13871387
self.figure = figure
13881388
# a dictionary from event name to a dictionary that maps cid->func
1389-
self.callbacks = cbook.CallbackRegistry(self.events)
1389+
self.callbacks = cbook.CallbackRegistry()
13901390
self.widgetlock = widgets.LockDraw()
13911391
self._button = None # the button pressed
13921392
self._key = None # the key pressed

lib/matplotlib/cbook.py

Lines changed: 11 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -109,15 +109,13 @@ class CallbackRegistry:
109109
Handle registering and disconnecting for a set of signals and
110110
callbacks::
111111
112-
signals = 'eat', 'drink', 'be merry'
113-
114112
def oneat(x):
115113
print 'eat', x
116114
117115
def ondrink(x):
118116
print 'drink', x
119117
120-
callbacks = CallbackRegistry(signals)
118+
callbacks = CallbackRegistry()
121119
122120
ideat = callbacks.connect('eat', oneat)
123121
iddrink = callbacks.connect('drink', ondrink)
@@ -208,30 +206,21 @@ def __ne__(self, other):
208206
'''
209207
return not self.__eq__(other)
210208

211-
def __init__(self, signals):
212-
'*signals* is a sequence of valid signals'
213-
self.signals = set(signals)
214-
self.callbacks = dict([(s, dict()) for s in signals])
209+
def __init__(self):
210+
self.callbacks = dict()
215211
self._cid = 0
216212
self._func_cid_map = WeakKeyDictionary()
217213

218-
def _check_signal(self, s):
219-
'make sure *s* is a valid signal or raise a ValueError'
220-
if s not in self.signals:
221-
signals = list(self.signals)
222-
signals.sort()
223-
raise ValueError('Unknown signal "%s"; valid signals are %s'%(s, signals))
224-
225214
def connect(self, s, func):
226215
"""
227216
register *func* to be called when a signal *s* is generated
228217
func will be called
229218
"""
230-
self._check_signal(s)
231219
if func in self._func_cid_map:
232220
return self._func_cid_map[func]
233221
proxy = self.BoundMethodProxy(func)
234222
self._cid += 1
223+
self.callbacks.setdefault(s, dict())
235224
self.callbacks[s][self._cid] = proxy
236225
self._func_cid_map[func] = self._cid
237226
return self._cid
@@ -253,13 +242,13 @@ def process(self, s, *args, **kwargs):
253242
process signal *s*. All of the functions registered to receive
254243
callbacks on *s* will be called with *\*args* and *\*\*kwargs*
255244
"""
256-
self._check_signal(s)
257-
for cid, proxy in self.callbacks[s].items():
258-
# Clean out dead references
259-
if proxy.inst is not None and proxy.inst() is None:
260-
del self.callbacks[s][cid]
261-
else:
262-
proxy(*args, **kwargs)
245+
if s in self.callbacks:
246+
for cid, proxy in self.callbacks[s].items():
247+
# Clean out dead references
248+
if proxy.inst is not None and proxy.inst() is None:
249+
del self.callbacks[s][cid]
250+
else:
251+
proxy(*args, **kwargs)
263252

264253

265254
class Scheduler(threading.Thread):

lib/matplotlib/cm.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -165,8 +165,7 @@ def __init__(self, norm=None, cmap=None):
165165
:mod:`cm` colormap instance, for example :data:`cm.jet`
166166
"""
167167

168-
self.callbacksSM = cbook.CallbackRegistry((
169-
'changed',))
168+
self.callbacksSM = cbook.CallbackRegistry()
170169

171170
if cmap is None: cmap = get_cmap()
172171
if norm is None: norm = colors.Normalize()

lib/matplotlib/figure.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -247,7 +247,7 @@ def __init__(self,
247247
"""
248248
Artist.__init__(self)
249249

250-
self.callbacks = cbook.CallbackRegistry(('dpi_changed', ))
250+
self.callbacks = cbook.CallbackRegistry()
251251

252252
if figsize is None : figsize = rcParams['figure.figsize']
253253
if dpi is None : dpi = rcParams['figure.dpi']
@@ -777,7 +777,7 @@ def clf(self, keep_observers=False):
777777
a gui widget is tracking the axes in the figure.
778778
"""
779779
self.suppressComposite = None
780-
self.callbacks = cbook.CallbackRegistry(('dpi_changed', ))
780+
self.callbacks = cbook.CallbackRegistry()
781781

782782
for ax in tuple(self.axes): # Iterate over the copy.
783783
ax.cla()

0 commit comments

Comments
 (0)