@@ -1040,7 +1040,7 @@ def set_sketch_params(self, scale=None, length=None, randomness=None):
1040
1040
1041
1041
1042
1042
class TimerBase (object ):
1043
- '''
1043
+ """
1044
1044
A base class for providing timer events, useful for things animations.
1045
1045
Backends need to implement a few specific methods in order to use their
1046
1046
own timing mechanisms so that the timer events are integrated into their
@@ -1083,8 +1083,7 @@ class TimerBase(object):
1083
1083
Stores list of (func, args, kwargs) tuples that will be called upon
1084
1084
timer events. This list can be manipulated directly, or the
1085
1085
functions `add_callback` and `remove_callback` can be used.
1086
-
1087
- '''
1086
+ """
1088
1087
def __init__ (self , interval = None , callbacks = None ):
1089
1088
#Initialize empty callbacks list and setup default settings if necssary
1090
1089
if callbacks is None :
@@ -1103,22 +1102,25 @@ def __init__(self, interval=None, callbacks=None):
1103
1102
self ._timer = None
1104
1103
1105
1104
def __del__ (self ):
1106
- ' Need to stop timer and possibly disconnect timer.'
1105
+ """ Need to stop timer and possibly disconnect timer."""
1107
1106
self ._timer_stop ()
1108
1107
1109
1108
def start (self , interval = None ):
1110
- '''
1111
- Start the timer object. `interval` is optional and will be used
1112
- to reset the timer interval first if provided.
1113
- '''
1109
+ """
1110
+ Start the timer object.
1111
+
1112
+ Parameters
1113
+ ----------
1114
+ interval : int, optional
1115
+ Timer interval in milliseconds; overrides a previously set interval
1116
+ if provided.
1117
+ """
1114
1118
if interval is not None :
1115
1119
self ._set_interval (interval )
1116
1120
self ._timer_start ()
1117
1121
1118
1122
def stop (self ):
1119
- '''
1120
- Stop the timer.
1121
- '''
1123
+ """Stop the timer."""
1122
1124
self ._timer_stop ()
1123
1125
1124
1126
def _timer_start (self ):
@@ -1149,19 +1151,33 @@ def single_shot(self, ss):
1149
1151
self ._timer_set_single_shot ()
1150
1152
1151
1153
def add_callback (self , func , * args , ** kwargs ):
1152
- '''
1154
+ """
1153
1155
Register *func* to be called by timer when the event fires. Any
1154
1156
additional arguments provided will be passed to *func*.
1155
- '''
1157
+
1158
+ This function returns *func*, which makes it possible to use it as a
1159
+ decorator.
1160
+ """
1156
1161
self .callbacks .append ((func , args , kwargs ))
1162
+ return func
1157
1163
1158
1164
def remove_callback (self , func , * args , ** kwargs ):
1159
- '''
1160
- Remove *func* from list of callbacks. *args* and *kwargs* are optional
1161
- and used to distinguish between copies of the same function registered
1162
- to be called with different arguments.
1163
- '''
1165
+ """
1166
+ Remove *func* from list of callbacks.
1167
+
1168
+ *args* and *kwargs* are optional and used to distinguish between copies
1169
+ of the same function registered to be called with different arguments.
1170
+ This behavior is deprecated. In the future, `*args, **kwargs` won't be
1171
+ considered anymore; to keep a specific callback removable by itself,
1172
+ pass it to `add_callback` as a `functools.partial` object.
1173
+ """
1164
1174
if args or kwargs :
1175
+ cbook .warn_deprecated (
1176
+ "3.1" , "In a future version, Timer.remove_callback will not "
1177
+ "take *args, **kwargs anymore, but remove all callbacks where "
1178
+ "the callable matches; to keep a specific callback removable "
1179
+ "by itself, pass it to add_callback as a functools.partial "
1180
+ "object." )
1165
1181
self .callbacks .remove ((func , args , kwargs ))
1166
1182
else :
1167
1183
funcs = [c [0 ] for c in self .callbacks ]
@@ -1175,11 +1191,11 @@ def _timer_set_single_shot(self):
1175
1191
"""Used to set single shot on underlying timer object."""
1176
1192
1177
1193
def _on_timer (self ):
1178
- '''
1194
+ """
1179
1195
Runs all function that have been registered as callbacks. Functions
1180
1196
can return False (or 0) if they should not be called any more. If there
1181
1197
are no callbacks, the timer is automatically stopped.
1182
- '''
1198
+ """
1183
1199
for func , args , kwargs in self .callbacks :
1184
1200
ret = func (* args , ** kwargs )
1185
1201
# docstring above explains why we use `if ret == 0` here,
0 commit comments