def test_zero_dimension(self): provider = WXFExprProvider(NumPyWXFEncoder()) arr = numpy.ndarray([2, 0, 3]) with self.assertRaises(Exception) as err: next(provider.provide_wxfexpr(arr)) self.assertEqual(force_text(err.exception), "Dimensions must be positive integers.")
def test_default(self): expr_provider = WXFExprProvider(default=list) stream = six.BytesIO() serializer = WXFExprSerializer(stream, expr_provider=expr_provider) serializer.serialize(range(1, 4)) wxf = b"\x38\x3a\x66\x03\x73\x04\x4c\x69\x73\x74\x43\x01\x43\x02\x43\x03" self.assertSequenceEqual(stream.getvalue(), wxf)
def test_custom_encoder(self): """ test re-entrant calls """ expr_provider = WXFExprProvider() expr_provider.add_encoder(MyClassEncoder()) stream = six.BytesIO() serializer = WXFExprSerializer(stream, expr_provider=expr_provider) myclass2 = MyClass2(True, "foobar") myclass1 = MyClass1(1, None) myclass = MyClass1(1, 2, [myclass2, myclass1]) o = ["foo", [1, {"k1": myclass, "k2": False}]] serializer.serialize(o) data_dir = dir_test_data() filepath = os.path.join(data_dir, "test.wxf") with open(filepath, "wb") as w_file: w_file.write(stream.getvalue()) os.remove(filepath)
def test_custom_encoder(self): ''' test re-entrant calls ''' expr_provider = WXFExprProvider() expr_provider.add_encoder(MyClassEncoder()) stream = six.BytesIO() serializer = WXFExprSerializer(stream, expr_provider=expr_provider) myclass2 = MyClass2(True, 'foobar') myclass1 = MyClass1(1, None) myclass = MyClass1(1, 2, [myclass2, myclass1]) o = ['foo', [1, {'k1': myclass, 'k2': False}]] serializer.serialize(o) data_dir = dir_test_data() filepath = os.path.join(data_dir, 'test.wxf') with open(filepath, 'wb') as w_file: w_file.write(stream.getvalue()) os.remove(filepath)
def init(compress=False, enforce=True): expr_provider = WXFExprProvider() stream = six.BytesIO() serializer = WXFExprSerializer(stream, expr_provider=expr_provider, enforce=True, compress=compress) return (serializer, stream)
def init(pa, ra): expr_provider = WXFExprProvider() numpy_encoder = NumPyWXFEncoder(packed_array_support=pa, numeric_array_support=ra) expr_provider.add_encoder(numpy_encoder) expr_provider.add_encoder(DefaultWXFEncoder()) serializer = WXFExprSerializer(six.BytesIO(), expr_provider=expr_provider) return serializer
def test_dimensions(self): provider = WXFExprProvider(NumPyWXFEncoder()) arr = numpy.ndarray([2, 1, 3]) wxfExpr = next(provider.provide_wxfexpr(arr)) self.assertEqual(wxfExpr.dimensions, (2, 1, 3))