Beispiel #1
0
    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)
Beispiel #2
0
 def testObjectArrayImmutable(self):
     obj = np.zeros([10])
     schema, size, serialized = numbuf.serialize_list([obj])
     result = numbuf.deserialize_list(serialized)
     assert_equal(result[0], obj)
     with self.assertRaises(ValueError):
         result[0][0] = 1
Beispiel #3
0
 def testBuffer(self):
     for (i, obj) in enumerate(TEST_OBJECTS):
         size, batch = numbuf.serialize_list([obj])
         size = size
         buff = np.zeros(size, dtype="uint8")
         numbuf.write_to_buffer(batch, memoryview(buff))
         array = numbuf.read_from_buffer(memoryview(buff))
         result = numbuf.deserialize_list(array)
         assert_equal(result[0], obj)
Beispiel #4
0
 def testBuffer(self):
     for (i, obj) in enumerate(TEST_OBJECTS):
         schema, size, batch = numbuf.serialize_list([obj])
         size = size + 4096  # INITIAL_METADATA_SIZE in arrow.
         buff = np.zeros(size, dtype="uint8")
         metadata_offset = numbuf.write_to_buffer(batch, memoryview(buff))
         array = numbuf.read_from_buffer(memoryview(buff),
                                         memoryview(schema),
                                         metadata_offset)
         result = numbuf.deserialize_list(array)
         assert_equal(result[0], obj)
Beispiel #5
0
    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])
Beispiel #6
0
 def roundTripTest(self, data):
     schema, size, serialized = numbuf.serialize_list(data)
     result = numbuf.deserialize_list(serialized)
     assert_equal(data, result)