Пример #1
0
 def tofile(self, f):
     """Write the bloom filter to file object `f'. Underlying bits
     are written as machine values. This is much more space
     efficient than pickling the object."""
     f.write(pack(self.FILE_FMT, self.error_rate, self.num_slices,
                  self.bits_per_slice, self.capacity, self.count))
     (f.write(self.bitarray.tobytes()) if is_string_io(f)
      else self.bitarray.tofile(f))
Пример #2
0
 def tofile(self, f):
     """Write the bloom filter to file object `f'. Underlying bits
     are written as machine values. This is much more space
     efficient than pickling the object."""
     f.write(
         pack(self.FILE_FMT, self.error_rate, self.num_slices,
              self.bits_per_slice, self.capacity, self.count))
     (f.write(self.bitarray.tobytes())
      if is_string_io(f) else self.bitarray.tofile(f))
Пример #3
0
    def fromfile(cls, f, n=-1):
        """Read a bloom filter from file-object `f' serialized with
        ``BloomFilter.tofile''. If `n' > 0 read only so many bytes."""
        headerlen = calcsize(cls.FILE_FMT)

        if 0 < n < headerlen:
            raise ValueError('n too small!')

        filter = cls(1)  # Bogus instantiation, we will `_setup'.
        filter._setup(*unpack(cls.FILE_FMT, f.read(headerlen)))
        filter.bitarray = bitarray.bitarray(endian='little')
        if n > 0:
            (filter.bitarray.frombytes(f.read(n - headerlen)) if
             is_string_io(f) else filter.bitarray.fromfile(f, n - headerlen))
        else:
            (filter.bitarray.frombytes(f.read())
             if is_string_io(f) else filter.bitarray.fromfile(f))
        if filter.num_bits != filter.bitarray.length() and \
               (filter.num_bits + (8 - filter.num_bits % 8)
                != filter.bitarray.length()):
            raise ValueError('Bit length mismatch!')

        return filter
Пример #4
0
    def fromfile(cls, f, n=-1):
        """Read a bloom filter from file-object `f' serialized with
        ``BloomFilter.tofile''. If `n' > 0 read only so many bytes."""
        headerlen = calcsize(cls.FILE_FMT)

        if 0 < n < headerlen:
            raise ValueError('n too small!')

        filter = cls(1)  # Bogus instantiation, we will `_setup'.
        filter._setup(*unpack(cls.FILE_FMT, f.read(headerlen)))
        filter.bitarray = bitarray.bitarray(endian='little')
        if n > 0:
            (filter.bitarray.frombytes(f.read(n-headerlen)) if is_string_io(f)
             else filter.bitarray.fromfile(f, n - headerlen))
        else:
            (filter.bitarray.frombytes(f.read()) if is_string_io(f)
             else filter.bitarray.fromfile(f))
        if filter.num_bits != filter.bitarray.length() and \
               (filter.num_bits + (8 - filter.num_bits % 8)
                != filter.bitarray.length()):
            raise ValueError('Bit length mismatch!')

        return filter