##// END OF EJS Templates
nbconvert --to script...
nbconvert --to script Use extension and mime type from NB metadata

File last commit:

r18330:286cdd8b
r18966:b28ec91e
Show More
test_serialize.py
208 lines | 6.1 KiB | text/x-python | PythonLexer
MinRK
better serialization for parallel code...
r7967 """test serialization tools"""
MinRK
Fix NaN warnings and failures in test_serialize...
r17119 # Copyright (c) IPython Development Team.
# Distributed under the terms of the Modified BSD License.
MinRK
better serialization for parallel code...
r7967
import pickle
MinRK
test serializing lists / tuples / namedtuples
r9713 from collections import namedtuple
MinRK
better serialization for parallel code...
r7967
import nose.tools as nt
# from unittest import TestCaes
MinRK
s/unserialize/deserialize
r18330 from IPython.kernel.zmq.serialize import serialize_object, deserialize_object
MinRK
better serialization for parallel code...
r7967 from IPython.testing import decorators as dec
MinRK
test canning classes
r9001 from IPython.utils.pickleutil import CannedArray, CannedClass
Thomas Kluyver
Fix references to dict.iteritems and dict.itervalues
r13361 from IPython.utils.py3compat import iteritems
MinRK
test canning classes
r9001 from IPython.parallel import interactive
MinRK
better serialization for parallel code...
r7967
MinRK
update serialize tests
r7972 #-------------------------------------------------------------------------------
# Globals and Utilities
#-------------------------------------------------------------------------------
MinRK
better serialization for parallel code...
r7967 def roundtrip(obj):
"""roundtrip an object through serialization"""
bufs = serialize_object(obj)
MinRK
s/unserialize/deserialize
r18330 obj2, remainder = deserialize_object(bufs)
MinRK
better serialization for parallel code...
r7967 nt.assert_equals(remainder, [])
return obj2
class C(object):
"""dummy class for """
def __init__(self, **kwargs):
Thomas Kluyver
Fix references to dict.iteritems and dict.itervalues
r13361 for key,value in iteritems(kwargs):
MinRK
better serialization for parallel code...
r7967 setattr(self, key, value)
MinRK
update serialize tests
r7972 SHAPES = ((100,), (1024,10), (10,8,6,5), (), (0,))
DTYPES = ('uint8', 'float64', 'int32', [('g', 'float32')], '|S10')
MinRK
Fix NaN warnings and failures in test_serialize...
r17119
MinRK
update serialize tests
r7972 #-------------------------------------------------------------------------------
# Tests
#-------------------------------------------------------------------------------
MinRK
Fix NaN warnings and failures in test_serialize...
r17119 def new_array(shape, dtype):
import numpy
return numpy.random.random(shape).astype(dtype)
MinRK
better serialization for parallel code...
r7967 def test_roundtrip_simple():
for obj in [
'hello',
dict(a='b', b=10),
[1,2,'hi'],
(b'123', 'hello'),
]:
obj2 = roundtrip(obj)
Thomas Kluyver
Remove uses of @parametric decorator
r12374 nt.assert_equal(obj, obj2)
MinRK
better serialization for parallel code...
r7967
def test_roundtrip_nested():
for obj in [
dict(a=range(5), b={1:b'hello'}),
[range(5),[range(3),(1,[b'whoda'])]],
]:
obj2 = roundtrip(obj)
Thomas Kluyver
Remove uses of @parametric decorator
r12374 nt.assert_equal(obj, obj2)
MinRK
better serialization for parallel code...
r7967
def test_roundtrip_buffered():
for obj in [
dict(a=b"x"*1025),
b"hello"*500,
[b"hello"*501, 1,2,3]
]:
bufs = serialize_object(obj)
Thomas Kluyver
Remove uses of @parametric decorator
r12374 nt.assert_equal(len(bufs), 2)
MinRK
s/unserialize/deserialize
r18330 obj2, remainder = deserialize_object(bufs)
Thomas Kluyver
Remove uses of @parametric decorator
r12374 nt.assert_equal(remainder, [])
nt.assert_equal(obj, obj2)
MinRK
better serialization for parallel code...
r7967
@dec.skip_without('numpy')
def test_numpy():
import numpy
from numpy.testing.utils import assert_array_equal
MinRK
update serialize tests
r7972 for shape in SHAPES:
for dtype in DTYPES:
MinRK
Fix NaN warnings and failures in test_serialize...
r17119 A = new_array(shape, dtype=dtype)
MinRK
update serialize tests
r7972 bufs = serialize_object(A)
MinRK
s/unserialize/deserialize
r18330 B, r = deserialize_object(bufs)
Thomas Kluyver
Remove uses of @parametric decorator
r12374 nt.assert_equal(r, [])
nt.assert_equal(A.shape, B.shape)
nt.assert_equal(A.dtype, B.dtype)
assert_array_equal(A,B)
MinRK
update serialize tests
r7972
@dec.skip_without('numpy')
def test_recarray():
import numpy
from numpy.testing.utils import assert_array_equal
for shape in SHAPES:
for dtype in [
[('f', float), ('s', '|S10')],
[('n', int), ('s', '|S1'), ('u', 'uint32')],
]:
MinRK
Fix NaN warnings and failures in test_serialize...
r17119 A = new_array(shape, dtype=dtype)
MinRK
update serialize tests
r7972
MinRK
better serialization for parallel code...
r7967 bufs = serialize_object(A)
MinRK
s/unserialize/deserialize
r18330 B, r = deserialize_object(bufs)
Thomas Kluyver
Remove uses of @parametric decorator
r12374 nt.assert_equal(r, [])
nt.assert_equal(A.shape, B.shape)
nt.assert_equal(A.dtype, B.dtype)
assert_array_equal(A,B)
MinRK
better serialization for parallel code...
r7967
@dec.skip_without('numpy')
def test_numpy_in_seq():
import numpy
from numpy.testing.utils import assert_array_equal
MinRK
update serialize tests
r7972 for shape in SHAPES:
for dtype in DTYPES:
MinRK
Fix NaN warnings and failures in test_serialize...
r17119 A = new_array(shape, dtype=dtype)
MinRK
better serialization for parallel code...
r7967 bufs = serialize_object((A,1,2,b'hello'))
canned = pickle.loads(bufs[0])
Thomas Kluyver
Remove uses of @parametric decorator
r12374 nt.assert_is_instance(canned[0], CannedArray)
MinRK
s/unserialize/deserialize
r18330 tup, r = deserialize_object(bufs)
MinRK
better serialization for parallel code...
r7967 B = tup[0]
Thomas Kluyver
Remove uses of @parametric decorator
r12374 nt.assert_equal(r, [])
nt.assert_equal(A.shape, B.shape)
nt.assert_equal(A.dtype, B.dtype)
assert_array_equal(A,B)
MinRK
better serialization for parallel code...
r7967
@dec.skip_without('numpy')
def test_numpy_in_dict():
import numpy
from numpy.testing.utils import assert_array_equal
MinRK
update serialize tests
r7972 for shape in SHAPES:
for dtype in DTYPES:
MinRK
Fix NaN warnings and failures in test_serialize...
r17119 A = new_array(shape, dtype=dtype)
MinRK
better serialization for parallel code...
r7967 bufs = serialize_object(dict(a=A,b=1,c=range(20)))
canned = pickle.loads(bufs[0])
Thomas Kluyver
Remove uses of @parametric decorator
r12374 nt.assert_is_instance(canned['a'], CannedArray)
MinRK
s/unserialize/deserialize
r18330 d, r = deserialize_object(bufs)
MinRK
better serialization for parallel code...
r7967 B = d['a']
Thomas Kluyver
Remove uses of @parametric decorator
r12374 nt.assert_equal(r, [])
nt.assert_equal(A.shape, B.shape)
nt.assert_equal(A.dtype, B.dtype)
assert_array_equal(A,B)
MinRK
test canning classes
r9001
def test_class():
@interactive
class C(object):
a=5
bufs = serialize_object(dict(C=C))
canned = pickle.loads(bufs[0])
Thomas Kluyver
Remove uses of @parametric decorator
r12374 nt.assert_is_instance(canned['C'], CannedClass)
MinRK
s/unserialize/deserialize
r18330 d, r = deserialize_object(bufs)
MinRK
test canning classes
r9001 C2 = d['C']
Thomas Kluyver
Remove uses of @parametric decorator
r12374 nt.assert_equal(C2.a, C.a)
MinRK
test canning classes
r9001
def test_class_oldstyle():
@interactive
class C:
a=5
MinRK
better serialization for parallel code...
r7967
MinRK
test canning classes
r9001 bufs = serialize_object(dict(C=C))
canned = pickle.loads(bufs[0])
Thomas Kluyver
Remove uses of @parametric decorator
r12374 nt.assert_is_instance(canned['C'], CannedClass)
MinRK
s/unserialize/deserialize
r18330 d, r = deserialize_object(bufs)
MinRK
test canning classes
r9001 C2 = d['C']
Thomas Kluyver
Remove uses of @parametric decorator
r12374 nt.assert_equal(C2.a, C.a)
MinRK
better serialization for parallel code...
r7967
MinRK
test serializing lists / tuples / namedtuples
r9713 def test_tuple():
tup = (lambda x:x, 1)
bufs = serialize_object(tup)
canned = pickle.loads(bufs[0])
Thomas Kluyver
Remove uses of @parametric decorator
r12374 nt.assert_is_instance(canned, tuple)
MinRK
s/unserialize/deserialize
r18330 t2, r = deserialize_object(bufs)
Thomas Kluyver
Remove uses of @parametric decorator
r12374 nt.assert_equal(t2[0](t2[1]), tup[0](tup[1]))
MinRK
test serializing lists / tuples / namedtuples
r9713
point = namedtuple('point', 'x y')
def test_namedtuple():
p = point(1,2)
bufs = serialize_object(p)
canned = pickle.loads(bufs[0])
Thomas Kluyver
Remove uses of @parametric decorator
r12374 nt.assert_is_instance(canned, point)
MinRK
s/unserialize/deserialize
r18330 p2, r = deserialize_object(bufs, globals())
Thomas Kluyver
Remove uses of @parametric decorator
r12374 nt.assert_equal(p2.x, p.x)
nt.assert_equal(p2.y, p.y)
MinRK
test serializing lists / tuples / namedtuples
r9713
def test_list():
lis = [lambda x:x, 1]
bufs = serialize_object(lis)
canned = pickle.loads(bufs[0])
Thomas Kluyver
Remove uses of @parametric decorator
r12374 nt.assert_is_instance(canned, list)
MinRK
s/unserialize/deserialize
r18330 l2, r = deserialize_object(bufs)
Thomas Kluyver
Remove uses of @parametric decorator
r12374 nt.assert_equal(l2[0](l2[1]), lis[0](lis[1]))
MinRK
test serializing lists / tuples / namedtuples
r9713
MinRK
test canning classes
r9001 def test_class_inheritance():
@interactive
class C(object):
a=5
@interactive
class D(C):
b=10
bufs = serialize_object(dict(D=D))
canned = pickle.loads(bufs[0])
Thomas Kluyver
Remove uses of @parametric decorator
r12374 nt.assert_is_instance(canned['D'], CannedClass)
MinRK
s/unserialize/deserialize
r18330 d, r = deserialize_object(bufs)
MinRK
test canning classes
r9001 D2 = d['D']
Thomas Kluyver
Remove uses of @parametric decorator
r12374 nt.assert_equal(D2.a, D.a)
nt.assert_equal(D2.b, D.b)