当前位置: 首页>>代码示例>>Python>>正文


Python ProtocolBase.from_string方法代码示例

本文整理汇总了Python中spyne.protocol.ProtocolBase.from_string方法的典型用法代码示例。如果您正苦于以下问题:Python ProtocolBase.from_string方法的具体用法?Python ProtocolBase.from_string怎么用?Python ProtocolBase.from_string使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在spyne.protocol.ProtocolBase的用法示例。


在下文中一共展示了ProtocolBase.from_string方法的6个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: _from_dict_value

# 需要导入模块: from spyne.protocol import ProtocolBase [as 别名]
# 或者: from spyne.protocol.ProtocolBase import from_string [as 别名]
    def _from_dict_value(cls, class_, value, validator):
        # validate raw input
        if validator is cls.SOFT_VALIDATION:
            if issubclass(class_, Unicode) and not isinstance(value, basestring):
                raise ValidationError(value)
            if issubclass(class_, Unicode) and not isinstance(value, unicode):
                # Note that String is a subclass of Unicode
                if not (issubclass(class_, String) and isinstance(value, str)):
                    value = ProtocolBase.from_string(class_, value)

            elif issubclass(class_, Decimal) and not isinstance(value,
                                                            (int, long, float)):
                raise ValidationError(value)

            elif issubclass(class_, DateTime) and not (
                                isinstance(value, unicode) and
                                         class_.validate_string(class_, value)):
                raise ValidationError(value)

        # get native type
        if issubclass(class_, ComplexModelBase):
            retval = cls._doc_to_object(class_, value, validator)

        elif issubclass(class_, DateTime):
            retval = ProtocolBase.from_string(class_, value)

        else:
            retval = value

        # validate native type
        if validator is cls.SOFT_VALIDATION and \
                                     not class_.validate_native(class_, retval):
            raise ValidationError(retval)

        return retval
开发者ID:amgibson,项目名称:spyne,代码行数:37,代码来源:dictdoc.py

示例2: test_duration_xml_duration

# 需要导入模块: from spyne.protocol import ProtocolBase [as 别名]
# 或者: from spyne.protocol.ProtocolBase import from_string [as 别名]
    def test_duration_xml_duration(self):
        dur = datetime.timedelta(days=5 + 30 + 365, hours=1, minutes=1,
                                                   seconds=12, microseconds=8e5)

        str1 = 'P400DT3672.8S'
        str2 = 'P1Y1M5DT1H1M12.8S'

        self.assertEquals(dur, ProtocolBase.from_string(Duration, str1))
        self.assertEquals(dur, ProtocolBase.from_string(Duration, str2))

        self.assertEquals(dur, ProtocolBase.from_string(Duration, ProtocolBase.to_string(Duration, dur)))
开发者ID:amgibson,项目名称:spyne,代码行数:13,代码来源:test_primitive.py

示例3: test_limits

# 需要导入模块: from spyne.protocol import ProtocolBase [as 别名]
# 或者: from spyne.protocol.ProtocolBase import from_string [as 别名]
    def test_limits(self):
        try:
            ProtocolBase.from_string(Integer, "1" * (Integer.__max_str_len__ + 1))
        except:
            pass
        else:
            raise Exception("must fail.")

        ProtocolBase.from_string(UnsignedInteger, "-1") # This is not supposed to fail.

        try:
            UnsignedInteger.validate_native(-1) # This is supposed to fail.
        except:
            pass
        else:
            raise Exception("must fail.")
开发者ID:amgibson,项目名称:spyne,代码行数:18,代码来源:test_primitive.py

示例4: test_date

# 需要导入模块: from spyne.protocol import ProtocolBase [as 别名]
# 或者: from spyne.protocol.ProtocolBase import from_string [as 别名]
    def test_date(self):
        n = datetime.date(2011,12,13)

        ret = ProtocolBase.to_string(Date, n)
        self.assertEquals(ret, n.isoformat())

        dt = ProtocolBase.from_string(Date, ret)
        self.assertEquals(n, dt)
开发者ID:amgibson,项目名称:spyne,代码行数:10,代码来源:test_primitive.py

示例5: test_time

# 需要导入模块: from spyne.protocol import ProtocolBase [as 别名]
# 或者: from spyne.protocol.ProtocolBase import from_string [as 别名]
    def test_time(self):
        n = datetime.time(1, 2, 3, 4)

        ret = ProtocolBase.to_string(Time, n)
        self.assertEquals(ret, n.isoformat())

        dt = ProtocolBase.from_string(Time, ret)
        self.assertEquals(n, dt)
开发者ID:amgibson,项目名称:spyne,代码行数:10,代码来源:test_primitive.py

示例6: flat_dict_to_object

# 需要导入模块: from spyne.protocol import ProtocolBase [as 别名]
# 或者: from spyne.protocol.ProtocolBase import from_string [as 别名]
    def flat_dict_to_object(cls, doc, inst_class, validator=None, hier_delim="_"):
        """Converts a flat dict to a native python object.

        See :func:`spyne.model.complex.ComplexModelBase.get_flat_type_info`.
        """

        simple_type_info = inst_class.get_simple_type_info(inst_class)
        inst = inst_class.get_deserialization_instance()

        # this is for validating cls.Attributes.{min,max}_occurs
        frequencies = defaultdict(lambda: defaultdict(int))

        for orig_k, v in doc.items():
            k = RE_HTTP_ARRAY_INDEX.sub("", orig_k)
            member = simple_type_info.get(k, None)
            if member is None:
                logger.debug("discarding field %r" % k)
                continue

            value = getattr(inst, k, None)
            if value is None:
                value = []

            # extract native values from the list of strings that come from the
            # http dict.
            for v2 in v:
                if (validator is cls.SOFT_VALIDATION and not
                                  member.type.validate_string(member.type, v2)):
                    raise ValidationError(v2)

                if issubclass(member.type, (File, ByteArray)):
                    if isinstance(v2, str) or isinstance(v2, unicode):
                        native_v2 = ProtocolBase.from_string(member.type, v2)
                    else:
                        native_v2 = v2
                else:
                    native_v2 = ProtocolBase.from_string(member.type, v2)

                if (validator is cls.SOFT_VALIDATION and not
                            member.type.validate_native(member.type, native_v2)):
                    raise ValidationError(v2)

                value.append(native_v2)

            # assign the native value to the relevant class in the nested object
            # structure.
            ccls, cinst = inst_class, inst
            ctype_info = inst_class.get_flat_type_info(inst_class)

            idx, nidx = 0, 0
            pkey = member.path[0]
            cfreq_key = inst_class, idx

            indexes = deque(RE_HTTP_ARRAY_INDEX.findall(orig_k))
            for i in range(len(member.path) - 1):
                pkey = member.path[i]
                nidx = 0

                ncls, ninst = ctype_info[pkey], getattr(cinst, pkey, None)

                mo = ncls.Attributes.max_occurs
                if ninst is None:
                    ninst = ncls.get_deserialization_instance()
                    if mo > 1:
                        ninst = [ninst]
                    setattr(cinst, pkey, ninst)
                    frequencies[cfreq_key][pkey] += 1

                if mo > 1:
                    if len(indexes) == 0:
                        raise ValidationError(orig_k,
                                               "%r requires index information.")

                    nidx = int(indexes.popleft())

                    if nidx > len(ninst) or nidx < 0:
                        raise ValidationError(orig_k,
                                            "%%r Invalid array index %d." % idx)

                    if nidx == len(ninst):
                        ninst.append(ncls.get_deserialization_instance())
                        frequencies[cfreq_key][pkey] += 1

                    cinst = ninst[nidx]

                else:
                    cinst = ninst

                cfreq_key = cfreq_key + (ncls, nidx)
                ccls, idx = ncls, nidx
                ctype_info = ncls._type_info

            frequencies[cfreq_key][member.path[-1]] += len(value)

            if member.type.Attributes.max_occurs > 1:
                v = getattr(cinst, member.path[-1], None)
                if v is None:
                    setattr(cinst, member.path[-1], value)
                else:
                    v.extend(value)
#.........这里部分代码省略.........
开发者ID:amgibson,项目名称:spyne,代码行数:103,代码来源:dictdoc.py


注:本文中的spyne.protocol.ProtocolBase.from_string方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。