Home | Trees | Indices | Help |
---|
|
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 3133 """ 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 """10141 super(Observable, self).__init__() 42 from .lists_and_dicts import ObserverList 43 self.observers = ObserverList() 44 self._update_on = True45 4850 """ 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)5557 """ 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)7476 """ 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
Home | Trees | Indices | Help |
---|
Generated by Epydoc 3.0.1 on Tue Jul 4 12:00:20 2017 | http://epydoc.sourceforge.net |