コード例 #1
0
def to_isoformat(value):
    if value is None:
        return None

    try:
        value = value.isoformat()
    except AttributeError as error:
        raise ValueSerializationError(error.args[0])

    return value
コード例 #2
0
def to_total_seconds(value):
    if value is None:
        return None

    try:
        value = value.total_seconds()
    except AttributeError as error:
        raise ValueSerializationError(error.args[0])

    return value
コード例 #3
0
ファイル: base_model.py プロジェクト: timmy224/sqlathanor
    def _get_serialized_value(self, format, attribute, config_set=None):
        """Retrieve the value of ``attribute`` after applying the attribute's
        ``on_serialize`` function for the format indicated by ``format``.

        :param format: The format to which the value should be serialized. Accepts
          either: ``csv``, ``json``, ``yaml``, or ``dict``.
        :type format: :class:`str <python:str>`

        :param attribute: The name of the attribute that whose serialized value
          should be returned.
        :type attribute: :class:`str <python:str>`

        :param config_set: The name of the named configuration set whose configuration
          should be used to retrieve the serialized value. Defaults to
          :obj:`None <python:None>`.
        :type config_set: :class:`str <python:str>` / :obj:`None <python:None>`

        :returns: The value returned by the attribute's ``on_serialize`` function
          for the indicated ``format``.

        :raises InvalidFormatError: if ``format`` is not ``csv``, ``json``, ``yaml``,
          or ``dict``.
        :raises ValueSerializationError: if the ``on_serialize`` function raises
          an exception
        """
        # pylint: disable=line-too-long

        to_csv, to_json, to_yaml, to_dict = format_to_tuple(format)

        supports_serialization = self.does_support_serialization(
            attribute,
            to_csv=to_csv,
            to_json=to_json,
            to_yaml=to_yaml,
            to_dict=to_dict,
            config_set=config_set)
        if not supports_serialization:
            raise UnsupportedSerializationError(
                "%s attribute '%s' does not support serialization to '%s'" %
                (self.__class__, attribute, format))

        config = self.get_attribute_serialization_config(attribute,
                                                         config_set=config_set)

        on_serialize = config.on_serialize[format]
        if on_serialize is None:
            on_serialize = get_default_serializer(
                getattr(self.__class__, attribute),
                format=format,
                value=getattr(self, attribute, None))

        if on_serialize is None:
            if format == 'csv':
                return getattr(self, attribute, '')

            return getattr(self, attribute, None)

        try:
            return_value = on_serialize(getattr(self, attribute, None))
        except Exception:
            raise ValueSerializationError(
                "attribute '%s' failed serialization to format '%s'" %
                (attribute, format))

        return return_value