Changeset 4817
- Timestamp:
- 15 Jan 2010, 12:02:14 (15 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
waeup/branches/ulif-importers/src/waeup/utils/converters.txt
r4816 r4817 294 294 True 295 295 296 Bool field converters 297 --------------------- 298 299 We create a field that also implements ``IBool``, a regular 300 ``Bool`` field: 301 302 >>> from zope.schema import Bool 303 >>> field = Bool(title=u'Some truth', 304 ... default=True, 305 ... required=True) 306 307 Now we can get a converter for this field by explicitly creating a 308 :class:`BoolConverter` instance: 309 310 >>> from waeup.utils.converters import BoolConverter 311 >>> converter = BoolConverter(field) 312 313 Or we can just grab a registered adapter: 314 315 >>> from waeup.interfaces import ISchemaTypeConverter 316 >>> converter = ISchemaTypeConverter(field) 317 318 This will select the correct converter for us automatically. 319 320 >>> converter 321 <waeup.utils.converters.BoolConverter object at 0x...> 322 323 Now we can convert strings to this type: 324 325 >>> converter.fromString('yes') 326 True 327 328 >>> converter.fromString('Yes') 329 True 330 331 >>> converter.fromString('yEs') 332 True 333 334 >>> converter.fromString('1') 335 True 336 337 >>> converter.fromString('True') 338 True 339 340 >>> converter.fromString('0') 341 False 342 343 >>> converter.fromString('no') 344 False 345 346 >>> converter.fromString('false') 347 False 348 349 350 And back: 351 352 >>> converter.toString(True) 353 '1' 354 355 >>> converter.toString(False) 356 '0' 357 358 Okay, not very surprising. But the field definitions can help also 359 deliver values, if the given value is missing: 360 361 >>> converter.fromString(None) 362 True 363 364 ``None`` is not an acceptable value for fields which are required but 365 provide no default: 366 367 >>> field = Bool(title=u'Some Truth', 368 ... required=True) 369 370 >>> converter = ISchemaTypeConverter(field) 371 >>> converter.fromString(None) 372 Traceback (most recent call last): 373 ... 374 RequiredMissing 375 376 If we want to avoid this type of exception (and risk non-applicable 377 data to be stored), we can use the ``strict`` parameter of 378 ``fromString()``: 379 380 >>> converter.fromString(None, strict=False) is None 381 True 382 383 The same for the inverse operation: 384 385 >>> converter.toString(None) 386 Traceback (most recent call last): 387 ... 388 RequiredMissing 389 390 >>> converter.toString(None, strict=False) is None 391 True 392 393 394 If a field is not required, we will get the ``missing_value`` type in 395 same case: 396 397 >>> field = Bool(title=u'Some Title', 398 ... required=False) 399 400 >>> converter = ISchemaTypeConverter(field) 401 >>> converter.fromString(None) is None 402 True 403 404 >>> converter.toString(None) is None 405 True 406 296 407 297 408 Int field converters … … 307 418 308 419 Now we can get a converter for this field by explicitly creating a 309 :class:` TextConverter` instance:420 :class:`IntConverter` instance: 310 421 311 422 >>> from waeup.utils.converters import IntConverter
Note: See TracChangeset for help on using the changeset viewer.