Changeset 4834 for waeup


Ignore:
Timestamp:
17 Jan 2010, 14:13:51 (15 years ago)
Author:
uli
Message:
  • Define a unified 'None' value for string imports.
  • Add docstrings.
  • Modify choice converter to cache values if possible. This speeds up conversion by about factor 10 and more. Also imports are 10 times faster now.
File:
1 edited

Legend:

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

    r4814 r4834  
    1010from zope.schema.interfaces import IBool, IText, IInt, IChoice
    1111from waeup.interfaces import ISchemaTypeConverter
     12
     13# If a string has this value, it is considered as 'missing_value' or None.
     14NONE_STRING_VALUE = ''
    1215
    1316class Converter(grok.Adapter):
     
    3235        """
    3336        result = None
     37        if string is NONE_STRING_VALUE:
     38            string = None
    3439        if string is None:
    3540            if self.context.required is True:
     
    5459
    5560class BoolConverter(Converter):
     61    """A converter for zope.schema.Bool fields.
     62    """
    5663    grok.context(IBool)
    5764    grok.provides(ISchemaTypeConverter)
    5865
    5966    def _convertValueFromString(self, string):
     67        if string is NONE_STRING_VALUE:
     68            string = None
    6069        if string is None:
    6170            return None
     
    7281   
    7382class TextConverter(Converter):
     83    """A converter for zope.schema.interfaces.IText fields.
     84    """
    7485    grok.context(IText)
    7586    grok.provides(ISchemaTypeConverter)
     
    7990
    8091class IntConverter(Converter):
     92    """A converter for zope.schema.Int fields.
     93    """
    8194    grok.context(IInt)
    8295    grok.provides(ISchemaTypeConverter)
     
    8699
    87100class ChoiceConverter(Converter):
     101    """A converter for zope.schema.Choice fields.
     102    """
    88103    grok.context(IChoice)
    89104    grok.provides(ISchemaTypeConverter)
    90105
     106    tokens = None
     107    values = None
     108   
    91109    def __init__(self, context):
    92110        self.context = context
    93         try:
    94             self.terms = getMultiAdapter(
    95                 (self.context.source, TestRequest()), ITerms)
    96         except:
    97             self.terms = None
     111        if not hasattr(self.context.source, 'factory'):
     112            try:
     113                self.terms = getMultiAdapter(
     114                    (self.context.source, TestRequest()), ITerms)
     115            except:
     116                self.terms = None
     117
     118            return
     119        if not hasattr(self.context.source.factory, 'getToken'):
     120            return
     121        # For expensive token/key lookups we create a 'cache'
     122        # here. This speeds up mass operations with many conversions
     123        # by factor 10 or more.
     124       
     125        # Mapping token -> value
     126        self.tokens = dict([(self.context.source.factory.getToken(x), x)
     127                            for x in self.context.source.factory.getValues()])
     128        # Mapping value -> token
     129        self.values = dict([(y,x) for x,y in self.tokens.items()])
    98130
    99131    def _convertValueFromString(self, string):
     132        if self.tokens is not None:
     133            return self.tokens[string]
    100134        if self.terms is not None:
    101135            return self.terms.getValue(string)
     
    103137
    104138    def _convertValueToString(self, value):
     139        if self.values is not None:
     140            return self.values[value]
    105141        if self.terms is not None:
    106142            return self.terms.getTerm(value).token
    107143        return str(value)
     144
     145    def fromString(self, string=None, strict=False):
     146        """Convert ``string`` to value according to assigned field type.
     147
     148        We change the default for ``strict``: this disables extra
     149        validation checks for Choice fields and saves lots of time. If
     150        a string/value is out of allowed range we get a value or key
     151        error anyway.
     152        """
     153        return super(ChoiceConverter, self).fromString(string=string,
     154                                                       strict=strict)
     155
     156    def toString(self, value, strict=False):
     157        """Convert ``value`` to string according to assigned field type.
     158
     159        We change the default for ``strict``: this disables extra
     160        validation checks for Choice fields and saves lots of time. If
     161        a string/value is out of allowed range we get a value or key
     162        error anyway.
     163        """
     164        return super(ChoiceConverter, self).toString(value=value,
     165                                                     strict=strict)
Note: See TracChangeset for help on using the changeset viewer.