def testCallback(self): class Foo(object): def __init__(self): self.x = 1 class Bar(object): def __init__(self): self.foo = Foo() def serialize(obj): return dict(obj.__dict__, **{"_pytype_": type(obj).__name__}) def deserialize(obj): if obj["_pytype_"] == "Foo": result = Foo() elif obj["_pytype_"] == "Bar": result = Bar() obj.pop("_pytype_", None) result.__dict__ = obj return result bar = Bar() bar.foo.x = 42 numbuf.register_callbacks(serialize, deserialize) metadata, size, serialized = numbuf.serialize_list([bar]) self.assertEqual(numbuf.deserialize_list(serialized)[0].foo.x, 42)
def testObjectArray(self): x = np.array([1, 2, "hello"], dtype=object) y = np.array([[1, 2], [3, 4]], dtype=object) def myserialize(obj): return {"_pytype_": "numpy.array", "data": obj.tolist()} def mydeserialize(obj): if obj["_pytype_"] == "numpy.array": return np.array(obj["data"], dtype=object) numbuf.register_callbacks(myserialize, mydeserialize) metadata, size, serialized = numbuf.serialize_list([x, y]) assert_equal(numbuf.deserialize_list(serialized), [x, y])