本文整理汇总了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
示例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
示例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
示例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 == ''
示例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
示例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])
示例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
示例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
示例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')
示例10: process
def process(value):
if value is not None:
return get_xml_as_object(etree.fromstring(value), self.cls)
示例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
示例12: test_parse
def test_parse(self):
package = get_xml_as_object(etree.fromstring(SAMPLE_OPF_DOC), Package)
示例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]))
示例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]))