Contact: fumanchu@aminus.org

Log in as guest/dejavu to create tickets

I think I've seen this ORM somewhere before...

Changeset 71

Show
Ignore:
Timestamp:
04/22/05 15:14:36
Author:
fumanchu
Message:

Removed Unit.pre and Unit.post. Override set now to get the same effect.

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/__init__.py

    r70 r71  
    164164    Data descriptor for Unit data which will persist in storage. 
    165165     
    166     pre, post: Override these with functions to provide custom behaviors 
    167         upon attribute modification. They are not called if sandbox is None. 
    168         If you need a behavior which fires regardless, you should override 
    169         __set__. They also are not called if the new value matches the 
    170         existing value. Again, override __set__ if you need an exception. 
    171      
    172166    hints: A dictionary which provides named hints to Storage Managers 
    173167        concerning the nature of the data. A common use, for example, 
     
    180174    """ 
    181175     
    182     pre = None 
    183     post = None 
    184      
    185176    def __init__(self, type=unicode, index=False, hints=None, key=None): 
    186177        if type.__name__ == 'FixedPoint': 
     
    205196        if self.coerce: 
    206197            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 
    217199     
    218200    def coerce(self, unit, value): 
     
    273255    sandbox: The sandbox in which the Unit "lives". Also serves as a flag 
    274256        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. 
    277258         
    278259        Sandboxes receive Units during recall() and memorize(); 
  • trunk/doc/modeling.html

    r70 r71  
    168168 
    169169<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 
     171changed. You can override a UnitProperty's __set__ method to achieve this 
     172in Dejavu. For example: 
    175173<pre>class DatedProperty(UnitProperty): 
    176     def post(self, unit, value): 
     174    def __set__(self, unit, value): 
     175        UnitProperty.__set__(self, unit, value) 
    177176        unit.Date = datetime.datetime.now().replace(microsecond=0) 
    178177        parent = unit.first(Forum) 
     
    189188 
    190189associate(Topic, 'ForumID', Forum, 'ID')</pre> 
    191 In this example, whenever Topic().Content is set, the <tt>post</tt> 
     190In this example, whenever Topic().Content is set, the <tt>__set__</tt> 
    192191method will be called and the object's <tt>Date</tt> attribute will 
    193192be modified. Then, the Topic's parent Forum is looked up and <i>its</i> 
  • trunk/zoo.py

    r70 r71  
    2727 
    2828class EscapeProperty(UnitProperty): 
    29     def post(self, unit, value): 
     29    def __set__(self, unit, value): 
     30        UnitProperty.__set__(self, unit, value) 
    3031        z = unit.first(Zoo) 
    3132        if z: 
     
    3940    LastEscape = EscapeProperty(datetime.datetime) 
    4041    Lifespan = UnitProperty(float, hints={'bytes': 4}) 
     42     
     43    def in_first_zoo(self): 
     44        return (self.PreviousZoo and self.PreviousZoo[0] == self.ZooID) 
    4145 
    4246associate(Zoo, 'ID', Animal, 'ZooID')