Scio: Pickling SOAP typesΒΆ

Note

The reduce callback and pickling client mentioned below are defined in a fixtures module because both must be module globals for pickling and unpickling to work. Therefore they may not be defined in the body of a doctest.

Because Scio type classes are dynamically generated, pickling and unpickling them is somewhat complex. At unpickling time, there’s no way for Python to know where a dynamically generated class definition can be found unless you tell it. Therefore, to enable pickling and unpickling, you must provide a top-level function as a reduce callback.

The basic form of a reduce callback is this:

def reviver(classname, proto=object, args=()):
    return proto.__new__(getattr(client.type, classname), *args)

To enable pickling, pass the reduce callback to the client:

pickling_client = scio.Client(urlopen(url), reduce_callback=reviver)

Now types generated by this client can be pickled and unpickled:

>>> from cPickle import dumps, loads
>>> artist, albums = pickling_client.service.getArtist('Iris Dement')
>>> angel = albums[0]
>>> angel.album
u'Infamous Angel'
>>> angel.songs
[u'Let The Mystery Be', u'These Hills', u'Hotter Than Mojave In My Heart', u'When Love Was Young', u'Our Town', u'Fifty Miles Of Elbow Room', u'Infamous Angel', u'Sweet Forgiveness', u"After You're Gone", u"Mama's Opry", u'Higher Ground']
>>> pickled = dumps(angel)
>>> loaded = loads(pickled)
>>> loaded.songs
[u'Let The Mystery Be', u'These Hills', u'Hotter Than Mojave In My Heart', u'When Love Was Young', u'Our Town', u'Fifty Miles Of Elbow Room', u'Infamous Angel', u'Sweet Forgiveness', u"After You're Gone", u"Mama's Opry", u'Higher Ground']

Previous topic

Scio: API and Internals

Next topic

Scio: custom client subclass example

This Page