コード例 #1
0
 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)
コード例 #2
0
    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)
コード例 #3
0
ファイル: wxf_numpy.py プロジェクト: cosmosZhou/sagemath
    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
コード例 #4
0
    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)
コード例 #5
0
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)