To get it down to it down to one function, how about: def compute_geoCode(r, component): g = geocoders.Google() place, (lat, lng) = g.geocode(r.name) return eval(component)
db.cities.full_address.compute = lambda r: compute_geoCode(r, 'place') db.cities.lat.compute = lambda r: compute_geoCode(r, 'lat') db.cities.lgt.compute = lambda r: compute_geoCode(r, 'lng') Anthony On Thursday, May 17, 2012 4:35:46 AM UTC-4, Gabriella Canavesi wrote: > > Hi all, > I have a simple table with 3 computed fields, the function that set > their values is almost the same. However, unfortunately I had to set up > three different functions because I didn't find a better approach. > The code: > db.define_table('cities', > Field('name', 'string', requires=IS_TRIM()), > Field('full_address', 'string',requires=IS_TRIM()), > Field("lat", "double", label=T('Latitude')), > Field("lgt", "double", label=T('Longitude'))) > > db.cities.full_address.compute = compute_geoCode_place > db.cities.lat.compute = compute_geoCode_lat > db.cities.lgt.compute = compute_geoCode_lng > > def compute_geoCode_place(r): > g = geocoders.Google() > place, (lat, lng) = g.geocode(r.name) > return place > > def compute_geoCode_lat(r): > g = geocoders.Google() > place, (lat, lng) = g.geocode(r.name) > return lat > > def compute_geoCode_lng(r): > g = geocoders.Google() > place, (lat, lng) = g.geocode(r.name) > return lng > > What I would like to find is a way to fire the execution of a function > when the form is submitted. More or less something like a compute action > at table level. > > Regards, > > -- > Paolo >