Skip to content

Commit

Permalink
Update pybloom.py
Browse files Browse the repository at this point in the history
  • Loading branch information
JannikWortmann authored Apr 12, 2022
1 parent 2bbe01a commit b6e7b97
Showing 1 changed file with 21 additions and 0 deletions.
21 changes: 21 additions & 0 deletions pybloom/pybloom.py
Original file line number Diff line number Diff line change
Expand Up @@ -274,6 +274,27 @@ def fromfile(cls, f, n=-1):
raise ValueError('Bit length mismatch!')

return filter

def toBytes(self):
filter = bytearray()
filter.extend(pack(self.FILE_FMT, self.error_rate, self.num_slices,
self.bits_per_slice, self.capacity, self.count))
filter.extend(self.bitarray.tobytes())
return bytes(filter)

@classmethod
def fromBytes(cls, bytes):
headerlen = calcsize(cls.FILE_FMT)
filter = cls(1)
filter._setup(*unpack(cls.FILE_FMT, bytes[: headerlen]))
filter.bitarray = bitarray.bitarray(endian='little')
filter.bitarray.frombytes(bytes[headerlen:])

if filter.num_bits != len(filter.bitarray) and \
(filter.num_bits + (8 - filter.num_bits % 8) != len(filter.bitarray)):
raise ValueError('Bit length mismatch!')

return filter

def __getstate__(self):
d = self.__dict__.copy()
Expand Down

0 comments on commit b6e7b97

Please sign in to comment.