Ignore:
Timestamp:
15 Jan 2010, 11:53:06 (15 years ago)
Author:
uli
Message:

Be a bit more verbose.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • waeup/branches/ulif-importers/src/waeup/utils/converters.txt

    r4815 r4816  
    498498    '1'
    499499
    500 
     500A more complex (but still realistic example) for conversion of
     501:mode:`zope.schema.Choice` fields follows. Here we have a special kind
     502of object, the `Cave` class, and get their `name` attribute as token.
    501503
    502504    >>> class Cave(object):
     
    505507    >>> def tokenizer(cave):
    506508    ...   return cave.name
     509
     510The tokenizer has to be registered as an Cave-to-IToken adapter to
     511keep :mod:`zc.sourcefactory` happy:
    507512
    508513    >>> from zc.sourcefactory.interfaces import IToken
     
    510515    >>> gsm = getGlobalSiteManager()
    511516    >>> gsm.registerAdapter(tokenizer, required=(Cave,), provided=IToken)
     517
     518Now we finally can create two objects of `Cave` and make a field, that
     519gives the choice between these two objects:
    512520
    513521    >>> obj1 = Cave()
     
    530538    ...                required=True)
    531539
     540We get a converter for this field in the usual way. The
     541:meth:`waeup.utils.converters.fromString()` method will return one of
     542the objects if we feed it with ``'Wilma'`` or ``'Fred'``:
     543
    532544    >>> converter = ISchemaTypeConverter(field)
    533545    >>> result = converter.fromString('Wilma')
     
    538550    (True, 'Wilma')
    539551
     552If we use the converter the other way round, it will call the
     553tokenizer above and deliver ``'Wilma'`` or ``'Fred'`` as they are the
     554tokens of the objects in question:
     555
    540556    >>> converter.toString(obj2)
    541557    'Fred'
Note: See TracChangeset for help on using the changeset viewer.