aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMattias Andrée <maandree@operamail.com>2014-06-13 01:05:51 +0200
committerMattias Andrée <maandree@operamail.com>2014-06-13 01:05:51 +0200
commitca268e660276479f18d17434bffc3b6de977edcf (patch)
treef2e21fa3a9f5cc090172034ea8c2b0b30cb24ed0
parentadd leap second announcement monitor (diff)
downloadxpybar-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 '')
-rw-r--r--src/util.py37
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)
+