exact syntax for compute example

184 views
Skip to first unread message

Alex Glaros

unread,
Nov 15, 2013, 7:41:52 PM11/15/13
to
Can anyone please type exact syntax for computed field "personDisplayName" below?

I want to concatenate first_name, a space if possible, and last_name.

auth.settings.extra_fields['auth_user']= [
    Field('personMiddleName','string', label='Middle name'),
    Field('personDisplayName', compute=lambda r: r[first_name] [last_name])
  ]

(I realize field is redundant there is a reason for it.)

thanks!

Alex Glaros

黄祥

unread,
Nov 15, 2013, 8:09:03 PM11/15/13
to web...@googlegroups.com
hm, i think what you need is represent in the field constructor.
e.g. not tested
Field('personDisplayName', represent = lambda personDisplayName, field: '%s %s' % (field.first_name, field.last_name))

best regards,
stifan

Alex Glaros

unread,
Nov 15, 2013, 8:14:18 PM11/15/13
to web...@googlegroups.com
Hi Stifan,

I have a special situation where represent can't work with the business logic.  Has to be a compute.

Any hints for the "compute" syntax?

thanks,

Alex

黄祥

unread,
Nov 15, 2013, 8:27:58 PM11/15/13
to web...@googlegroups.com
i think in compute you must put the row on it (both first_name and last_name) and not sure it can handle string:
e.g. not tested
Field('personDisplayName', compute=lambda r: r[first_name] r[last_name])

another suggestion why not use default? 
or maybe after_insert callback to update the persondisplayname with the combination first_name and last_name value? 
or maybe use on form validation function, assigned the value of persondisplayname base on first_name and last_name form field?

best regards,
stifan

Alex Glaros

unread,
Nov 15, 2013, 8:52:03 PM11/15/13
to web...@googlegroups.com
which ones of those will only work on new record and which ones will work on existing records?

Alex Glaros

unread,
Nov 15, 2013, 8:58:20 PM11/15/13
to web...@googlegroups.com
can't get any of the syntax to work, any hints from this attempt below? (have simplified and removed concatenation)

  Field('personDisplayName', default=lambda r: r'%(first_name)%s')

黄祥

unread,
Nov 15, 2013, 9:01:47 PM11/15/13
to web...@googlegroups.com
default can work both for new insert and update existing.
ref:

form validation function can work both for new insert and update existing.
ref:

callback can work both for new insert and update existing. 
for after new insert, please use : _after_insert 
and 
for after update existing, please use : _after_update

ref:
stifan

黄祥

unread,
Nov 15, 2013, 9:03:15 PM11/15/13
to web...@googlegroups.com
not tested
Field('personDisplayName', default = lambda personDisplayName, field: '%s %s' % (field.first_name, field.last_name) )

best regards,
stifan

Alex Glaros

unread,
Nov 15, 2013, 9:27:59 PM11/15/13
to
it's getting better but received

<lambda>() takes exactly 2 arguments (0 given)

Alex Glaros

unread,
Nov 16, 2013, 3:21:49 AM11/16/13
to web...@googlegroups.com
I gave up in the model but was successful in the form.  Something like:

  db.auth_user(fieldID).update_record(personDisplayName='%(first_name)s %(last_name)s' % form.vars   

thanks for helping me get it

Alex

Niphlod

unread,
Nov 16, 2013, 5:59:43 AM11/16/13
to web...@googlegroups.com
doesn't

Field('personDisplayName', compute=lambda r: "%s %s" % (r[first_name], r[last_name]))

work  ?

黄祥

unread,
Nov 16, 2013, 6:37:24 AM11/16/13
to web...@googlegroups.com
no it's not work, the point i want to show in here is at the first time the code was :
Field('personDisplayName', compute=lambda r: r[first_name] [last_name])
lack of r on [last_name]

best regards,
stifan

Niphlod

unread,
Nov 16, 2013, 7:30:13 AM11/16/13
to web...@googlegroups.com
it's still an invalid python syntax.
correction to the above....


Field('personDisplayName', compute=lambda r: "%s %s" % (r['first_name'], r['last_name']))

should work.

>>> record = dict(first_name='john', last_name='foo')
>>> result = lambda r : "%s %s" % (r['first_name'], r['last_name'])
>>> result(record)
'john foo'
>>>

Alex Glaros

unread,
Nov 17, 2013, 1:44:37 AM11/17/13
to
Thanks guys, it worked.

Field('personDisplayName', compute=lambda r: "%s %s" % (r['first_name'], r['last_name']))

How would you identify the workings of each component, I mean does "%s %s" mean "put two strings together here that are identified after the percent sign?"

Much appreciated!

Alex 

Ron McOuat

unread,
Nov 17, 2013, 3:04:26 AM11/17/13
to web...@googlegroups.com
Yes, a Python feature called string formatting

http://docs.python.org/2/library/stdtypes.html#string-formatting-operations

r['first_name'] and r['last_name'] are processed by their str function because of the %s in "%s %s" the format specifier.
The standalone % is the operator specifying string formatting.
The result of the expression is a string containing the two strings with a space separator.

On Saturday, 16 November 2013 22:41:16 UTC-8, Alex Glaros wrote:
Thanks guys, it worked.

Field('personDisplayName', compute=lambda r: "%s %s" % (r['first_name'], r['last_name']))

How would you identify the workings of each component, I mean does "%s %s" mean "put two strings together here that are identified after the percent sign?"

Much appreciated!

Alex 

Alex Glaros

unread,
Nov 17, 2013, 3:37:54 AM11/17/13
to web...@googlegroups.com
Thanks Ron. I didn't immediately grasp that I should have been learning Python concurrently. Alex
Reply all
Reply to author
Forward
0 new messages