aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMattias Andrée <maandree@operamail.com>2014-07-02 11:42:56 +0200
committerMattias Andrée <maandree@operamail.com>2014-07-02 11:42:56 +0200
commita56d0e10f298bf3d451b3f327e51a530331286fa (patch)
tree575defd469089dd8f615792167fcadec6190e7c7
parentcleanup (diff)
downloadxpybar-a56d0e10f298bf3d451b3f327e51a530331286fa.tar.gz
xpybar-a56d0e10f298bf3d451b3f327e51a530331286fa.tar.bz2
xpybar-a56d0e10f298bf3d451b3f327e51a530331286fa.tar.xz
moved Clocked into util.py
Signed-off-by: Mattias Andrée <maandree@operamail.com>
-rw-r--r--examples/mixed13
-rw-r--r--src/util.py72
2 files changed, 61 insertions, 24 deletions
diff --git a/examples/mixed b/examples/mixed
index 5770dc8..3211ad5 100644
--- a/examples/mixed
+++ b/examples/mixed
@@ -15,15 +15,6 @@ OUTPUT, HEIGHT, YPOS, TOP = 0, 12, 0, True
text = ''
clock_ = Clock(format = '%Y-(%m)%b-%d %T, %a w%V, %Z', sync_to = 0.5)
-class Clocked:
- def __init__(self, *args):
- self.sometimes = Sometimes(*args)
- self.text = self.sometimes()
- def __call__(self, update = False):
- if update:
- self.text = self.sometimes()
- return self.text
-
functions = [ lambda : text
, Clocked(clock_.read, 1)
]
@@ -43,9 +34,7 @@ def start():
bar.invalidate()
def update_per_clock():
- for f in functions:
- if isinstance(f, Clocked):
- f(True)
+ Clocked.update_all(functions)
bar.invalidate()
async(lambda : forever(read_stdin))
diff --git a/src/util.py b/src/util.py
index 1d3d740..e21e074 100644
--- a/src/util.py
+++ b/src/util.py
@@ -125,17 +125,17 @@ class Sometimes:
self.counter = initial
self.last_return = None
- def __call__(self, *args, **kargs):
+ def __call__(self, *args, **kwargs):
'''
Invoke the function, every `interval`:th time
- @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
+ @param args:*? The parameters of the function
+ @param kwargs:**? The named parameters of the function
+ @return :¿R? The return value of the function, the last return if not invoked
'''
rc = self.last_return
if self.counter == 0:
- rc = self.function(*args, **kargs)
+ rc = self.function(*args, **kwargs)
self.last_return = rc
self.counter = self.interval
self.counter -= 1
@@ -162,19 +162,67 @@ class DelayedSometimes:
@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):
+ def f(*args, **kwargs):
self.function = Sometimes(function, interval, initial = delay)
- self.function.last_return = function(*args, **kargs)
+ self.function.last_return = function(*args, **kwargs)
return self.function.last_return
self.function = f
- def __call__(self, *args, **kargs):
+ def __call__(self, *args, **kwargs):
'''
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
+ @param args:*? The parameters of the function
+ @param kwargs:**? 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)
+ return self.function(*args, **kwargs)
+
+
+class Clocked:
+ '''
+ `Sometimes` wrapper that needs explicit re-evaluation before
+ its `Sometimes` functionallity is invoked. That is, it needs
+ a selected number of explicit re-evaluation before the value
+ is actually re-evaluted.
+
+ The rationale for this class is that you may want to update
+ somethings more often than other things, periodically, but
+ you may also want to update other things when certain events
+ occur.
+ '''
+
+ def __init__(self, *args, **kwargs):
+ '''
+ Constructor
+
+ @param args:* Positional arguments for the `Sometimes` constructor
+ @param kwargs:** Keyworkd arguments for the `Sometimes` constructor
+ '''
+ self.sometimes = Sometimes(*args, **kwargs)
+ self.text = self.sometimes()
+
+
+ def __call__(self, update = False):
+ '''
+ Return the most recently evaluated value
+
+ @param update:bool Whether to re-evalute the value and return the new value
+ @return :¿T? The most recently evaluated value
+ '''
+ if update:
+ self.text = self.sometimes()
+ return self.text
+
+
+ @staticmethod
+ def update_all(functions):
+ '''
+ Update all elements in an iteratable that is of the type `Clocked`
+
+ @param functions:itr<¿T?> The iteratable
+ '''
+ for f in functions:
+ if isinstance(f, Clocked):
+ f(True)