如何正确序列化python bitarray?

时间:2016-03-09 13:10:15

标签: python serialization bitarray

简单地使用tobytes和frombytes方法并不总能提供如下所示的等效对象。

这似乎源于源bitarray没有可被8整除的长度。

from bitarray import bitarray  # pip install bitarray


foo = bitarray('0000000000000000000000000000000000000000000000000000000000'
               '0000000000000000000000000000000000000000000000000000000000'
               '0000000000000000000000000000000000000000000000000010000000'
               '0000000000000000000000000000000000000000000000000000000000'
               '0000000000000000000000000000000000000000000000000000000000'
               '0000000000000000000000000000000000000000000000000000000000'
               '0000000000000000000000000000000000000000000000010000000000'
               '0000000000000000000000000000000000000000000000000000000000'
               '0000000000000000000000000000000000000000000000000000000000'
               '0000000000010000000000000000000000000000000000000000000000'
               '0000000000000000000000000000000000000000000000000000000000'
               '0000000000000000000000000000000000000000000000000000000000'
               '0000000000000000000000000000000000000000000000000000000000'
               '0000000000000000000000000000000000000000000000000000000000'
               '0000000000000000000000000000000000000000000000000000000000'
               '000000000000000000000000000000000000000000000001000000',
               endian="little")


bar = bitarray(endian="little")
bar.frombytes(foo.tobytes())
assert(foo.tobytes() == bar.tobytes())  # passes
assert(foo == bar)  # fails

2 个答案:

答案 0 :(得分:0)

解决方案是保存长度并修剪反序列化的比特阵列。

from bitarray import bitarray  # pip install bitarray
import json
from base64 import b64encode, b64decode


def serialize(ba):
    return json.dumps({
        "endian": ba.endian(),
        "bytes": b64encode(ba.tobytes()),
        "len": len(ba)
    })


def deserialize(data):
    data = json.loads(data)
    ba = bitarray(endian=data["endian"])
    ba.frombytes(b64decode(data["bytes"]))
    return ba[:data["len"]]


foo = bitarray('0000000000000000000000000000000000000000000000000000000000'
               '0000000000000000000000000000000000000000000000000000000000'
               '0000000000000000000000000000000000000000000000000010000000'
               '0000000000000000000000000000000000000000000000000000000000'
               '0000000000000000000000000000000000000000000000000000000000'
               '0000000000000000000000000000000000000000000000000000000000'
               '0000000000000000000000000000000000000000000000010000000000'
               '0000000000000000000000000000000000000000000000000000000000'
               '0000000000000000000000000000000000000000000000000000000000'
               '0000000000010000000000000000000000000000000000000000000000'
               '0000000000000000000000000000000000000000000000000000000000'
               '0000000000000000000000000000000000000000000000000000000000'
               '0000000000000000000000000000000000000000000000000000000000'
               '0000000000000000000000000000000000000000000000000000000000'
               '0000000000000000000000000000000000000000000000000000000000'
               '000000000000000000000000000000000000000000000001000000',
               endian="little")


bar = deserialize(serialize(foo))
assert(foo.tobytes() == bar.tobytes())
assert(foo == bar)

答案 1 :(得分:0)

您可以直接使用bitarray python模块保存和加载位数组

# Save bitarray
ba = bitarray('11100000')
with open('bitarray.bin', 'wb') as f:
        ba.tofile(f)

# Load bitarray
ba = bitarray()
with open('bitarray.bin', 'rb') as f:
        ba.fromfile(f)