diff options
| author | Mattias Andrée <maandree@operamail.com> | 2014-03-03 20:44:07 +0100 | 
|---|---|---|
| committer | Mattias Andrée <maandree@operamail.com> | 2014-03-03 20:44:07 +0100 | 
| commit | c896626727df7d650df27d6cae369260e5cb8ae8 (patch) | |
| tree | 5155af17d53b9c09cb1a7bafee5b246ad62ef5f8 /src | |
| parent | add network (diff) | |
| download | xpybar-c896626727df7d650df27d6cae369260e5cb8ae8.tar.gz xpybar-c896626727df7d650df27d6cae369260e5cb8ae8.tar.bz2 xpybar-c896626727df7d650df27d6cae369260e5cb8ae8.tar.xz | |
add Sometimes
Signed-off-by: Mattias Andrée <maandree@operamail.com>
Diffstat (limited to 'src')
| -rw-r--r-- | src/util.py | 35 | 
1 files changed, 35 insertions, 0 deletions
| diff --git a/src/util.py b/src/util.py index 6b43e83..0ed41f8 100644 --- a/src/util.py +++ b/src/util.py @@ -94,3 +94,38 @@ def reduce(f, items):          rc = f(rc, items[i])      return rc + +class Sometimes: +    ''' +    Function wrapper for only actually invoking +    the function every n:th time, where n is a +    customisable parameter +    ''' +     +    def __init__(self, function, interval, initial = 0): +        ''' +        Constructor +         +        @param  function:(*?)→¿R?  The function +        @param  interval:int       Invoke the function every `interval`:th time +        @param  initial:int        The of times needed to invoke before actual invocation +        ''' +        self.function = function +        self.interval = interval +        self.counter = initial +     +    def __call__(self, *args, **kargs): +        ''' +        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, `None` if not invoked +        ''' +        rc = None +        if self.counter == 0: +            rc = self.function(*args, **kargs) +            self.counter = self.interval +        self.counter -= 1 +        return rc + | 
