Changeset 4815


Ignore:
Timestamp:
15 Jan 2010, 11:42:21 (15 years ago)
Author:
uli
Message:
  • Test toString-conversions.
File:
1 edited

Legend:

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

    r4813 r4815  
    2424
    2525:class:`Converter`
    26 ----------------------
     26------------------
    2727
    2828.. class:: Converter(field)
     
    7575       data.
    7676
     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
    77130
    78131:class:`IntConverter`
     
    101154       Use `strict` to enable/disable validations of transformed
    102155       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
    103170
    104171
     
    171238    u'blah'
    172239
     240And back:
     241
     242    >>> converter.toString(u'blah')
     243    'blah'
     244
    173245Okay, not very surprising. But the field definitions can help also
    174246deliver values, if the given value is missing:
     
    194266
    195267    >>> converter.fromString(None, strict=False) is None
    196     True  
     268    True
    197269
    198270
     
    207279    True
    208280
    209 We can also set a missing type:
     281We can also set a value that indicates missing data (``None`` by default):
    210282
    211283    >>> field = TextLine(title=u'Some Title',
     
    216288    >>> converter.fromString(None)
    217289    u'<NO-VALUE>'
     290
     291And put it back:
     292
     293    >>> converter.toString(u'<NO-VALUE>') is None
     294    True
    218295
    219296
     
    250327    666
    251328
     329And back:
     330
     331    >>> converter.toString(666)
     332    '666'
     333
    252334Okay, not very surprising. But the field definitions can help also
    253335deliver values, if the given value is missing:
     
    275357    True
    276358
     359The 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
    277370If a field is not required, we will get the ``missing_value`` type in
    278371same case:
     
    283376    >>> converter = ISchemaTypeConverter(field)
    284377    >>> converter.fromString(None) is None
     378    True
     379
     380    >>> converter.toString(None) is None
    285381    True
    286382
     
    295391    >>> from zc.sourcefactory.browser.source import FactoredTerms
    296392    >>> from zc.sourcefactory.browser.token import (
    297     ...   fromString, fromUnicode, fromInteger)
     393    ...   fromString, fromUnicode, fromInteger, fromPersistent)
    298394    >>> from zope.component import getGlobalSiteManager
    299395    >>> gsm = getGlobalSiteManager()
     
    302398    >>> gsm.registerAdapter(fromUnicode)
    303399    >>> gsm.registerAdapter(fromInteger)
     400    >>> gsm.registerAdapter(fromPersistent)
    304401
    305402We create a field that also implements ``IChoice``, a regular
     
    331428
    332429    >>> converter.fromString('a')
     430    'a'
     431
     432or values back to strings:
     433
     434    >>> converter.toString('a')
    333435    'a'
    334436
     
    373475Note, that when asking for conversion of Choice fields, you must
    374476deliver the token value of the source/vocabulary. This might be the
    375 same as the actual value, but it it might be not.
     477same as the actual value as string, but it it might be different,
     478depending of the kind of source/vocabulary used.
    376479
    377480Furthermore, when writing 'exporters', i.e. components that turn a
     
    379482token value of a term.
    380483
     484Using 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.