Re: [Zope3-Users] Smarter values for values = [ ] in Choice schema fields?

2008-11-06 Thread Hermann Himmelbauer
Am Donnerstag 06 November 2008 17:30:27 schrieb Marius Gedminas:
 On Thu, Nov 06, 2008 at 02:13:56PM +0100, Hermann Himmelbauer wrote:
  I quite often have Choice schema fields in my applications. In many
  cases, these choice fields should have fixed values, thus I do it like
  this:
 
  color = Choice( title=uColor, values=['red', 'green', 'yellow'])
 
  My application then uses the these values for further processing, e.g.:
 
  if color == 'red': stop_traffic()
 
  The problem is, that often it is more appropriate to have one value for
  display, and another for internal processing (e.g. when msgid strings are
  involved, when the program needs specific values etc.)
 
  The only way I found is to set up a vocabulary and use
  SimpleVocabulary.createTerm(key, n, name), however, that's quite tedious,
  as I need to write quite some code, register the vocabulary etc.

 Not really.

  So, perhaps there's a simpler solution? I'd favour something like this:
 
  color = Choice(titel=uColor, values = [('red', 0, uRed), ('green', 1,
  uGreen)])
 
  Is that possible?

 Define a helper function

 def vocabulary(*terms):
 return SimpleVocabulary([SimpleTerm(value, token, title)
  for value, token, title in terms])

 and use it

 color = Choice(title=uColor,
vocabulary=vocabulary(
(0, 'red', u'Red'),
(1, 'green', u'Green'),
))

Ah, that's nice! I did not know that I can actually use a vocabulary object as 
parameter! Thanks!

Best Regards,
Hermann

-- 
[EMAIL PROTECTED]
GPG key ID: 299893C7 (on keyservers)
FP: 0124 2584 8809 EF2A DBF9  4902 64B4 D16B 2998 93C7
___
Zope3-users mailing list
Zope3-users@zope.org
http://mail.zope.org/mailman/listinfo/zope3-users


Re: [Zope3-Users] Smarter values for values = [] in Choice schema fields?

2008-11-06 Thread Marius Gedminas
On Thu, Nov 06, 2008 at 02:13:56PM +0100, Hermann Himmelbauer wrote:
 I quite often have Choice schema fields in my applications. In many cases, 
 these choice fields should have fixed values, thus I do it like this:
 
 color = Choice( title=uColor, values=['red', 'green', 'yellow'])
 
 My application then uses the these values for further processing, e.g.:
 
 if color == 'red': stop_traffic()
 
 The problem is, that often it is more appropriate to have one value for 
 display, and another for internal processing (e.g. when msgid strings are 
 involved, when the program needs specific values etc.)
 
 The only way I found is to set up a vocabulary and use 
 SimpleVocabulary.createTerm(key, n, name), however, that's quite tedious, as 
 I need to write quite some code, register the vocabulary etc.

Not really.

 So, perhaps there's a simpler solution? I'd favour something like this:
 
 color = Choice(titel=uColor, values = [('red', 0, uRed), ('green', 1, 
 uGreen)])
 
 Is that possible?

Define a helper function

def vocabulary(*terms):
return SimpleVocabulary([SimpleTerm(value, token, title)
 for value, token, title in terms])

and use it

color = Choice(title=uColor,
   vocabulary=vocabulary(
   (0, 'red', u'Red'),
   (1, 'green', u'Green'),
   ))

HTH,
Marius Gedminas
-- 
Undergraduates owe their happiness chiefly to the fact that they are no
longer at school...The nonsense which was knocked out of them at school is
all gently put back at Oxford or Cambridge
-- Sir Max Beerbohm (1872-1956)


signature.asc
Description: Digital signature
___
Zope3-users mailing list
Zope3-users@zope.org
http://mail.zope.org/mailman/listinfo/zope3-users