Skip to content

Commit 28aa647

Browse files
committed
Merge pull request #1334 from NelleV/pep8_blocking_input
PEP8 fixes on blocking_input.py
2 parents f0fb4f7 + 292eb8e commit 28aa647

File tree

1 file changed

+67
-60
lines changed

1 file changed

+67
-60
lines changed

lib/matplotlib/blocking_input.py

Lines changed: 67 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,24 @@
11
"""
2-
This provides several classes used for blocking interaction with figure windows:
2+
This provides several classes used for blocking interaction with figure
3+
windows:
34
45
:class:`BlockingInput`
5-
creates a callable object to retrieve events in a blocking way for interactive sessions
6+
creates a callable object to retrieve events in a blocking way for
7+
interactive sessions
68
79
:class:`BlockingKeyMouseInput`
8-
creates a callable object to retrieve key or mouse clicks in a blocking way for interactive sessions.
10+
creates a callable object to retrieve key or mouse clicks in a blocking
11+
way for interactive sessions.
912
Note: Subclass of BlockingInput. Used by waitforbuttonpress
1013
1114
:class:`BlockingMouseInput`
12-
creates a callable object to retrieve mouse clicks in a blocking way for interactive sessions.
15+
creates a callable object to retrieve mouse clicks in a blocking way for
16+
interactive sessions.
1317
Note: Subclass of BlockingInput. Used by ginput
1418
1519
:class:`BlockingContourLabeler`
16-
creates a callable object to retrieve mouse clicks in a blocking way that will then be used to place labels on a ContourSet
20+
creates a callable object to retrieve mouse clicks in a blocking way that
21+
will then be used to place labels on a ContourSet
1722
Note: Subclass of BlockingMouseInput. Used by clabel
1823
"""
1924

@@ -22,14 +27,16 @@
2227
from matplotlib.cbook import is_sequence_of_strings
2328
import matplotlib.lines as mlines
2429

30+
2531
class BlockingInput(object):
2632
"""
2733
Class that creates a callable object to retrieve events in a
2834
blocking way.
2935
"""
3036
def __init__(self, fig, eventslist=()):
3137
self.fig = fig
32-
assert is_sequence_of_strings(eventslist), "Requires a sequence of event name strings"
38+
assert is_sequence_of_strings(
39+
eventslist), "Requires a sequence of event name strings"
3340
self.eventslist = eventslist
3441

3542
def on_event(self, event):
@@ -60,13 +67,13 @@ def cleanup(self):
6067
for cb in self.callbacks:
6168
self.fig.canvas.mpl_disconnect(cb)
6269

63-
self.callbacks=[]
70+
self.callbacks = []
6471

65-
def add_event(self,event):
72+
def add_event(self, event):
6673
"""For base class, this just appends an event to events."""
6774
self.events.append(event)
6875

69-
def pop_event(self,index=-1):
76+
def pop_event(self, index=-1):
7077
"""
7178
This removes an event from the event list. Defaults to
7279
removing last event, but an index can be supplied. Note that
@@ -76,11 +83,11 @@ def pop_event(self,index=-1):
7683
"""
7784
self.events.pop(index)
7885

79-
def pop(self,index=-1):
86+
def pop(self, index=-1):
8087
self.pop_event(index)
81-
pop.__doc__=pop_event.__doc__
88+
pop.__doc__ = pop_event.__doc__
8289

83-
def __call__(self, n=1, timeout=30 ):
90+
def __call__(self, n=1, timeout=30):
8491
"""
8592
Blocking call to retrieve n events
8693
"""
@@ -96,18 +103,20 @@ def __call__(self, n=1, timeout=30 ):
96103

97104
# connect the events to the on_event function call
98105
for n in self.eventslist:
99-
self.callbacks.append( self.fig.canvas.mpl_connect(n, self.on_event) )
106+
self.callbacks.append(
107+
self.fig.canvas.mpl_connect(n, self.on_event))
100108

101109
try:
102110
# Start event loop
103111
self.fig.canvas.start_event_loop(timeout=timeout)
104-
finally: # Run even on exception like ctrl-c
112+
finally: # Run even on exception like ctrl-c
105113
# Disconnect the callbacks
106114
self.cleanup()
107115

108116
# Return the events in this case
109117
return self.events
110118

119+
111120
class BlockingMouseInput(BlockingInput):
112121
"""
113122
Class that creates a callable object to retrieve mouse clicks in a
@@ -118,25 +127,23 @@ class BlockingMouseInput(BlockingInput):
118127
enter is like mouse button 2 and all others are like mouse button 1).
119128
"""
120129

121-
button_add = 1
122-
button_pop = 3
123-
button_stop = 2
130+
button_add = 1
131+
button_pop = 3
132+
button_stop = 2
124133

125134
def __init__(self, fig, mouse_add=1, mouse_pop=3, mouse_stop=2):
126135
BlockingInput.__init__(self, fig=fig,
127136
eventslist=('button_press_event',
128-
'key_press_event') )
137+
'key_press_event'))
129138
self.button_add = mouse_add
130139
self.button_pop = mouse_pop
131-
self.button_stop= mouse_stop
132-
133-
140+
self.button_stop = mouse_stop
134141

135142
def post_event(self):
136143
"""
137144
This will be called to process events
138145
"""
139-
assert len(self.events)>0, "No events yet"
146+
assert len(self.events) > 0, "No events yet"
140147

141148
if self.events[-1].name == 'key_press_event':
142149
self.key_event()
@@ -171,56 +178,57 @@ def key_event(self):
171178

172179
if key in ['backspace', 'delete']:
173180
self.mouse_event_pop(event)
174-
elif key in ['escape', 'enter']: # on windows XP and wxAgg, the enter key doesn't seem to register
181+
elif key in ['escape', 'enter']:
182+
# on windows XP and wxAgg, the enter key doesn't seem to register
175183
self.mouse_event_stop(event)
176184
else:
177185
self.mouse_event_add(event)
178186

179-
def mouse_event_add( self, event ):
187+
def mouse_event_add(self, event):
180188
"""
181189
Will be called for any event involving a button other than
182190
button 2 or 3. This will add a click if it is inside axes.
183191
"""
184192
if event.inaxes:
185193
self.add_click(event)
186-
else: # If not a valid click, remove from event list
187-
BlockingInput.pop(self,-1)
194+
else: # If not a valid click, remove from event list
195+
BlockingInput.pop(self, -1)
188196

189-
def mouse_event_stop( self, event ):
197+
def mouse_event_stop(self, event):
190198
"""
191199
Will be called for any event involving button 2.
192200
Button 2 ends blocking input.
193201
"""
194202

195203
# Remove last event just for cleanliness
196-
BlockingInput.pop(self,-1)
204+
BlockingInput.pop(self, -1)
197205

198206
# This will exit even if not in infinite mode. This is
199207
# consistent with MATLAB and sometimes quite useful, but will
200208
# require the user to test how many points were actually
201209
# returned before using data.
202210
self.fig.canvas.stop_event_loop()
203211

204-
def mouse_event_pop( self, event ):
212+
def mouse_event_pop(self, event):
205213
"""
206214
Will be called for any event involving button 3.
207215
Button 3 removes the last click.
208216
"""
209217
# Remove this last event
210-
BlockingInput.pop(self,-1)
218+
BlockingInput.pop(self, -1)
211219

212220
# Now remove any existing clicks if possible
213-
if len(self.events)>0:
214-
self.pop(event,-1)
221+
if len(self.events) > 0:
222+
self.pop(event, -1)
215223

216-
def add_click(self,event):
224+
def add_click(self, event):
217225
"""
218226
This add the coordinates of an event to the list of clicks
219227
"""
220-
self.clicks.append((event.xdata,event.ydata))
228+
self.clicks.append((event.xdata, event.ydata))
221229

222230
verbose.report("input %i: %f,%f" %
223-
(len(self.clicks),event.xdata, event.ydata))
231+
(len(self.clicks), event.xdata, event.ydata))
224232

225233
# If desired plot up click
226234
if self.show_clicks:
@@ -230,9 +238,7 @@ def add_click(self,event):
230238
self.marks.append(line)
231239
self.fig.canvas.draw()
232240

233-
234-
235-
def pop_click(self,event,index=-1):
241+
def pop_click(self, event, index=-1):
236242
"""
237243
This removes a click from the list of clicks. Defaults to
238244
removing the last click.
@@ -249,17 +255,16 @@ def pop_click(self,event,index=-1):
249255
# for the keyboard backspace event on windows XP wxAgg.
250256
# maybe event.inaxes here is a COPY of the actual axes?
251257

252-
253-
def pop(self,event,index=-1):
258+
def pop(self, event, index=-1):
254259
"""
255260
This removes a click and the associated event from the object.
256261
Defaults to removing the last click, but any index can be
257262
supplied.
258263
"""
259-
self.pop_click(event,index)
260-
BlockingInput.pop(self,index)
264+
self.pop_click(event, index)
265+
BlockingInput.pop(self, index)
261266

262-
def cleanup(self,event=None):
267+
def cleanup(self, event=None):
263268
# clean the figure
264269
if self.show_clicks:
265270

@@ -278,28 +283,29 @@ def __call__(self, n=1, timeout=30, show_clicks=True):
278283
clicks.
279284
"""
280285
self.show_clicks = show_clicks
281-
self.clicks = []
282-
self.marks = []
283-
BlockingInput.__call__(self,n=n,timeout=timeout)
286+
self.clicks = []
287+
self.marks = []
288+
BlockingInput.__call__(self, n=n, timeout=timeout)
284289

285290
return self.clicks
286291

287-
class BlockingContourLabeler( BlockingMouseInput ):
292+
293+
class BlockingContourLabeler(BlockingMouseInput):
288294
"""
289295
Class that creates a callable object that uses mouse clicks or key
290296
clicks on a figure window to place contour labels.
291297
"""
292-
def __init__(self,cs):
298+
def __init__(self, cs):
293299
self.cs = cs
294-
BlockingMouseInput.__init__(self, fig=cs.ax.figure )
300+
BlockingMouseInput.__init__(self, fig=cs.ax.figure)
295301

296302
def add_click(self, event):
297303
self.button1(event)
298304

299305
def pop_click(self, event, index=-1):
300306
self.button3(event)
301307

302-
def button1(self,event):
308+
def button1(self, event):
303309
"""
304310
This will be called if an event involving a button other than
305311
2 or 3 occcurs. This will add a label to a contour.
@@ -311,10 +317,10 @@ def button1(self,event):
311317
inline_spacing=self.inline_spacing,
312318
transform=False)
313319
self.fig.canvas.draw()
314-
else: # Remove event if not valid
320+
else: # Remove event if not valid
315321
BlockingInput.pop(self)
316322

317-
def button3(self,event):
323+
def button3(self, event):
318324
"""
319325
This will be called if button 3 is clicked. This will remove
320326
a label if not in inline mode. Unfortunately, if one is doing
@@ -329,26 +335,28 @@ def button3(self,event):
329335
self.cs.pop_label()
330336
self.cs.ax.figure.canvas.draw()
331337

332-
def __call__(self,inline,inline_spacing=5,n=-1,timeout=-1):
333-
self.inline=inline
334-
self.inline_spacing=inline_spacing
338+
def __call__(self, inline, inline_spacing=5, n=-1, timeout=-1):
339+
self.inline = inline
340+
self.inline_spacing = inline_spacing
335341

336-
BlockingMouseInput.__call__(self,n=n,timeout=timeout,
342+
BlockingMouseInput.__call__(self, n=n, timeout=timeout,
337343
show_clicks=False)
338344

345+
339346
class BlockingKeyMouseInput(BlockingInput):
340347
"""
341348
Class that creates a callable object to retrieve a single mouse or
342349
keyboard click
343350
"""
344351
def __init__(self, fig):
345-
BlockingInput.__init__(self, fig=fig, eventslist=('button_press_event','key_press_event') )
352+
BlockingInput.__init__(self, fig=fig, eventslist=(
353+
'button_press_event', 'key_press_event'))
346354

347355
def post_event(self):
348356
"""
349357
Determines if it is a key event
350358
"""
351-
assert len(self.events)>0, "No events yet"
359+
assert len(self.events) > 0, "No events yet"
352360

353361
self.keyormouse = self.events[-1].name == 'key_press_event'
354362

@@ -358,7 +366,6 @@ def __call__(self, timeout=30):
358366
Returns True if key click, False if mouse, or None if timeout
359367
"""
360368
self.keyormouse = None
361-
BlockingInput.__call__(self,n=1,timeout=timeout)
369+
BlockingInput.__call__(self, n=1, timeout=timeout)
362370

363371
return self.keyormouse
364-

0 commit comments

Comments
 (0)