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

Skip to content

Remove the need for a fixed set of signal names in the CallbackRegistry #384

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 2 commits into from
Jul 2, 2011
Merged
Show file tree
Hide file tree
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
3 changes: 1 addition & 2 deletions lib/matplotlib/axes.py
Original file line number Diff line number Diff line change
Expand Up @@ -826,8 +826,7 @@ def cla(self):
spine.cla()

self.ignore_existing_data_limits = True
self.callbacks = cbook.CallbackRegistry(('xlim_changed',
'ylim_changed'))
self.callbacks = cbook.CallbackRegistry()

if self._sharex is not None:
# major and minor are class instances with
Expand Down
4 changes: 2 additions & 2 deletions lib/matplotlib/axis.py
Original file line number Diff line number Diff line change
Expand Up @@ -619,7 +619,7 @@ def __init__(self, axes, pickradius=15):
self.axes = axes
self.major = Ticker()
self.minor = Ticker()
self.callbacks = cbook.CallbackRegistry(('units', 'units finalize'))
self.callbacks = cbook.CallbackRegistry()

#class dummy:
# locator = None
Expand Down Expand Up @@ -714,7 +714,7 @@ def cla(self):
self.isDefault_label = True

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

# whether the grids are on
self._gridOnMajor = rcParams['axes.grid']
Expand Down
2 changes: 1 addition & 1 deletion lib/matplotlib/backend_bases.py
Original file line number Diff line number Diff line change
Expand Up @@ -1386,7 +1386,7 @@ def __init__(self, figure):
figure.set_canvas(self)
self.figure = figure
# a dictionary from event name to a dictionary that maps cid->func
self.callbacks = cbook.CallbackRegistry(self.events)
self.callbacks = cbook.CallbackRegistry()
self.widgetlock = widgets.LockDraw()
self._button = None # the button pressed
self._key = None # the key pressed
Expand Down
37 changes: 15 additions & 22 deletions lib/matplotlib/cbook.py
Original file line number Diff line number Diff line change
Expand Up @@ -109,15 +109,13 @@ class CallbackRegistry:
Handle registering and disconnecting for a set of signals and
callbacks::

signals = 'eat', 'drink', 'be merry'

def oneat(x):
print 'eat', x

def ondrink(x):
print 'drink', x

callbacks = CallbackRegistry(signals)
callbacks = CallbackRegistry()

ideat = callbacks.connect('eat', oneat)
iddrink = callbacks.connect('drink', ondrink)
Expand Down Expand Up @@ -208,30 +206,25 @@ def __ne__(self, other):
'''
return not self.__eq__(other)

def __init__(self, signals):
'*signals* is a sequence of valid signals'
self.signals = set(signals)
self.callbacks = dict([(s, dict()) for s in signals])
def __init__(self, *args):
if len(args):
warnings.warn(
'CallbackRegistry no longer requires a list of callback types. Ignoring arguments',
DeprecationWarning)
self.callbacks = dict()
self._cid = 0
self._func_cid_map = WeakKeyDictionary()

def _check_signal(self, s):
'make sure *s* is a valid signal or raise a ValueError'
if s not in self.signals:
signals = list(self.signals)
signals.sort()
raise ValueError('Unknown signal "%s"; valid signals are %s'%(s, signals))

def connect(self, s, func):
"""
register *func* to be called when a signal *s* is generated
func will be called
"""
self._check_signal(s)
if func in self._func_cid_map:
return self._func_cid_map[func]
proxy = self.BoundMethodProxy(func)
self._cid += 1
self.callbacks.setdefault(s, dict())
self.callbacks[s][self._cid] = proxy
self._func_cid_map[func] = self._cid
return self._cid
Expand All @@ -253,13 +246,13 @@ def process(self, s, *args, **kwargs):
process signal *s*. All of the functions registered to receive
callbacks on *s* will be called with *\*args* and *\*\*kwargs*
"""
self._check_signal(s)
for cid, proxy in self.callbacks[s].items():
# Clean out dead references
if proxy.inst is not None and proxy.inst() is None:
del self.callbacks[s][cid]
else:
proxy(*args, **kwargs)
if s in self.callbacks:
for cid, proxy in self.callbacks[s].items():
# Clean out dead references
if proxy.inst is not None and proxy.inst() is None:
del self.callbacks[s][cid]
else:
proxy(*args, **kwargs)


class Scheduler(threading.Thread):
Expand Down
3 changes: 1 addition & 2 deletions lib/matplotlib/cm.py
Original file line number Diff line number Diff line change
Expand Up @@ -165,8 +165,7 @@ def __init__(self, norm=None, cmap=None):
:mod:`cm` colormap instance, for example :data:`cm.jet`
"""

self.callbacksSM = cbook.CallbackRegistry((
'changed',))
self.callbacksSM = cbook.CallbackRegistry()

if cmap is None: cmap = get_cmap()
if norm is None: norm = colors.Normalize()
Expand Down
4 changes: 2 additions & 2 deletions lib/matplotlib/figure.py
Original file line number Diff line number Diff line change
Expand Up @@ -247,7 +247,7 @@ def __init__(self,
"""
Artist.__init__(self)

self.callbacks = cbook.CallbackRegistry(('dpi_changed', ))
self.callbacks = cbook.CallbackRegistry()

if figsize is None : figsize = rcParams['figure.figsize']
if dpi is None : dpi = rcParams['figure.dpi']
Expand Down Expand Up @@ -777,7 +777,7 @@ def clf(self, keep_observers=False):
a gui widget is tracking the axes in the figure.
"""
self.suppressComposite = None
self.callbacks = cbook.CallbackRegistry(('dpi_changed', ))
self.callbacks = cbook.CallbackRegistry()

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