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