Changeset 4816 for waeup/branches
- Timestamp:
- 15 Jan 2010, 11:53:06 (15 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
waeup/branches/ulif-importers/src/waeup/utils/converters.txt
r4815 r4816 498 498 '1' 499 499 500 500 A more complex (but still realistic example) for conversion of 501 :mode:`zope.schema.Choice` fields follows. Here we have a special kind 502 of object, the `Cave` class, and get their `name` attribute as token. 501 503 502 504 >>> class Cave(object): … … 505 507 >>> def tokenizer(cave): 506 508 ... return cave.name 509 510 The tokenizer has to be registered as an Cave-to-IToken adapter to 511 keep :mod:`zc.sourcefactory` happy: 507 512 508 513 >>> from zc.sourcefactory.interfaces import IToken … … 510 515 >>> gsm = getGlobalSiteManager() 511 516 >>> gsm.registerAdapter(tokenizer, required=(Cave,), provided=IToken) 517 518 Now we finally can create two objects of `Cave` and make a field, that 519 gives the choice between these two objects: 512 520 513 521 >>> obj1 = Cave() … … 530 538 ... required=True) 531 539 540 We get a converter for this field in the usual way. The 541 :meth:`waeup.utils.converters.fromString()` method will return one of 542 the objects if we feed it with ``'Wilma'`` or ``'Fred'``: 543 532 544 >>> converter = ISchemaTypeConverter(field) 533 545 >>> result = converter.fromString('Wilma') … … 538 550 (True, 'Wilma') 539 551 552 If we use the converter the other way round, it will call the 553 tokenizer above and deliver ``'Wilma'`` or ``'Fred'`` as they are the 554 tokens of the objects in question: 555 540 556 >>> converter.toString(obj2) 541 557 'Fred'
Note: See TracChangeset for help on using the changeset viewer.