• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

Python xml.get_object_as_xml函数代码示例

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

本文整理汇总了Python中spyne.util.xml.get_object_as_xml函数的典型用法代码示例。如果您正苦于以下问题:Python get_object_as_xml函数的具体用法?Python get_object_as_xml怎么用?Python get_object_as_xml使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。



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

示例1: test_serialize

    def test_serialize(self):
        from spyne.util.xml import get_object_as_xml
        from lxml import etree

        class C(ComplexModel):
            __namespace__ = "tns"
            i = Integer
            s = Unicode

        c = C(i=5, s="x")

        ret = get_object_as_xml(c, C)
        print etree.tostring(ret)
        assert ret.tag == "{tns}C"

        ret = get_object_as_xml(c, C, "X")
        print etree.tostring(ret)
        assert ret.tag == "{tns}X"

        ret = get_object_as_xml(c, C, "X", no_namespace=True)
        print etree.tostring(ret)
        assert ret.tag == "X"

        ret = get_object_as_xml(c, C, no_namespace=True)
        print etree.tostring(ret)
        assert ret.tag == "C"
开发者ID:amgibson,项目名称:spyne,代码行数:26,代码来源:test_util.py


示例2: test_file

 def test_file(self):
     v = b"aaaa"
     f = BytesIO(v)
     elt = get_object_as_xml(File.Value(handle=f), File, "B")
     eltstr = etree.tostring(elt)
     print(eltstr)
     assert elt.text == b64encode(v).decode("ascii")
开发者ID:plq,项目名称:spyne,代码行数:7,代码来源:test_xml.py


示例3: test_sub_attributes

    def test_sub_attributes(self):
        from lxml import etree
        from spyne.util.xml import get_xml_as_object
        from spyne.util.xml import get_object_as_xml

        m = {
            "s0": "aa",
            "s2": "cc",
            "s3": "dd",
        }
        class C(ComplexModel):
            __namespace__ = "aa"
            a = XmlAttribute(Integer)
            b = XmlAttribute(Integer(sub_name="bb"))
            c = XmlAttribute(Integer(sub_ns="cc"))
            d = XmlAttribute(Integer(sub_ns="dd", sub_name="dd"))

        elt = get_object_as_xml(C(a=1, b=2, c=3, d=4), C)
        print(etree.tostring(elt, pretty_print=True))

        assert elt.xpath("//*/@a")  == ["1"]
        assert elt.xpath("//*/@bb") == ["2"]
        assert elt.xpath("//*/@s2:c", namespaces=m)  == ["3"]
        assert elt.xpath("//*/@s3:dd", namespaces=m) == ["4"]

        c = get_xml_as_object(elt, C)
        print(c)
        assert c.a == 1
        assert c.b == 2
        assert c.c == 3
        assert c.d == 4
开发者ID:DmitriyMoseev,项目名称:spyne,代码行数:31,代码来源:test_xml.py


示例4: test_any_xml_bytes

    def test_any_xml_bytes(self):
        v = b"<roots><bloody/></roots>"

        elt = get_object_as_xml(v, AnyXml, 'B', no_namespace=True)
        eltstr = etree.tostring(elt)
        print(eltstr)
        assert etree.tostring(elt[0]) == v
开发者ID:knoxsp,项目名称:spyne,代码行数:7,代码来源:test_xml.py


示例5: test_subs

    def test_subs(self):
        from lxml import etree
        from spyne.util.xml import get_xml_as_object
        from spyne.util.xml import get_object_as_xml

        m = {
            "s0": "aa",
            "s2": "cc",
            "s3": "dd",
        }
        class C(ComplexModel):
            __namespace__ = "aa"
            a = Integer
            b = Integer(sub_name="bb")
            c = Integer(sub_ns="cc")
            d = Integer(sub_ns="dd", sub_name="dd")

        elt = get_object_as_xml(C(a=1, b=2, c=3, d=4), C)
        print etree.tostring(elt, pretty_print=True)

        assert elt.xpath("s0:a/text()",  namespaces=m) == ["1"]
        assert elt.xpath("s0:bb/text()", namespaces=m) == ["2"]
        assert elt.xpath("s2:c/text()",  namespaces=m) == ["3"]
        assert elt.xpath("s3:dd/text()", namespaces=m) == ["4"]

        c = get_xml_as_object(elt, C)
        print c
        assert c.a == 1
        assert c.b == 2
        assert c.c == 3
        assert c.d == 4
开发者ID:harshil07,项目名称:spyne,代码行数:31,代码来源:test_xml.py


示例6: test_null_mandatory_attribute

    def test_null_mandatory_attribute(self):
        class Action (ComplexModel):
            data = XmlAttribute(M(Unicode))

        elt = get_object_as_xml(Action(), Action)
        eltstr = etree.tostring(elt)
        print(eltstr)
        assert eltstr == b'<Action/>'
开发者ID:DmitriyMoseev,项目名称:spyne,代码行数:8,代码来源:test_xml.py


示例7: test_deserialize

    def test_deserialize(self):
        class Punk(ComplexModel):
            __namespace__ = 'some_namespace'

            a = Unicode
            b = Integer
            c = Decimal
            d = DateTime

        class Foo(ComplexModel):
            __namespace__ = 'some_other_namespace'

            a = Unicode
            b = Integer
            c = Decimal
            d = DateTime
            e = XmlAttribute(Integer)

            def __eq__(self, other):
                # remember that this is a test object
                assert (
                    self.a == other.a and
                    self.b == other.b and
                    self.c == other.c and
                    self.d == other.d and
                    self.e == other.e
                )

                return True

        docs = get_schema_documents([Punk, Foo])
        pprint(docs)
        assert docs['s0'].tag == '{http://www.w3.org/2001/XMLSchema}schema'
        assert docs['tns'].tag == '{http://www.w3.org/2001/XMLSchema}schema'
        print()

        print("the other namespace %r:" % docs['tns'].attrib['targetNamespace'])
        assert docs['tns'].attrib['targetNamespace'] == 'some_namespace'
        print(etree.tostring(docs['tns'], pretty_print=True))
        print()

        print("the other namespace %r:" % docs['s0'].attrib['targetNamespace'])
        assert docs['s0'].attrib['targetNamespace'] == 'some_other_namespace'
        print(etree.tostring(docs['s0'], pretty_print=True))
        print()

        foo = Foo(a=u'a', b=1, c=decimal.Decimal('3.4'),
                                    d=datetime(2011,02,20,tzinfo=pytz.utc), e=5)
        doc = get_object_as_xml(foo, Foo)
        print(etree.tostring(doc, pretty_print=True))
        foo_back = get_xml_as_object(doc, Foo)

        assert foo_back == foo

        # as long as it doesn't fail, it's ok.
        get_validation_schema([Punk, Foo])
开发者ID:sashka,项目名称:spyne,代码行数:56,代码来源:test_util.py


示例8: test_bare_sub_name_ns

    def test_bare_sub_name_ns(self):
        class Action (ComplexModel):
            class Attributes(ComplexModel.Attributes):
                sub_ns = "SOME_NS"
                sub_name = "Action"
            data = XmlData(Unicode)
            must_understand = XmlAttribute(Unicode)

        elt = get_object_as_xml(Action("x", must_understand="y"), Action)
        eltstr = etree.tostring(elt)
        print(eltstr)
        assert eltstr == b'<ns0:Action xmlns:ns0="SOME_NS" must_understand="y">x</ns0:Action>'
开发者ID:DmitriyMoseev,项目名称:spyne,代码行数:12,代码来源:test_xml.py


示例9: process

 def process(value):
     if value is not None:
         return etree.tostring(get_object_as_xml(value, self.cls,
             self.root_tag_name, self.no_namespace), encoding='utf8',
                   pretty_print=self.pretty_print, xml_declaration=False)
开发者ID:1-bit,项目名称:spyne,代码行数:5,代码来源:document.py


示例10: test_fault_detail_as_dict

 def test_fault_detail_as_dict(self):
     elt = get_object_as_xml(Fault(detail={"this": "that"}), Fault)
     eltstr = etree.tostring(elt)
     print(eltstr)
     assert b'<detail><this>that</this></detail>' in eltstr
开发者ID:DmitriyMoseev,项目名称:spyne,代码行数:5,代码来源:test_xml.py


示例11: complex_add

def complex_add(document, cls, tags):
    complex_type = etree.Element("{%s}complexType" % _ns_xsd)
    complex_type.set('name', cls.get_type_name())

    if cls.Annotations.doc != '' or cls.Annotations.appinfo != None or \
                                             cls.Annotations.__use_parent_doc__:
        annotation = etree.SubElement(complex_type, "{%s}annotation" % _ns_xsd)
        if cls.Annotations.doc != '' or cls.Annotations.__use_parent_doc__:
            doc = etree.SubElement(annotation, "{%s}documentation" % _ns_xsd)
            if cls.Annotations.__use_parent_doc__:
                doc.text = getattr(cls, '__doc__')
            else:
                doc.text = cls.Annotations.doc

        _ai = cls.Annotations.appinfo;
        if _ai != None:
            appinfo = etree.SubElement(annotation, "{%s}appinfo" % _ns_xsd)
            if isinstance(_ai, dict):
                dict_to_etree(_ai, appinfo)

            elif isinstance(_ai, str) or isinstance(_ai, unicode):
                appinfo.text = _ai

            elif isinstance(_ai, etree._Element):
                appinfo.append(_ai)

            else:
                from spyne.util.xml import get_object_as_xml

                appinfo.append(get_object_as_xml(_ai))

    sequence_parent = complex_type
    extends = getattr(cls, '__extends__', None)

    type_info = cls._type_info
    if extends is not None:
        if (extends.get_type_name() == cls.get_type_name() and
                               extends.get_namespace() == cls.get_namespace()):
            raise Exception("%r can't extend %r because they are both '{%s}%s'"
                    % (cls, extends, cls.get_namespace(), cls.get_type_name()))

        if extends.Attributes.exc_interface:
            # If the parent class is private, it won't be in the schema, so we
            # need to act as if its attributes are part of cls as well.
            type_info = cls.get_simple_type_info(cls)

        else:
            complex_content = etree.SubElement(complex_type,
                                                "{%s}complexContent" % _ns_xsd)
            extension = etree.SubElement(complex_content,
                                                     "{%s}extension" % _ns_xsd)
            extension.set('base', extends.get_type_name_ns(document.interface))
            sequence_parent = extension

    sequence = etree.Element('{%s}sequence' % _ns_xsd)

    deferred = deque()
    choice_tags = defaultdict(lambda: etree.Element('{%s}choice' % _ns_xsd))
    for k, v in type_info.items():
        a = v.Attributes
        if a.exc_interface:
            continue

        if issubclass(v, XmlAttribute):
            deferred.append((k,v))
            continue

        document.add(v, tags)

        name = a.sub_name
        ns = a.sub_ns
        if name is None:
            name = k
        if ns is not None:
            name = "{%s}%s" % (ns, name)

        member = etree.Element(a.schema_tag)
        if a.schema_tag == '{%s}element' % _ns_xsd:
            member.set('name', name)
            member.set('type', v.get_type_name_ns(document.interface))

        elif a.schema_tag == '{%s}any' % _ns_xsd and \
                                                    (issubclass(v, AnyXml)):
            if a.namespace is not None:
                member.set('namespace', a.namespace)
            if a.process_contents is not None:
                member.set('processContents', a.process_contents)

        else:
            raise ValueError("Unhandled schema_tag / type combination. %r %r"
                    % (v, a.schema_tag))

        if a.min_occurs != 1: # 1 is the xml schema default
            member.set('minOccurs', str(a.min_occurs))

        if a.max_occurs != 1: # 1 is the xml schema default
            val = a.max_occurs
            if val in (decimal.Decimal('inf'), float('inf')):
                val = 'unbounded'
            else:
#.........这里部分代码省略.........
开发者ID:wirewit,项目名称:spyne,代码行数:101,代码来源:model.py


示例12: SomeObject

    i = Integer
    s = Unicode
    d = DateTime
    __repr__ = hier_repr

# Instantiate the object
instance = SomeObject(i=5, s="str", d=datetime.now())

# Generate schema documents
schema_elts = get_schema_documents([SomeObject], 'some_ns')

# Serialize the xml schema document for object
schema = etree.tostring(schema_elts['tns'],  pretty_print=True)

# Serialize the object to XML
instance_elt = get_object_as_xml(instance, SomeObject)

# Serialize the element tree to string
data = etree.tostring(instance_elt, pretty_print=True)

print(instance)
print()
print(schema)
print(data)

# parse the schema document
parsed_schema = parse_schema_string(schema)['some_ns']

# Get SomeObject definition from the parsed schema document
NewObject = parsed_schema.types['SomeObject']
开发者ID:1-bit,项目名称:spyne,代码行数:30,代码来源:schema.py


示例13: XmlAttribute

    __namespace__ = 'some_other_namespace'

    a = String
    b = Integer
    c = Decimal
    d = DateTime
    e = XmlAttribute(Integer)


docs = get_schema_documents([Punk, Foo])
pprint(docs)
print()

# the default ns prefix is always tns
print("the default namespace %r:" % docs['tns'].attrib['targetNamespace'])
print(etree.tostring(docs['tns'], pretty_print=True))
print()

# Namespace prefixes are assigned like s0, s1, s2, etc...
print("the other namespace %r:" % docs['s0'].attrib['targetNamespace'])
print(etree.tostring(docs['s0'], pretty_print=True))


foo = Foo(a='a', b=1, c=3.4, d=datetime(2011,02,20),e=5)
doc = get_object_as_xml(foo)
print(etree.tostring(doc, pretty_print=True))
print(get_xml_as_object(Foo, doc))

# See http://lxml.de/validation.html to see what this could be used for.
print(get_validation_schema([Punk, Foo]))
开发者ID:Bluehorn,项目名称:spyne,代码行数:30,代码来源:xml_utils.py


示例14: test_bytearray

 def test_bytearray(self):
     v = b"aaaa"
     elt = get_object_as_xml([v], ByteArray, "B")
     eltstr = etree.tostring(elt)
     print(eltstr)
     assert elt.text == b64encode(v).decode("ascii")
开发者ID:plq,项目名称:spyne,代码行数:6,代码来源:test_xml.py


示例15: print

# the default ns prefix is always tns
print("the default namespace %r:" % docs['tns'].attrib['targetNamespace'])
print(etree.tostring(docs['tns'], pretty_print=True))
print()

# Namespace prefixes are assigned like s0, s1, s2, etc...
print("the other namespace %r:" % docs['s0'].attrib['targetNamespace'])
print(etree.tostring(docs['s0'], pretty_print=True))
print()

print("the other namespace %r:" % docs['s2'].attrib['targetNamespace'])
print(etree.tostring(docs['s2'], pretty_print=True))
print()

# Object serialization and deserialization
foo = Foo(a='a', b=1, c=3.4, d=datetime(2011, 02, 20), e=5, f='f')
doc = get_object_as_xml(foo, Foo)
print(etree.tostring(doc, pretty_print=True))
print(get_xml_as_object(doc, Foo))
print()

# XmlData example.
print("Product output (illustrates XmlData):")
product = Product(id=uuid.uuid4(), edition=ProductEdition(id=uuid.uuid4(),
                                                             name='My edition'))
print(etree.tostring(get_object_as_xml(product, Product), pretty_print=True))

# See http://lxml.de/validation.html to see what this could be used for.
print(get_validation_schema([Punk, Foo]))
开发者ID:1-bit,项目名称:spyne,代码行数:29,代码来源:utils.py


示例16: test_any_xml_elt

 def test_any_xml_elt(self):
     v = E.roots(E.bloody(E.roots()))
     elt = get_object_as_xml(v, AnyXml, 'B')
     eltstr = etree.tostring(elt)
     print(eltstr)
     assert etree.tostring(elt[0]) == etree.tostring(v)
开发者ID:knoxsp,项目名称:spyne,代码行数:6,代码来源:test_xml.py


示例17: test_any_xml_text

 def test_any_xml_text(self):
     v = u"<roots><bloody/></roots>"
     elt = get_object_as_xml(v, AnyXml, 'B', no_namespace=True)
     eltstr = etree.tostring(elt)
     print(eltstr)
     assert etree.tostring(elt[0], encoding="unicode") == v
开发者ID:knoxsp,项目名称:spyne,代码行数:6,代码来源:test_xml.py


示例18: complex_add

def complex_add(document, cls, tags):
    complex_type = etree.Element(XSD('complexType'))
    complex_type.set('name', cls.get_type_name())

    doc_text = cls.get_documentation()
    if doc_text or cls.Annotations.appinfo is not None:
        annotation = etree.SubElement(complex_type, XSD('annotation'))
        if doc_text:
            doc = etree.SubElement(annotation, XSD('documentation'))
            doc.text = doc_text

        _ai = cls.Annotations.appinfo
        if _ai is not None:
            appinfo = etree.SubElement(annotation, XSD('appinfo'))
            if isinstance(_ai, dict):
                dict_to_etree(_ai, appinfo)

            elif isinstance(_ai, str) or isinstance(_ai, unicode):
                appinfo.text = _ai

            elif isinstance(_ai, etree._Element):
                appinfo.append(_ai)

            else:
                from spyne.util.xml import get_object_as_xml

                appinfo.append(get_object_as_xml(_ai))

    sequence_parent = complex_type
    extends = getattr(cls, '__extends__', None)

    type_info = cls._type_info
    if extends is not None:
        if (extends.get_type_name() == cls.get_type_name() and
                               extends.get_namespace() == cls.get_namespace()):
            raise Exception("%r can't extend %r because they are both '{%s}%s'"
                    % (cls, extends, cls.get_namespace(), cls.get_type_name()))

        if extends.Attributes.exc_interface:
            # If the parent class is private, it won't be in the schema, so we
            # need to act as if its attributes are part of cls as well.
            type_info = cls.get_simple_type_info(cls)

        else:
            complex_content = etree.SubElement(complex_type,
                                                          XSD('complexContent'))
            extension = etree.SubElement(complex_content, XSD('extension'))
            extension.set('base', extends.get_type_name_ns(document.interface))
            sequence_parent = extension

    xtba_key, xtba_type = cls.Attributes._xml_tag_body_as
    if xtba_key is not None:
        _sc = etree.SubElement(sequence_parent, XSD('simpleContent'))
        xtba_ext = etree.SubElement(_sc, XSD('extension'))
        xtba_ext.attrib['base'] = xtba_type.type.get_type_name_ns(
                                                             document.interface)

    sequence = etree.Element(XSD('sequence'))

    deferred = deque()
    choice_tags = defaultdict(lambda: etree.Element(XSD('choice')))

    for k, v in type_info.items():
        assert isinstance(k, string_types)
        assert issubclass(v, ModelBase)

        a = v.Attributes
        if a.exc_interface:
            continue

        if issubclass(v, XmlData):
            continue

        if issubclass(v, XmlAttribute):
            deferred.append((k,v))
            continue

        document.add(v, tags)

        name = a.sub_name
        if name is None:
            name = k
        #ns = a.sub_ns
        #if ns is not None:
        #    name = "{%s}%s" % (ns, name)

        type_name_ns = v.get_type_name_ns(document.interface)
        if v.__extends__ is not None and v.__orig__ is not None and \
                                              _check_extension_attrs(v) is None:
            type_name_ns = v.__orig__.get_type_name_ns(document.interface)

        member = etree.Element(a.schema_tag)
        if a.schema_tag == XSD('element'):
            member.set('name', name)
            member.set('type', type_name_ns)

        elif a.schema_tag == XSD('any') and issubclass(v, AnyXml):
            if a.namespace is not None:
                member.set('namespace', a.namespace)
            if a.process_contents is not None:
#.........这里部分代码省略.........
开发者ID:infoxchange,项目名称:spyne,代码行数:101,代码来源:model.py


示例19: complex_add

def complex_add(document, cls):
    complex_type = etree.Element("{%s}complexType" % _ns_xsd)
    complex_type.set('name', cls.get_type_name())

    if cls.Annotations.doc != '' or cls.Annotations.appinfo != None:
        annotation = etree.SubElement(complex_type, "{%s}annotation" % _ns_xsd)
        if cls.Annotations.doc != '':
            doc = etree.SubElement(annotation, "{%s}documentation" % _ns_xsd)
            doc.text = cls.Annotations.doc

        _ai = cls.Annotations.appinfo;
        if _ai != None:
            appinfo = etree.SubElement(annotation, "{%s}appinfo" % _ns_xsd)
            if isinstance(_ai, dict):
                dict_to_etree(_ai, appinfo)
            elif isinstance(_ai, str) or isinstance(_ai, unicode):
                appinfo.text = _ai
            elif isinstance(_ai, etree._Element):
                appinfo.append(_ai)
            else:
                from spyne.util.xml import get_object_as_xml

                appinfo.append(get_object_as_xml(_ai))

    sequence_parent = complex_type
    extends = getattr(cls, '__extends__', None)
    if not (extends is None):
        if (extends.get_type_name() == cls.get_type_name() and
                                extends.get_namespace() == cls.get_namespace()):
            raise Exception("%r can't extend %r because they are all '{%s}%s'"
                    % (cls, extends, cls.get_type_name(), cls.get_namespace()))

        else:
            complex_content = etree.SubElement(complex_type,
                                       "{%s}complexContent" % _ns_xsd)
            extension = etree.SubElement(complex_content,
                                       "{%s}extension" % _ns_xsd)
            extension.set('base', extends.get_type_name_ns(document.interface))
            sequence_parent = extension

    sequence = etree.SubElement(sequence_parent, '{%s}sequence' % _ns_xsd)

    for k, v in cls._type_info.items():
        if issubclass(v, XmlAttribute):
            attribute = etree.SubElement(complex_type,
                                        '{%s}attribute' % _ns_xsd)
            v.describe(k, attribute, document)
            continue

        if not issubclass(v, cls):
            document.add(v)

        member = etree.SubElement(sequence, v.Attributes.schema_tag)
        if v.Attributes.schema_tag == '{%s}element' % _ns_xsd:
            member.set('name', k)
            member.set('type', v.get_type_name_ns(document.interface))

        elif v.Attributes.schema_tag == '{%s}any' % _ns_xsd and \
                                                    (issubclass(v, AnyXml)):
            if v.Attributes.namespace is not None:
                member.set('namespace', v.Attributes.namespace)
            if v.Attributes.process_contents is not None:
                member.set('processContents', v.Attributes.process_contents)

        else:
            raise ValueError("Unhandled schema_tag / type combination. %r %r"
                    % (v, v.Attributes.schema_tag))

        if v.Attributes.min_occurs != 1: # 1 is the xml schema default
            member.set('minOccurs', str(v.Attributes.min_occurs))
        if v.Attributes.max_occurs != 1: # 1 is the xml schema default
            val = v.Attributes.max_occurs
            if val == float('inf'):
                val = 'unbounded'
            else:
                val = str(val)

            member.set('maxOccurs', val)

        if bool(v.Attributes.nillable) != False: # False is the xml schema default
            member.set('nillable', 'true')

    document.add_complex_type(cls, complex_type)

    # simple node
    element = etree.Element('{%s}element' % _ns_xsd)
    element.set('name', cls.get_type_name())
    element.set('type', cls.get_type_name_ns(document.interface))

    document.add_element(cls, element)
开发者ID:66ru,项目名称:spyne,代码行数:90,代码来源:model.py



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


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
Python xml.get_schema_documents函数代码示例发布时间:2022-05-27
下一篇:
Python test.show函数代码示例发布时间:2022-05-27
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap