From ca268e660276479f18d17434bffc3b6de977edcf Mon Sep 17 00:00:00 2001 From: Mattias Andrée Date: Fri, 13 Jun 2014 01:05:51 +0200 Subject: add DelayedSometimes MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Mattias Andrée --- src/util.py | 37 +++++++++++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) diff --git a/src/util.py b/src/util.py index ef7cd52..1d3d740 100644 --- a/src/util.py +++ b/src/util.py @@ -141,3 +141,40 @@ class Sometimes: self.counter -= 1 return rc + +class DelayedSometimes: + ''' + Function wrapper for only actually invoking + the function every n:th time, where n is a + customisable parameter, with an added + functionallity: the actual invocation will + take place at the first invocation and then + a number of invocations is required before + the second actual invocation after which + the normal interval will be used + ''' + + def __init__(self, function, delay, interval): + ''' + Constructor + + @param function:(*?)→¿R? The functiony + @param delay:int The of times needed to invoke between the first and second actual invocation + @param interval:int Invoke the function every `interval`:th time + ''' + def f(*args, **kargs): + self.function = Sometimes(function, interval, initial = delay) + self.function.last_return = function(*args, **kargs) + return self.function.last_return + self.function = f + + def __call__(self, *args, **kargs): + ''' + Invoke the function when scheduled + + @param args:*? The parameters of the function + @param kargs:**? The named parameters of the function + @return :¿R? The return value of the function, the last return if not invoked + ''' + return self.function(*args, **kargs) + -- cgit v1.2.3-70-g09d2