Package horizons :: Package util :: Module living
[hide private]
[frames] | no frames]

Source Code for Module horizons.util.living

 1  # ################################################### 
 2  # Copyright (C) 2008-2017 The Unknown Horizons Team 
 3  # team@unknown-horizons.org 
 4  # This file is part of Unknown Horizons. 
 5  # 
 6  # Unknown Horizons is free software; you can redistribute it and/or modify 
 7  # it under the terms of the GNU General Public License as published by 
 8  # the Free Software Foundation; either version 2 of the License, or 
 9  # (at your option) any later version. 
10  # 
11  # This program is distributed in the hope that it will be useful, 
12  # but WITHOUT ANY WARRANTY; without even the implied warranty of 
13  # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the 
14  # GNU General Public License for more details. 
15  # 
16  # You should have received a copy of the GNU General Public License 
17  # along with this program; if not, write to the 
18  # Free Software Foundation, Inc., 
19  # 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA 
20  # ################################################### 
21   
22  from typing import Any, Optional 
23   
24   
25 -class livingProperty:
26 """livingProperty's are used to assign properties to a class, similar to python properties. 27 The extra benefit with livingPropertys is, that they will call the previously assigned 28 instances' end() function one being replaced. Note that all classes that are assigned 29 to a livingProperty should subclass the LivingObject class, to ensure the existence of 30 the end() method. 31 The main purpose of the livingProperty is to ensure the correct deletion of objects, 32 so Classes that derive from the LivingObject class will usually have all their __del__ 33 code in the end() method, to ensure it gets called upon being overwritten, even if 34 other references to it exist (which should not!). 35 Here is a small example on the usage: 36 37 class Livetest: 38 prop1 = new livingProperty() 39 40 def __init__(self): 41 prop1 = new TestObj() 42 prop1 = new Test2Obj() // TestObj().end() is called 43 prop1 = new TestObj() // Testobj2().end() is called 44 45 class TestObj(LivingProperty): 46 def end(): 47 print "TestObj end" 48 49 class Testobj2(LivingProperty): 50 def end(): 51 print "TestObj2 end" 52 53 This would result in the following output: 54 TestObj end 55 TestObj2 end 56 """ 57
58 - def __init__(self):
59 self.__value = None # type: Optional[Any]
60
61 - def __get__(self, obj, cls=None):
62 return self.__value
63
64 - def __set__(self, obj, value):
65 if hasattr(self.__value, 'end'): 66 self.__value.end() 67 self.__value = value
68
69 - def __delete__(self, obj):
70 self.__set__(obj, None)
71 72
73 -class LivingObject:
74 """This class is intended to be used with the livingProperty to ensure all variables 75 are safely deinited when intended by the programmer. The livingProperty calls the 76 livingObject's end() function to deinit the object. This mostly replacing the __del__ 77 function, as its behavior is not well behaved.""" 78
79 - def end(self):
80 """Put all the code the object needs to end safely here. Make sure it always 81 contains the super(YOUROBJECT, self).end() call, to ensure all parentobjects are 82 deinited correctly.""" 83 pass
84