Changeset 382

Show
Ignore:
Timestamp:
07/29/08 10:48:03 (4 years ago)
Author:
ged
Message:

- extra args and kwargs to Synonym and ColumnProperty are forwarded to their

underlying constructs

- fix ColumnProperty docstring

Location:
elixir/trunk
Files:
2 modified

Legend:

Unmodified
Added
Removed
  • elixir/trunk/CHANGES

    r380 r382  
    66  resorting to using the ugly "column_kwargs" (patch from Jason R. Coombs, 
    77  closes #39). 
     8- extra args and kwargs to Synonym and ColumnProperty are forwarded to their 
     9  underlying constructs 
    810 
    911 
  • elixir/trunk/elixir/properties.py

    r362 r382  
    6767    def create_properties(self): 
    6868        ''' 
    69         Subclasses may override this method to add properties to the involved  
     69        Subclasses may override this method to add properties to the involved 
    7070        entity. 
    7171        ''' 
     
    128128 
    129129        # delete the original attribute so that it doesn't interfere with 
    130         # SQLAlchemy. 
     130        # SQLAlchemy. Note that getattr and delattr are not symmetrical: 
     131        # getattr look up in parent classes, while delattr must be called on 
     132        # the exact class holding the attribute. 
    131133        if name in entity.__dict__: 
    132134            delattr(entity, name) 
     
    149151    ''' 
    150152 
    151     def __init__(self, prop): 
    152         super(GenericProperty, self).__init__() 
     153    def __init__(self, prop, *args, **kwargs): 
     154        super(GenericProperty, self).__init__(*args, **kwargs) 
    153155        self.prop = prop 
     156        self.args = args 
     157        self.kwargs = kwargs 
    154158 
    155159    def create_properties(self): 
     
    162166 
    163167    def evaluate_property(self, prop): 
     168        if self.args or self.kwargs: 
     169            raise Exception('superfluous arguments passed to GenericProperty') 
    164170        return prop 
    165171 
     
    170176    ``column_property``'s. 
    171177 
    172     It takes a single argument, which is a function (often 
    173     given as an anonymous lambda) taking one argument and returning the 
    174     desired (scalar-returning) SQLAlchemy ClauseElement. That function will be 
    175     called whenever the entity table is completely defined, and will be given 
    176     the .c attribute of the entity as argument (as a way to access the entity 
    177     columns). The ColumnProperty will first wrap your ClauseElement in a label 
    178     with the same name as the property, then wrap that in a column_property. 
     178    It takes a function (often given as an anonymous lambda) as its first 
     179    argument. Other arguments and keyword arguments are forwarded to the 
     180    column_property construct. That first-argument function must accept exactly 
     181    one argument and must return the desired (scalar-returning) SQLAlchemy 
     182    ClauseElement. 
     183 
     184    The function will be called whenever the entity table is completely 
     185    defined, and will be given 
     186    the .c attribute of the table of the entity as argument (as a way to 
     187    access the entity columns). The ColumnProperty will first wrap your 
     188    ClauseElement in an 
     189    "empty" label (ie it will be labelled automatically during queries), 
     190    then wrap that in a column_property. 
    179191 
    180192    .. sourcecode:: python 
     
    183195            quantity = Field(Float) 
    184196            unit_price = Field(Numeric) 
    185             price = ColumnProperty(lambda c: c.quantity * c.unit_price) 
     197            price = ColumnProperty(lambda c: c.quantity * c.unit_price, 
     198                                   deferred=True) 
    186199 
    187200    Please look at the `corresponding SQLAlchemy 
     
    191204 
    192205    def evaluate_property(self, prop): 
    193         return column_property(prop.label(None)) 
     206        return column_property(prop.label(None), *self.args, **self.kwargs) 
    194207 
    195208 
     
    216229 
    217230    def evaluate_property(self, prop): 
    218         return synonym(prop) 
     231        return synonym(prop, *self.args, **self.kwargs) 
    219232 
    220233#class Composite(GenericProperty):