[Grok-dev] Re: Understanding unicode

Uli Fouquet uli at gnufix.de
Sun Sep 23 22:06:23 EDT 2007

Hi Sebastian,

Am Sonntag, den 23.09.2007, 20:24 +0200 schrieb Sebastian Ware:
> A recipe for this would be very much appreciated, even if it is a bit  
> cumbersome :) Choice fields with only ascii representations are  
> rather limited with repect to usability. I have had to use ISO-codes  
> instead of country names in a current project and it is not a very  
> good solution.

Simple Choice fields with zc.sourcefactory

Well, you can use Source objects instead of plain vocabularies. The
zc.sourcefactory package which Christian presented on the list three
weeks ago is 'unicodeaware' in that respect, easy to use and allows
quick creation of simple unicode Choice fields::

  >>> import zc.sourcefactory.basic
  >>> class AnimalSource(zc.sourcefactory.basic.BasicSourceFactory):
  ...     def getValues(self):
  ...         return [u'Killer Rabbit', 
  ...                 u'The Legendary Black Beast of Aaaargh',
  ...                 u'Nordafrikanische Küstenseeschwalbe']
  >>> c = Choice(title='Lovely animals', source=AnimalSource())

Don't forget to add an include to your configure.zcml::

  <include package="zc.sourcefactory" />

The zc.sourcefactory package takes care to create unique and valid
tokens (at least as long as you do not mix different types of values).

Simple Choice fields with zope.schema.SimpleVocabulary

The following was explained much shorter one minute ago by Leonardo :-)
So you can skip the rest.

But, finally, I found a way without 'external' packages: set the titles
of the SimpleTerms contained in a Vocabulary.

Say, you have an options dict like this::

  >>> animals = {'killer_rabbit': u'Killer Rabbit',
  ...            'black_beast': u'The Legendary Black Beast of Aaaargh',
  ...            'kuestenseeschwalbe': u'Küstenseeschwalbe'}

where the last entry normally will mean unicode trouble, then you can
create a list of SimpleTerm instances::

  >>> from zope.schema.vocabulary import SimpleVocabulary
  >>> termlist = [SimpleVocabulary.createTerm(token, token, title)
  ...             for token, title in animals.items()]

The classmethod SimpleVocabulary.createTerm() calls the constructor of
zope.schema.vocabulary.SimpleTerm which takes up to three parameters: a
value, a token and a title. While the token must be (7-bit) ASCII and
unique, the title can be unicode. Here we set the value to the same
value as the token.

While the tokens in the select widget lateron will be rendered as
values, the titles will serve as the text displayed.

The termlist can be used to initialize a SimpleVocabulary instance::

  >>> myvocabulary = SimpleVocabulary(termlist)

and the vocabulary in turn can be used to initialize your Choice field,
using the 'vocabulary' keyword::

  >>> c = Choice(title=u'Animals', vocabulary=myvocabulary)

All this can also be written in one expression, of course.

Hope that helps,


More information about the Grok-dev mailing list