Changeset 4815
- Timestamp:
- 15 Jan 2010, 11:42:21 (15 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
waeup/branches/ulif-importers/src/waeup/utils/converters.txt
r4813 r4815 24 24 25 25 :class:`Converter` 26 ------------------ ----26 ------------------ 27 27 28 28 .. class:: Converter(field) … … 75 75 data. 76 76 77 .. method:: toString(value[, strict=True]) 78 79 Return `value` as string or ``None``. 80 81 If `value` is ``None`` (or field's `missing_value`), ``None`` 82 is returned. 83 84 If the value is not valid for the applied field an exception 85 might be raised. 86 87 Use `strict` to enable/disable validations of input data. 88 89 90 :class:`BoolConverter` 91 --------------------- 92 93 .. class:: BoolConverter(field) 94 95 Create a converter for boolean fields. 96 97 `field` must be an object implementing 98 ``zope.schema.interfaces.IBool``. 99 100 Instances of this class are available as adapters from :mod:`IBool` 101 to :mod:`ISchemaTypeConverter`. 102 103 .. method:: adapts(zope.schema.IBool) 104 105 .. method:: provides(waeup.interfaces.ISchemaTypeConverter) 106 107 .. method:: fromString(string[, strict=True]) 108 109 Compute an integer number value from the given `string`. In 110 general '1', 'true', and 'yes' are interpreted as ``True``, 111 ``None`` is interpreted as 'no value' and all other values are 112 interpreted as ``True``. 113 114 Use `strict` to enable/disable validations of transformed 115 data. 116 117 .. method:: toString(value[, strict=True]) 118 119 Return `value` as string or ``None``. 120 121 If `value` is ``None`` (or field's `missing_value`), ``None`` 122 is returned. Otherwise ``0`` or ``1`` are returned. 123 124 If the value is not valid for the applied field (i.e. not an 125 integer or `missing_value`) an exception 126 might be raised. 127 128 Use `strict` to enable/disable validations of input data. 129 77 130 78 131 :class:`IntConverter` … … 101 154 Use `strict` to enable/disable validations of transformed 102 155 data. 156 157 .. method:: toString(value[, strict=True]) 158 159 Return `value` as string or ``None``. 160 161 If `value` is ``None`` (or field's `missing_value`), ``None`` 162 is returned. 163 164 If the value is not valid for the applied field (i.e. not an 165 integer or `missing_value`) an exception 166 might be raised. 167 168 Use `strict` to enable/disable validations of input data. 169 103 170 104 171 … … 171 238 u'blah' 172 239 240 And back: 241 242 >>> converter.toString(u'blah') 243 'blah' 244 173 245 Okay, not very surprising. But the field definitions can help also 174 246 deliver values, if the given value is missing: … … 194 266 195 267 >>> converter.fromString(None, strict=False) is None 196 True 268 True 197 269 198 270 … … 207 279 True 208 280 209 We can also set a missing type:281 We can also set a value that indicates missing data (``None`` by default): 210 282 211 283 >>> field = TextLine(title=u'Some Title', … … 216 288 >>> converter.fromString(None) 217 289 u'<NO-VALUE>' 290 291 And put it back: 292 293 >>> converter.toString(u'<NO-VALUE>') is None 294 True 218 295 219 296 … … 250 327 666 251 328 329 And back: 330 331 >>> converter.toString(666) 332 '666' 333 252 334 Okay, not very surprising. But the field definitions can help also 253 335 deliver values, if the given value is missing: … … 275 357 True 276 358 359 The same for the inverse operation: 360 361 >>> converter.toString(None) 362 Traceback (most recent call last): 363 ... 364 RequiredMissing 365 366 >>> converter.toString(None, strict=False) is None 367 True 368 369 277 370 If a field is not required, we will get the ``missing_value`` type in 278 371 same case: … … 283 376 >>> converter = ISchemaTypeConverter(field) 284 377 >>> converter.fromString(None) is None 378 True 379 380 >>> converter.toString(None) is None 285 381 True 286 382 … … 295 391 >>> from zc.sourcefactory.browser.source import FactoredTerms 296 392 >>> from zc.sourcefactory.browser.token import ( 297 ... fromString, fromUnicode, fromInteger )393 ... fromString, fromUnicode, fromInteger, fromPersistent) 298 394 >>> from zope.component import getGlobalSiteManager 299 395 >>> gsm = getGlobalSiteManager() … … 302 398 >>> gsm.registerAdapter(fromUnicode) 303 399 >>> gsm.registerAdapter(fromInteger) 400 >>> gsm.registerAdapter(fromPersistent) 304 401 305 402 We create a field that also implements ``IChoice``, a regular … … 331 428 332 429 >>> converter.fromString('a') 430 'a' 431 432 or values back to strings: 433 434 >>> converter.toString('a') 333 435 'a' 334 436 … … 373 475 Note, that when asking for conversion of Choice fields, you must 374 476 deliver the token value of the source/vocabulary. This might be the 375 same as the actual value, but it it might be not. 477 same as the actual value as string, but it it might be different, 478 depending of the kind of source/vocabulary used. 376 479 377 480 Furthermore, when writing 'exporters', i.e. components that turn a … … 379 482 token value of a term. 380 483 484 Using the ;meth:`toString()` will deliver the token for us: 485 486 >>> converter.toString(1) 487 '1' 488 489 >>> from waeup.interfaces import ICourse 490 >>> from zope.schema import getFields 491 >>> field = getFields(ICourse)['semester'] 492 493 >>> converter = ISchemaTypeConverter(field) 494 >>> converter.fromString('0') 495 0 496 497 >>> converter.toString(1) 498 '1' 499 500 501 502 >>> class Cave(object): 503 ... name = 'Home sweet home' 504 505 >>> def tokenizer(cave): 506 ... return cave.name 507 508 >>> from zc.sourcefactory.interfaces import IToken 509 >>> from zope.component import getGlobalSiteManager 510 >>> gsm = getGlobalSiteManager() 511 >>> gsm.registerAdapter(tokenizer, required=(Cave,), provided=IToken) 512 513 >>> obj1 = Cave() 514 >>> obj1.name = 'Wilma' 515 516 >>> obj2 = Cave() 517 >>> obj2.name = 'Fred' 518 519 >>> from zc.sourcefactory.basic import BasicSourceFactory 520 >>> class CustomSource(BasicSourceFactory): 521 ... def getValues(self): 522 ... return [obj1, obj2] 523 ... 524 ... def getTitle(self, value): 525 ... return value.name 526 527 >>> field = Choice(title=u'Some cave', 528 ... default=obj1, 529 ... source=CustomSource(), 530 ... required=True) 531 532 >>> converter = ISchemaTypeConverter(field) 533 >>> result = converter.fromString('Wilma') 534 >>> result 535 <Cave object at 0x...> 536 537 >>> result is obj1, result.name 538 (True, 'Wilma') 539 540 >>> converter.toString(obj2) 541 'Fred'
Note: See TracChangeset for help on using the changeset viewer.