diff options
author | Mattias Andrée <maandree@operamail.com> | 2014-06-13 01:05:51 +0200 |
---|---|---|
committer | Mattias Andrée <maandree@operamail.com> | 2014-06-13 01:05:51 +0200 |
commit | ca268e660276479f18d17434bffc3b6de977edcf (patch) | |
tree | f2e21fa3a9f5cc090172034ea8c2b0b30cb24ed0 /src/util.py | |
parent | add leap second announcement monitor (diff) | |
download | xpybar-ca268e660276479f18d17434bffc3b6de977edcf.tar.gz xpybar-ca268e660276479f18d17434bffc3b6de977edcf.tar.bz2 xpybar-ca268e660276479f18d17434bffc3b6de977edcf.tar.xz |
add DelayedSometimes
Signed-off-by: Mattias Andrée <maandree@operamail.com>
Diffstat (limited to 'src/util.py')
-rw-r--r-- | src/util.py | 37 |
1 files changed, 37 insertions, 0 deletions
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) + |