Package paramz :: Package core :: Module observable
[hide private]
[frames] | no frames]

Source Code for Module paramz.core.observable

  1  #=============================================================================== 
  2  # Copyright (c) 2015, Max Zwiessele 
  3  # All rights reserved. 
  4  #  
  5  # Redistribution and use in source and binary forms, with or without 
  6  # modification, are permitted provided that the following conditions are met: 
  7  #  
  8  # * Redistributions of source code must retain the above copyright notice, this 
  9  #   list of conditions and the following disclaimer. 
 10  #  
 11  # * Redistributions in binary form must reproduce the above copyright notice, 
 12  #   this list of conditions and the following disclaimer in the documentation 
 13  #   and/or other materials provided with the distribution. 
 14  #  
 15  # * Neither the name of paramax nor the names of its 
 16  #   contributors may be used to endorse or promote products derived from 
 17  #   this software without specific prior written permission. 
 18  #  
 19  # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 
 20  # AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 
 21  # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 
 22  # DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE 
 23  # FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 
 24  # DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 
 25  # SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 
 26  # CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 
 27  # OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 
 28  # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 
 29  #=============================================================================== 
 30   
 31   
32 -class Observable(object):
33 """ 34 Observable pattern for parameterization. 35 36 This Object allows for observers to register with self and a (bound!) function 37 as an observer. Every time the observable changes, it sends a notification with 38 self as only argument to all its observers. 39 """
40 - def __init__(self, *args, **kwargs):
41 super(Observable, self).__init__() 42 from .lists_and_dicts import ObserverList 43 self.observers = ObserverList() 44 self._update_on = True
45
46 - def set_updates(self, on=True):
47 self._update_on = on
48
49 - def add_observer(self, observer, callble, priority=0):
50 """ 51 Add an observer `observer` with the callback `callble` 52 and priority `priority` to this observers list. 53 """ 54 self.observers.add(priority, observer, callble)
55
56 - def remove_observer(self, observer, callble=None):
57 """ 58 Either (if callble is None) remove all callables, 59 which were added alongside observer, 60 or remove callable `callble` which was added alongside 61 the observer `observer`. 62 """ 63 to_remove = [] 64 for poc in self.observers: 65 _, obs, clble = poc 66 if callble is not None: 67 if (obs is observer) and (callble == clble): 68 to_remove.append(poc) 69 else: 70 if obs is observer: 71 to_remove.append(poc) 72 for r in to_remove: 73 self.observers.remove(*r)
74
75 - def notify_observers(self, which=None, min_priority=None):
76 """ 77 Notifies all observers. Which is the element, which kicked off this 78 notification loop. The first argument will be self, the second `which`. 79 80 .. note:: 81 82 notifies only observers with priority p > min_priority! 83 84 :param min_priority: only notify observers with priority > min_priority 85 if min_priority is None, notify all observers in order 86 """ 87 if self._update_on: 88 if which is None: 89 which = self 90 if min_priority is None: 91 [callble(self, which=which) for _, _, callble in self.observers] 92 else: 93 for p, _, callble in self.observers: 94 if p <= min_priority: 95 break 96 callble(self, which=which)
97
98 - def change_priority(self, observer, callble, priority):
99 self.remove_observer(observer, callble) 100 self.add_observer(observer, callble, priority)
101