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


Python xml.get_xml_as_object函数代码示例

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


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

示例1: 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

示例2: test_datetime_usec

    def test_datetime_usec(self):
        fs = etree.fromstring
        d = get_xml_as_object(fs('<d>2013-04-05T06:07:08.123456</d>'), DateTime)
        assert d.microsecond == 123456

        # rounds up
        d = get_xml_as_object(fs('<d>2013-04-05T06:07:08.1234567</d>'), DateTime)
        assert d.microsecond == 123457

        # rounds down
        d = get_xml_as_object(fs('<d>2013-04-05T06:07:08.1234564</d>'), DateTime)
        assert d.microsecond == 123456

        # rounds up as well
        d = get_xml_as_object(fs('<d>2013-04-05T06:07:08.1234565</d>'), DateTime)
        assert d.microsecond == 123457
开发者ID:DmitriyMoseev,项目名称:spyne,代码行数:16,代码来源:test_xml.py

示例3: 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

示例4: test_empty_string

    def test_empty_string(self):
        class a(ComplexModel):
            b = Unicode

        elt = etree.fromstring('<a><b/></a>')
        o = get_xml_as_object(elt, a)

        assert o.b == ''
开发者ID:DmitriyMoseev,项目名称:spyne,代码行数:8,代码来源:test_xml.py

示例5: test_dates

 def test_dates(self):
     d = Date
     xml_dates = [etree.fromstring('<d>2013-04-05</d>'), etree.fromstring('<d>2013-04-05+02:00</d>'), etree.fromstring('<d>2013-04-05-02:00</d>'), etree.fromstring('<d>2013-04-05Z</d>')]
     for xml_date in xml_dates:
         c = get_xml_as_object(xml_date, d)
         assert isinstance(c, datetime.date) == True
         assert c.year == 2013
         assert c.month == 4
         assert c.day == 5
开发者ID:harshil07,项目名称:spyne,代码行数:9,代码来源:test_xml.py

示例6: 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

示例7: test_datetime_usec

    def test_datetime_usec(self):
        fs = etree.fromstring
        d = get_xml_as_object(fs("<d>2013-04-05T06:07:08.123456</d>"), DateTime)
        assert d.microsecond == 123456

        # rounds up
        d = get_xml_as_object(fs("<d>2013-04-05T06:07:08.1234567</d>"), DateTime)
        assert d.microsecond == 123457

        # rounds down
        d = get_xml_as_object(fs("<d>2013-04-05T06:07:08.1234564</d>"), DateTime)
        assert d.microsecond == 123456

        # rounds up as well
        d = get_xml_as_object(fs("<d>2013-04-05T06:07:08.1234565</d>"), DateTime)
        # FIXME: this is very interesting. why?
        if six.PY3:
            assert d.microsecond == 123456
        else:
            assert d.microsecond == 123457
开发者ID:plq,项目名称:spyne,代码行数:20,代码来源:test_xml.py

示例8: test

 def test(xml):
     params = get_xml_as_object(xml, AnyDict)
     params = clear_list(params)
     method = get_method(params)
     c = wic_client()
     if not hasattr(c, method):
         raise Exception, 'Not implemented method: %s' % method
     kwargs = params[method]
     kwargs = getattr(c, method)(**kwargs)
     params[method] = kwargs
     return params
开发者ID:pubfox,项目名称:webservices,代码行数:11,代码来源:views.py

示例9: call

 def call(xml):
     #Decrypt request
     xml = subprocess.check_output(['java', '-jar', '/usr/share/openstack-dashboard/webservices/java/Encryptor.jar', '-decrypt', xml, ENCRYPT_PASSWORD]).strip()
     xml = AnyXml.from_string(xml)
     #Process params from request
     params = get_xml_as_object(xml, AnyDict)
     params = clear_list(params)
     #Handle request
     id, description = handle_request(params)
     res = '''<?xml version="1.0" encoding="UTF-8"?>
              <result>
                <id>%s</id>
                <description>%s</description>
              </result>''' % (id, description, )
     return res.decode('utf8')
开发者ID:pubfox,项目名称:webservices,代码行数:15,代码来源:views.py

示例10: process

 def process(value):
     if value is not None:
         return get_xml_as_object(etree.fromstring(value), self.cls)
开发者ID:1-bit,项目名称:spyne,代码行数:3,代码来源:document.py

示例11: get_object_as_xml

# 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']

# We print an empty instance just to see the parsed fields.
print(NewObject())

# Deserialize the xml document using the definition from the schema.
new_instance = get_xml_as_object(etree.fromstring(data), NewObject)

print(new_instance)

assert new_instance.s == instance.s
assert new_instance.i == instance.i
assert new_instance.d == instance.d
开发者ID:1-bit,项目名称:spyne,代码行数:31,代码来源:schema.py

示例12: test_parse

 def test_parse(self):
     package = get_xml_as_object(etree.fromstring(SAMPLE_OPF_DOC), Package)
开发者ID:plq,项目名称:kontes,代码行数:2,代码来源:test_opf.py

示例13: 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

示例14: 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


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