|
1 | | -""" BloomFilter and BloomFiter on Disk, python implementation |
2 | | - License: MIT |
3 | | - Author: Tyler Barrus (barrust@gmail.com) |
4 | | - URL: https://github.com/barrust/bloom |
| 1 | +"""BloomFilter and BloomFiter on Disk, python implementation |
| 2 | +License: MIT |
| 3 | +Author: Tyler Barrus (barrust@gmail.com) |
| 4 | +URL: https://github.com/barrust/bloom |
5 | 5 | """ |
| 6 | + |
6 | 7 | import math |
7 | 8 | import os |
8 | 9 | from array import array |
9 | 10 | from binascii import hexlify, unhexlify |
10 | 11 | from collections.abc import ByteString |
11 | | -from io import BytesIO, IOBase |
| 12 | +from io import BufferedRandom, BytesIO, IOBase |
12 | 13 | from mmap import mmap |
13 | 14 | from numbers import Number |
14 | 15 | from pathlib import Path |
@@ -308,10 +309,7 @@ def export_c_header(self, filename: Union[str, Path]) -> None: |
308 | 309 | Args: |
309 | 310 | filename (str): The filename to which the Bloom Filter will be written.""" |
310 | 311 | data = (" " + line for line in wrap(", ".join(f"0x{e:02x}" for e in bytearray.fromhex(self.export_hex())), 80)) |
311 | | - if self._type in ["regular", "regular-on-disk"]: |
312 | | - bloom_type = "standard BloomFilter" |
313 | | - else: |
314 | | - bloom_type = "CountingBloomFilter" |
| 312 | + bloom_type = "standard BloomFilter" if self._type in ["regular", "regular-on-disk"] else "CountingBloomFilter" |
315 | 313 |
|
316 | 314 | with open(filename, "w", encoding="utf-8") as file: |
317 | 315 | print(f"/* BloomFilter Export of a {bloom_type} */", file=file) |
@@ -570,9 +568,7 @@ def _verify_bloom_similarity(self, second: SimpleBloomT) -> bool: |
570 | 568 | hash_match = self.number_hashes != second.number_hashes |
571 | 569 | same_bits = self.number_bits != second.number_bits |
572 | 570 | next_hash = self.hashes("test") != second.hashes("test") |
573 | | - if hash_match or same_bits or next_hash: |
574 | | - return False |
575 | | - return True |
| 571 | + return not (hash_match or same_bits or next_hash) |
576 | 572 |
|
577 | 573 |
|
578 | 574 | class BloomFilterOnDisk(BloomFilter): |
@@ -609,7 +605,7 @@ def __init__( |
609 | 605 | ) -> None: |
610 | 606 | # set some things up |
611 | 607 | self._filepath = resolve_path(filepath) |
612 | | - self.__file_pointer = None |
| 608 | + self.__file_pointer: Union[BufferedRandom, None] = None |
613 | 609 | super().__init__(est_elements, false_positive_rate, filepath, hex_string, hash_function) |
614 | 610 |
|
615 | 611 | def _load_init(self, filepath, hash_function, hex_string, est_elements, false_positive_rate): |
@@ -644,7 +640,7 @@ def close(self) -> None: |
644 | 640 | """Clean up the BloomFilterOnDisk object""" |
645 | 641 | if self.__file_pointer is not None and not self.__file_pointer.closed: |
646 | 642 | self.__update() |
647 | | - self._bloom.close() |
| 643 | + self._bloom.close() # type: ignore |
648 | 644 | self.__file_pointer.close() |
649 | 645 | self.__file_pointer = None |
650 | 646 |
|
@@ -673,7 +669,7 @@ def _load(self, file: Union[str, Path], hash_function: Union[HashFuncT, None] = |
673 | 669 | fpr, n_hashes, n_bits = self._get_optimized_params(est_els, fpr) |
674 | 670 | self._set_values(est_els, fpr, n_hashes, n_bits, hash_function) |
675 | 671 | # setup a few additional items |
676 | | - self.__file_pointer = open(file, "r+b") # type: ignore |
| 672 | + self.__file_pointer = open(file, "r+b") # noqa: SIM115 |
677 | 673 | self._bloom = mmap(self.__file_pointer.fileno(), 0) # type: ignore |
678 | 674 | self._on_disk = True |
679 | 675 |
|
|
0 commit comments