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

Source Code for Module paramz.core.updateable

 1  #=============================================================================== 
 2  # Copyright (c) 2014-2015, Max Zwiessele 
 3  # 
 4  # All rights reserved. 
 5  #  
 6  # Redistribution and use in source and binary forms, with or without 
 7  # modification, are permitted provided that the following conditions are met: 
 8  #  
 9  # * Redistributions of source code must retain the above copyright notice, this 
10  #   list of conditions and the following disclaimer. 
11  #  
12  # * Redistributions in binary form must reproduce the above copyright notice, 
13  #   this list of conditions and the following disclaimer in the documentation 
14  #   and/or other materials provided with the distribution. 
15  #  
16  # * Neither the name of paramax nor the names of its 
17  #   contributors may be used to endorse or promote products derived from 
18  #   this software without specific prior written permission. 
19  #  
20  # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 
21  # AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 
22  # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 
23  # DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE 
24  # FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 
25  # DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 
26  # SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 
27  # CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 
28  # OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 
29  # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 
30  #=============================================================================== 
31  from .observable import Observable 
32   
33   
34 -class Updateable(Observable):
35 """ 36 A model can be updated or not. 37 Make sure updates can be switched on and off. 38 """
39 - def __init__(self, *args, **kwargs):
40 super(Updateable, self).__init__(*args, **kwargs)
41
42 - def update_model(self, updates=None):
43 """ 44 Get or set, whether automatic updates are performed. When updates are 45 off, the model might be in a non-working state. To make the model work 46 turn updates on again. 47 48 :param bool|None updates: 49 50 bool: whether to do updates 51 None: get the current update state 52 """ 53 if updates is None: 54 return self._update_on 55 assert isinstance(updates, bool), "updates are either on (True) or off (False)" 56 p = getattr(self, '_highest_parent_', None) 57 def turn_updates(s): 58 s._update_on = updates
59 p.traverse(turn_updates) 60 self.trigger_update()
61
62 - def toggle_update(self):
63 print("deprecated: toggle_update was renamed to update_toggle for easier access") 64 self.update_toggle()
65 - def update_toggle(self):
66 self.update_model(not self.update_model())
67
68 - def trigger_update(self, trigger_parent=True):
69 """ 70 Update the model from the current state. 71 Make sure that updates are on, otherwise this 72 method will do nothing 73 74 :param bool trigger_parent: Whether to trigger the parent, after self has updated 75 """ 76 if not self.update_model() or (hasattr(self, "_in_init_") and self._in_init_): 77 #print "Warning: updates are off, updating the model will do nothing" 78 return 79 self._trigger_params_changed(trigger_parent)
80