Changeset 71
- Timestamp:
- 04/22/05 15:14:36
- Files:
-
- trunk/__init__.py (modified) (4 diffs)
- trunk/doc/modeling.html (modified) (2 diffs)
- trunk/zoo.py (modified) (2 diffs)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
trunk/__init__.py
r70 r71 164 164 Data descriptor for Unit data which will persist in storage. 165 165 166 pre, post: Override these with functions to provide custom behaviors167 upon attribute modification. They are not called if sandbox is None.168 If you need a behavior which fires regardless, you should override169 __set__. They also are not called if the new value matches the170 existing value. Again, override __set__ if you need an exception.171 172 166 hints: A dictionary which provides named hints to Storage Managers 173 167 concerning the nature of the data. A common use, for example, … … 180 174 """ 181 175 182 pre = None183 post = None184 185 176 def __init__(self, type=unicode, index=False, hints=None, key=None): 186 177 if type.__name__ == 'FixedPoint': … … 205 196 if self.coerce: 206 197 value = self.coerce(unit, value) 207 208 oldvalue = unit._properties[self.key] 209 # This test is expensive, but it saves us a lot of 210 # unnecessary save() operations later on. 211 if value != oldvalue: 212 if unit.sandbox and self.pre: 213 self.pre(unit, value) 214 unit._properties[self.key] = value 215 if unit.sandbox and self.post: 216 self.post(unit, value) 198 unit._properties[self.key] = value 217 199 218 200 def coerce(self, unit, value): … … 273 255 sandbox: The sandbox in which the Unit "lives". Also serves as a flag 274 256 indicating whether this Unit has finished the initial creation 275 process. While sandbox is None, pre and post descriptor functions 276 will not be called. 257 process. 277 258 278 259 Sandboxes receive Units during recall() and memorize(); trunk/doc/modeling.html
r70 r71 168 168 169 169 <h4>Triggers</h4> 170 <p>In addition, each UnitProperty has a <tt>pre</tt> and <tt>post</tt> 171 attribute, which default to None. If you override these with methods 172 in a subclass of <tt>UnitProperty</tt>, they will be called when setting 173 a new value for that property, either before (pre) or after (post) the 174 new value is set. For example: 170 <p>Triggers are behaviors which fire when the value of a Unit Property is 171 changed. You can override a UnitProperty's __set__ method to achieve this 172 in Dejavu. For example: 175 173 <pre>class DatedProperty(UnitProperty): 176 def post(self, unit, value): 174 def __set__(self, unit, value): 175 UnitProperty.__set__(self, unit, value) 177 176 unit.Date = datetime.datetime.now().replace(microsecond=0) 178 177 parent = unit.first(Forum) … … 189 188 190 189 associate(Topic, 'ForumID', Forum, 'ID')</pre> 191 In this example, whenever Topic().Content is set, the <tt> post</tt>190 In this example, whenever Topic().Content is set, the <tt>__set__</tt> 192 191 method will be called and the object's <tt>Date</tt> attribute will 193 192 be modified. Then, the Topic's parent Forum is looked up and <i>its</i> trunk/zoo.py
r70 r71 27 27 28 28 class EscapeProperty(UnitProperty): 29 def post(self, unit, value): 29 def __set__(self, unit, value): 30 UnitProperty.__set__(self, unit, value) 30 31 z = unit.first(Zoo) 31 32 if z: … … 39 40 LastEscape = EscapeProperty(datetime.datetime) 40 41 Lifespan = UnitProperty(float, hints={'bytes': 4}) 42 43 def in_first_zoo(self): 44 return (self.PreviousZoo and self.PreviousZoo[0] == self.ZooID) 41 45 42 46 associate(Zoo, 'ID', Animal, 'ZooID')
