Table Of Contents

Previous topic

Auxiliary Processors

Next topic

Running Tests

This Page

Miscellanous Spyne Utilities

Class Dictionary

cdict (ClassDict) is a funny kind of dict that tries to return the values for the base classes of a key when the entry for the key is not found. It is not a generalized dictionary that can handle any type of key – it relies on spyne.model api to look for classes.

>>> from spyne.util.cdict import cdict
>>> class A(object):
...     pass
...
>>> class B(A):
...     pass
...
>>> class C(object):
...     pass
...
>>> class D:
...     pass
...
>>> d=cdict({A: "fun", object: "base"})
>>> print d[A]
fun
>>> print d
{<class '__main__.A'>: 'fun', <type 'object'>: 'base'}
>>> print d[B]
fun
>>> print d
{<class '__main__.A'>: 'fun', <class '__main__.B'>: 'fun', <type 'object'>: 'base'}
>>> print d[C]
base
>>> print d
{<class '__main__.A'>: 'fun', <class '__main__.B'>: 'fun', <class '__main__.C'>: 'base', <type 'object'>: 'base'}
>>> print d[D]
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/home/plq/src/github/plq/spyne/src/spyne/util/cdict.py", line 77, in __getitem__
    raise e
KeyError: <class __main__.D at 0x8d92c0>
>>>

Element Tree Conversion

This module contains the utility methods that convert an ElementTree hierarchy to python dicts and vice versa.

spyne.util.etreeconv.dict_to_etree(d, parent)[source]

Takes a the dict whose value is either None or an instance of dict, odict or an iterable. The iterables can contain either other dicts/odicts or str/unicode instances.

spyne.util.etreeconv.etree_strip_namespaces(element)[source]

Removes any namespace information form the given element recursively.

spyne.util.etreeconv.etree_to_dict(element, iterable=(<type 'list'>, <method 'append' of 'list' objects>))[source]

Takes an xml root element and returns the corresponding dict. The second argument is a pair of iterable type and the function used to add elements to the iterable. The xml attributes are ignored.

spyne.util.etreeconv.root_dict_to_etree(d)[source]

Converts a dictionary to an xml hiearchy. Just like a valid xml document, the dictionary must have a single element. The format of the child dictionaries is the same as dict_to_etree().

spyne.util.etreeconv.root_etree_to_dict(element, iterable=(<type 'list'>, <method 'append' of 'list' objects>))[source]

Takes an xml root element and returns the corresponding dict. The second argument is a pair of iterable type and the function used to add elements to the iterable. The xml attributes are ignored.

Simple Wrappers

Contains functions that implement the most common protocol and transport combinations

spyne.util.simple.pyramid_soap11_application(services, tns='spyne.simple.soap', validator=None, name=None)[source]

Wraps services argument inside a PyramidApplication that uses Soap 1.1 for both input and output protocols.

spyne.util.simple.wsgi_soap11_application(services, tns='spyne.simple.soap', validator=None, name=None)[source]

Wraps services argument inside a WsgiApplication that uses Soap 1.1 for both input and output protocols.

spyne.util.simple.wsgi_soap_application(services, tns='spyne.simple.soap', validator=None, name=None)

DEPRECATED! Use wsgi_soap11_application() instead.

Xml Utilities

The spyne.util.xml module contains various Xml and Xml Schema related utility functions.

spyne.util.xml.get_object_as_xml(inst, cls=None, root_tag_name=None, no_namespace=False)[source]

Returns an ElementTree representation of a spyne.model.complex.ComplexModel subclass.

Parameters:
  • inst – The instance of the class to be serialized.
  • cls – The class to be serialized. Optional.
  • root_tag_name – The root tag string to use. Defaults to the output of value.__class__.get_type_name_ns().
  • no_namespace – When true, namespace information is discarded.
spyne.util.xml.get_schema_documents(models, default_namespace=None)[source]

Returns the schema documents in a dict whose keys are namespace prefixes and values are Element objects.

Parameters:models – A list of spyne.model classes that will be represented in the schema.
spyne.util.xml.get_validation_schema(models, default_namespace=None)[source]

Returns the validation schema object for the given models.

Parameters:models – A list of spyne.model classes that will be represented in the schema.
spyne.util.xml.get_xml_as_object(elt, cls)[source]

Returns a native spyne.model.complex.ComplexModel child from an ElementTree representation of the same class.

Parameters:
  • elt – The xml document to be deserialized.
  • cls – The class the xml document represents.

Ordered Dictionary

This module contains a sort of an ordered dictionary implementation.

class spyne.util.odict.odict(data=[])[source]

Sort of an ordered dictionary implementation.

Ordered Set

The spyne.util.oset package contains implementation of an ordered set that works on Python versions 2.4 through 2.7.

Protocol Helpers

Helpers for protocol boilerplate.

spyne.util.protocol.deserialize_request_string(string, app)[source]

Deserialize request string using in_protocol in application definition. Returns the corresponding native python object.