Warning: Can't synchronize with repository "(default)" (Unsupported version control system "svn": No module named svn). Look in the Trac log for more information.

Changes between Version 4 and Version 5 of PassingArgumentsToCallables


Ignore:
Timestamp:
05/30/06 16:55:57 (13 years ago)
Author:
godoy
Comment:

Adding "add-ons" to help making lists where the "id" column is not the key that will be used in widgets.

Legend:

Unmodified
Added
Removed
Modified
  • PassingArgumentsToCallables

    v4 v5  
    236236 
    237237Currying is described throughly in the Python Cookbook (c. 16.4, p. 594-597) and is also [http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/52549 available on-line], although not in as pretty a package, nor as in-depth. 
     238 
     239= Add-Ons = 
     240 
     241Sometimes one wants to use a column other than "id" as the ID, i.e., have something else as the first value while using build_list.  These two alternative methods allows that.  Extending this for other needs should be trivial. 
     242 
     243The new build_list is named build_list_with_key and its code is: 
     244 
     245{{{ 
     246@classmethod 
     247def build_list_with_key(self, key, field, *args, **kwargs): 
     248    return [(getattr(a, key), getattr(a, field)) for a in self.select(*args, **kwargs)] 
     249 
     250SQLObject.build_list_with_key = build_list_with_key 
     251}}} 
     252 
     253The new make_list function now can pass a key to build_list_with_key, making it usable with this different value.  The code is: 
     254 
     255{{{ 
     256def make_list_with_key(c, none=None, key = 'id', *args, **kwargs): 
     257    def _call(): 
     258        return [ [], [('', none)] ][none is not None] + \ 
     259               c.build_list_with_key(key, *args, **kwargs) 
     260    return _call 
     261}}} 
     262