pyprobables 0.6.2__tar.gz → 0.7.1__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- {pyprobables-0.6.2 → pyprobables-0.7.1}/PKG-INFO +9 -5
- {pyprobables-0.6.2 → pyprobables-0.7.1}/README.rst +7 -2
- {pyprobables-0.6.2 → pyprobables-0.7.1}/probables/__init__.py +3 -1
- {pyprobables-0.6.2 → pyprobables-0.7.1}/probables/blooms/bloom.py +43 -46
- {pyprobables-0.6.2 → pyprobables-0.7.1}/probables/blooms/countingbloom.py +23 -26
- {pyprobables-0.6.2 → pyprobables-0.7.1}/probables/blooms/expandingbloom.py +14 -15
- {pyprobables-0.6.2 → pyprobables-0.7.1}/probables/countminsketch/countminsketch.py +38 -39
- {pyprobables-0.6.2 → pyprobables-0.7.1}/probables/cuckoo/countingcuckoo.py +48 -28
- {pyprobables-0.6.2 → pyprobables-0.7.1}/probables/cuckoo/cuckoo.py +44 -25
- {pyprobables-0.6.2 → pyprobables-0.7.1}/probables/exceptions.py +11 -0
- {pyprobables-0.6.2 → pyprobables-0.7.1}/probables/hashes.py +5 -4
- {pyprobables-0.6.2 → pyprobables-0.7.1}/probables/quotientfilter/quotientfilter.py +34 -29
- {pyprobables-0.6.2 → pyprobables-0.7.1}/probables/utilities.py +43 -6
- {pyprobables-0.6.2 → pyprobables-0.7.1}/pyprobables.egg-info/PKG-INFO +9 -5
- {pyprobables-0.6.2 → pyprobables-0.7.1}/pyproject.toml +4 -4
- {pyprobables-0.6.2 → pyprobables-0.7.1}/tests/test_utilities.py +26 -1
- {pyprobables-0.6.2 → pyprobables-0.7.1}/LICENSE +0 -0
- {pyprobables-0.6.2 → pyprobables-0.7.1}/probables/blooms/__init__.py +0 -0
- {pyprobables-0.6.2 → pyprobables-0.7.1}/probables/blooms/py.typed +0 -0
- {pyprobables-0.6.2 → pyprobables-0.7.1}/probables/constants.py +0 -0
- {pyprobables-0.6.2 → pyprobables-0.7.1}/probables/countminsketch/__init__.py +0 -0
- {pyprobables-0.6.2 → pyprobables-0.7.1}/probables/countminsketch/py.typed +0 -0
- {pyprobables-0.6.2 → pyprobables-0.7.1}/probables/cuckoo/__init__.py +0 -0
- {pyprobables-0.6.2 → pyprobables-0.7.1}/probables/cuckoo/py.typed +0 -0
- {pyprobables-0.6.2 → pyprobables-0.7.1}/probables/py.typed +0 -0
- {pyprobables-0.6.2 → pyprobables-0.7.1}/probables/quotientfilter/__init__.py +0 -0
- {pyprobables-0.6.2 → pyprobables-0.7.1}/probables/quotientfilter/py.typed +0 -0
- {pyprobables-0.6.2 → pyprobables-0.7.1}/pyprobables.egg-info/SOURCES.txt +0 -0
- {pyprobables-0.6.2 → pyprobables-0.7.1}/pyprobables.egg-info/dependency_links.txt +0 -0
- {pyprobables-0.6.2 → pyprobables-0.7.1}/pyprobables.egg-info/top_level.txt +0 -0
- {pyprobables-0.6.2 → pyprobables-0.7.1}/setup.cfg +0 -0
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 2.4
|
|
2
2
|
Name: pyprobables
|
|
3
|
-
Version: 0.
|
|
3
|
+
Version: 0.7.1
|
|
4
4
|
Summary: Probabilistic data structures in python
|
|
5
5
|
Author-email: Tyler Barrus <barrust@gmail.com>
|
|
6
6
|
License-Expression: MIT
|
|
@@ -16,13 +16,12 @@ Classifier: Topic :: Software Development :: Libraries
|
|
|
16
16
|
Classifier: Topic :: Utilities
|
|
17
17
|
Classifier: Programming Language :: Python
|
|
18
18
|
Classifier: Programming Language :: Python :: 3
|
|
19
|
-
Classifier: Programming Language :: Python :: 3.9
|
|
20
19
|
Classifier: Programming Language :: Python :: 3.10
|
|
21
20
|
Classifier: Programming Language :: Python :: 3.11
|
|
22
21
|
Classifier: Programming Language :: Python :: 3.12
|
|
23
22
|
Classifier: Programming Language :: Python :: 3.13
|
|
24
23
|
Classifier: Programming Language :: Python :: 3.14
|
|
25
|
-
Requires-Python: >=3.
|
|
24
|
+
Requires-Python: >=3.10
|
|
26
25
|
Description-Content-Type: text/x-rst
|
|
27
26
|
License-File: LICENSE
|
|
28
27
|
Dynamic: license-file
|
|
@@ -76,6 +75,11 @@ Pip Installation:
|
|
|
76
75
|
|
|
77
76
|
$ pip install pyprobables
|
|
78
77
|
|
|
78
|
+
Conda Installation:
|
|
79
|
+
::
|
|
80
|
+
|
|
81
|
+
$ conda install -c conda-forge pyprobables
|
|
82
|
+
|
|
79
83
|
To install from source:
|
|
80
84
|
|
|
81
85
|
To install `pyprobables`, simply clone the `repository on GitHub
|
|
@@ -85,13 +89,13 @@ To install `pyprobables`, simply clone the `repository on GitHub
|
|
|
85
89
|
|
|
86
90
|
$ python setup.py install
|
|
87
91
|
|
|
88
|
-
`pyprobables` supports python 3.
|
|
92
|
+
`pyprobables` supports python 3.10 - 3.14+
|
|
89
93
|
|
|
90
94
|
For *python 2.7* support, install `release 0.3.2 <https://github.com/barrust/pyprobables/releases/tag/v0.3.2>`__
|
|
91
95
|
|
|
92
96
|
::
|
|
93
97
|
|
|
94
|
-
$ pip install pyprobables==0.
|
|
98
|
+
$ pip install pyprobables==0.7.0
|
|
95
99
|
|
|
96
100
|
|
|
97
101
|
API Documentation
|
|
@@ -47,6 +47,11 @@ Pip Installation:
|
|
|
47
47
|
|
|
48
48
|
$ pip install pyprobables
|
|
49
49
|
|
|
50
|
+
Conda Installation:
|
|
51
|
+
::
|
|
52
|
+
|
|
53
|
+
$ conda install -c conda-forge pyprobables
|
|
54
|
+
|
|
50
55
|
To install from source:
|
|
51
56
|
|
|
52
57
|
To install `pyprobables`, simply clone the `repository on GitHub
|
|
@@ -56,13 +61,13 @@ To install `pyprobables`, simply clone the `repository on GitHub
|
|
|
56
61
|
|
|
57
62
|
$ python setup.py install
|
|
58
63
|
|
|
59
|
-
`pyprobables` supports python 3.
|
|
64
|
+
`pyprobables` supports python 3.10 - 3.14+
|
|
60
65
|
|
|
61
66
|
For *python 2.7* support, install `release 0.3.2 <https://github.com/barrust/pyprobables/releases/tag/v0.3.2>`__
|
|
62
67
|
|
|
63
68
|
::
|
|
64
69
|
|
|
65
|
-
$ pip install pyprobables==0.
|
|
70
|
+
$ pip install pyprobables==0.7.0
|
|
66
71
|
|
|
67
72
|
|
|
68
73
|
API Documentation
|
|
@@ -15,6 +15,7 @@ from probables.exceptions import (
|
|
|
15
15
|
NotSupportedError,
|
|
16
16
|
ProbablesBaseException,
|
|
17
17
|
RotatingBloomFilterError,
|
|
18
|
+
SimilarityError,
|
|
18
19
|
)
|
|
19
20
|
from probables.quotientfilter import QuotientFilter
|
|
20
21
|
from probables.utilities import Bitarray
|
|
@@ -23,7 +24,7 @@ __author__ = "Tyler Barrus"
|
|
|
23
24
|
__maintainer__ = "Tyler Barrus"
|
|
24
25
|
__email__ = "barrust@gmail.com"
|
|
25
26
|
__license__ = "MIT"
|
|
26
|
-
__version__ = "0.
|
|
27
|
+
__version__ = "0.7.1"
|
|
27
28
|
__credits__: list[str] = []
|
|
28
29
|
__url__ = "https://github.com/barrust/pyprobables"
|
|
29
30
|
__bugtrack_url__ = "https://github.com/barrust/pyprobables/issues"
|
|
@@ -48,4 +49,5 @@ __all__ = [
|
|
|
48
49
|
"RotatingBloomFilterError",
|
|
49
50
|
"QuotientFilter",
|
|
50
51
|
"Bitarray",
|
|
52
|
+
"SimilarityError",
|
|
51
53
|
]
|
|
@@ -18,7 +18,7 @@ from struct import Struct
|
|
|
18
18
|
from textwrap import wrap
|
|
19
19
|
from typing import Union
|
|
20
20
|
|
|
21
|
-
from probables.exceptions import InitializationError, NotSupportedError
|
|
21
|
+
from probables.exceptions import InitializationError, NotSupportedError, SimilarityError
|
|
22
22
|
from probables.hashes import HashFuncT, HashResultsT, KeyT, default_fnv_1a
|
|
23
23
|
from probables.utilities import MMap, is_hex_string, is_valid_file, resolve_path
|
|
24
24
|
|
|
@@ -29,7 +29,7 @@ SimpleBloomT = Union["BloomFilter", "BloomFilterOnDisk"]
|
|
|
29
29
|
|
|
30
30
|
def _verify_not_type_mismatch(second: SimpleBloomT) -> bool:
|
|
31
31
|
"""verify that there is not a type mismatch"""
|
|
32
|
-
return isinstance(second,
|
|
32
|
+
return isinstance(second, BloomFilter | BloomFilterOnDisk)
|
|
33
33
|
|
|
34
34
|
|
|
35
35
|
class BloomFilter:
|
|
@@ -68,11 +68,11 @@ class BloomFilter:
|
|
|
68
68
|
|
|
69
69
|
def __init__(
|
|
70
70
|
self,
|
|
71
|
-
est_elements:
|
|
72
|
-
false_positive_rate:
|
|
73
|
-
filepath:
|
|
74
|
-
hex_string:
|
|
75
|
-
hash_function:
|
|
71
|
+
est_elements: int | None = None,
|
|
72
|
+
false_positive_rate: float | None = None,
|
|
73
|
+
filepath: str | Path | None = None,
|
|
74
|
+
hex_string: str | None = None,
|
|
75
|
+
hash_function: HashFuncT | None = None,
|
|
76
76
|
):
|
|
77
77
|
# set some things up
|
|
78
78
|
self._on_disk = False
|
|
@@ -110,7 +110,7 @@ class BloomFilter:
|
|
|
110
110
|
_FPR_STRUCT = Struct("f")
|
|
111
111
|
_IMPT_STRUCT = Struct("B")
|
|
112
112
|
|
|
113
|
-
def __contains__(self, key: KeyT) ->
|
|
113
|
+
def __contains__(self, key: KeyT) -> int | bool:
|
|
114
114
|
"""setup the `in` keyword"""
|
|
115
115
|
return self.check(key)
|
|
116
116
|
|
|
@@ -220,7 +220,7 @@ class BloomFilter:
|
|
|
220
220
|
for idx in range(self._bloom_length):
|
|
221
221
|
self._bloom[idx] = 0
|
|
222
222
|
|
|
223
|
-
def hashes(self, key: KeyT, depth:
|
|
223
|
+
def hashes(self, key: KeyT, depth: int | None = None) -> HashResultsT:
|
|
224
224
|
"""Return the hashes based on the provided key
|
|
225
225
|
|
|
226
226
|
Args:
|
|
@@ -284,12 +284,12 @@ class BloomFilter:
|
|
|
284
284
|
bytes_string = hexlify(bytearray(self._bloom[: self.bloom_length])) + hexlify(footer_bytes)
|
|
285
285
|
return str(bytes_string, "utf-8")
|
|
286
286
|
|
|
287
|
-
def export(self, file:
|
|
287
|
+
def export(self, file: Path | str | IOBase | mmap) -> None:
|
|
288
288
|
"""Export the Bloom Filter to disk
|
|
289
289
|
|
|
290
290
|
Args:
|
|
291
291
|
file (str): The file or filepath to which the Bloom Filter will be written."""
|
|
292
|
-
if not isinstance(file,
|
|
292
|
+
if not isinstance(file, IOBase | mmap):
|
|
293
293
|
file = resolve_path(file)
|
|
294
294
|
with open(file, "wb") as filepointer:
|
|
295
295
|
self.export(filepointer)
|
|
@@ -303,7 +303,7 @@ class BloomFilter:
|
|
|
303
303
|
)
|
|
304
304
|
)
|
|
305
305
|
|
|
306
|
-
def export_c_header(self, filename:
|
|
306
|
+
def export_c_header(self, filename: str | Path) -> None:
|
|
307
307
|
"""Export the Bloom Filter to disk as a C header file.
|
|
308
308
|
|
|
309
309
|
Args:
|
|
@@ -322,7 +322,7 @@ class BloomFilter:
|
|
|
322
322
|
print("const unsigned char bloom[] = {", *data, "};", sep="\n", file=file)
|
|
323
323
|
|
|
324
324
|
@classmethod
|
|
325
|
-
def frombytes(cls, b: ByteString, hash_function:
|
|
325
|
+
def frombytes(cls, b: ByteString, hash_function: HashFuncT | None = None) -> "BloomFilter":
|
|
326
326
|
"""
|
|
327
327
|
Args:
|
|
328
328
|
b (ByteString): The bytes to load as a Bloom Filter
|
|
@@ -368,7 +368,7 @@ class BloomFilter:
|
|
|
368
368
|
exp = math.exp(dbl)
|
|
369
369
|
return math.pow((1 - exp), self.number_hashes)
|
|
370
370
|
|
|
371
|
-
def intersection(self, second: SimpleBloomT) ->
|
|
371
|
+
def intersection(self, second: SimpleBloomT) -> SimpleBloomT:
|
|
372
372
|
"""Return a new Bloom Filter that contains the intersection of the
|
|
373
373
|
two
|
|
374
374
|
|
|
@@ -378,15 +378,14 @@ class BloomFilter:
|
|
|
378
378
|
BloomFilter: The new Bloom Filter containing the intersection
|
|
379
379
|
Raises:
|
|
380
380
|
TypeError: When second is not either a :class:`BloomFilter` or :class:`BloomFilterOnDisk`
|
|
381
|
+
SimilarityError: When second is not of the same size (false_positive_rate and est_elements)
|
|
381
382
|
Note:
|
|
382
|
-
`second` may be a BloomFilterOnDisk object
|
|
383
|
-
Note:
|
|
384
|
-
If `second` is not of the same size (false_positive_rate and est_elements) then this will return `None`"""
|
|
383
|
+
`second` may be a BloomFilterOnDisk object"""
|
|
385
384
|
if not _verify_not_type_mismatch(second):
|
|
386
385
|
raise TypeError(MISMATCH_MSG)
|
|
387
386
|
|
|
388
387
|
if self._verify_bloom_similarity(second) is False:
|
|
389
|
-
|
|
388
|
+
raise SimilarityError("Bloom Filters are not similar")
|
|
390
389
|
|
|
391
390
|
res = BloomFilter(
|
|
392
391
|
self.estimated_elements,
|
|
@@ -399,7 +398,7 @@ class BloomFilter:
|
|
|
399
398
|
res.elements_added = res.estimate_elements()
|
|
400
399
|
return res
|
|
401
400
|
|
|
402
|
-
def union(self, second: SimpleBloomT) ->
|
|
401
|
+
def union(self, second: SimpleBloomT) -> "BloomFilter":
|
|
403
402
|
"""Return a new Bloom Filter that contains the union of the two
|
|
404
403
|
|
|
405
404
|
Args:
|
|
@@ -408,15 +407,14 @@ class BloomFilter:
|
|
|
408
407
|
BloomFilter: The new Bloom Filter containing the union
|
|
409
408
|
Raises:
|
|
410
409
|
TypeError: When second is not either a :class:`BloomFilter` or :class:`BloomFilterOnDisk`
|
|
410
|
+
SimilarityError: When second is not of the same size (false_positive_rate and est_elements)
|
|
411
411
|
Note:
|
|
412
|
-
`second` may be a BloomFilterOnDisk object
|
|
413
|
-
Note:
|
|
414
|
-
If `second` is not of the same size (false_positive_rate and est_elements) then this will return `None`"""
|
|
412
|
+
`second` may be a BloomFilterOnDisk object"""
|
|
415
413
|
if not _verify_not_type_mismatch(second):
|
|
416
414
|
raise TypeError(MISMATCH_MSG)
|
|
417
415
|
|
|
418
416
|
if self._verify_bloom_similarity(second) is False:
|
|
419
|
-
|
|
417
|
+
raise SimilarityError("Bloom Filters are not similar")
|
|
420
418
|
|
|
421
419
|
res = BloomFilter(
|
|
422
420
|
self.estimated_elements,
|
|
@@ -429,7 +427,7 @@ class BloomFilter:
|
|
|
429
427
|
res.elements_added = res.estimate_elements()
|
|
430
428
|
return res
|
|
431
429
|
|
|
432
|
-
def jaccard_index(self, second: SimpleBloomT) ->
|
|
430
|
+
def jaccard_index(self, second: SimpleBloomT) -> float:
|
|
433
431
|
"""Calculate the jaccard similarity score between two Bloom Filters
|
|
434
432
|
|
|
435
433
|
Args:
|
|
@@ -438,15 +436,14 @@ class BloomFilter:
|
|
|
438
436
|
float: A numeric value between 0 and 1 where 1 is identical and 0 means completely different
|
|
439
437
|
Raises:
|
|
440
438
|
TypeError: When second is not either a :class:`BloomFilter` or :class:`BloomFilterOnDisk`
|
|
439
|
+
SimilarityError: When second is not of the same size (false_positive_rate and est_elements)
|
|
441
440
|
Note:
|
|
442
|
-
`second` may be a BloomFilterOnDisk object
|
|
443
|
-
Note:
|
|
444
|
-
If `second` is not of the same size (false_positive_rate and est_elements) then this will return `None`"""
|
|
441
|
+
`second` may be a BloomFilterOnDisk object"""
|
|
445
442
|
if not _verify_not_type_mismatch(second):
|
|
446
443
|
raise TypeError(MISMATCH_MSG)
|
|
447
444
|
|
|
448
445
|
if self._verify_bloom_similarity(second) is False:
|
|
449
|
-
|
|
446
|
+
raise SimilarityError("Bloom Filters are not similar")
|
|
450
447
|
|
|
451
448
|
count_union = 0
|
|
452
449
|
|
|
@@ -491,7 +488,7 @@ class BloomFilter:
|
|
|
491
488
|
fpr: float,
|
|
492
489
|
n_hashes: int,
|
|
493
490
|
n_bits: int,
|
|
494
|
-
hash_func:
|
|
491
|
+
hash_func: HashFuncT | None,
|
|
495
492
|
) -> None:
|
|
496
493
|
self._est_elements = est_els
|
|
497
494
|
self._fpr = fpr
|
|
@@ -504,7 +501,7 @@ class BloomFilter:
|
|
|
504
501
|
self._number_hashes = n_hashes
|
|
505
502
|
self._num_bits = n_bits
|
|
506
503
|
|
|
507
|
-
def _load_hex(self, hex_string: str, hash_function:
|
|
504
|
+
def _load_hex(self, hex_string: str, hash_function: HashFuncT | None = None) -> None:
|
|
508
505
|
"""placeholder for loading from hex string"""
|
|
509
506
|
offset = self._FOOTER_STRUCT_BE.size * 2
|
|
510
507
|
est_els, els_added, fpr, n_hashes, n_bits = self._parse_footer(
|
|
@@ -516,11 +513,11 @@ class BloomFilter:
|
|
|
516
513
|
|
|
517
514
|
def _load(
|
|
518
515
|
self,
|
|
519
|
-
file:
|
|
520
|
-
hash_function:
|
|
516
|
+
file: Path | str | IOBase | mmap | ByteString,
|
|
517
|
+
hash_function: HashFuncT | None = None,
|
|
521
518
|
) -> None:
|
|
522
519
|
"""load the Bloom Filter from file or bytes"""
|
|
523
|
-
if not isinstance(file,
|
|
520
|
+
if not isinstance(file, IOBase | mmap | bytes | bytearray | memoryview):
|
|
524
521
|
file = resolve_path(file)
|
|
525
522
|
with MMap(file) as filepointer:
|
|
526
523
|
self._load(filepointer, hash_function)
|
|
@@ -597,15 +594,15 @@ class BloomFilterOnDisk(BloomFilter):
|
|
|
597
594
|
|
|
598
595
|
def __init__(
|
|
599
596
|
self,
|
|
600
|
-
filepath:
|
|
601
|
-
est_elements:
|
|
602
|
-
false_positive_rate:
|
|
603
|
-
hex_string:
|
|
604
|
-
hash_function:
|
|
597
|
+
filepath: str | Path,
|
|
598
|
+
est_elements: int | None = None,
|
|
599
|
+
false_positive_rate: float | None = None,
|
|
600
|
+
hex_string: str | None = None,
|
|
601
|
+
hash_function: HashFuncT | None = None,
|
|
605
602
|
) -> None:
|
|
606
603
|
# set some things up
|
|
607
604
|
self._filepath = resolve_path(filepath)
|
|
608
|
-
self.__file_pointer:
|
|
605
|
+
self.__file_pointer: BufferedRandom | None = None
|
|
609
606
|
super().__init__(est_elements, false_positive_rate, filepath, hex_string, hash_function)
|
|
610
607
|
|
|
611
608
|
def _load_init(self, filepath, hash_function, hex_string, est_elements, false_positive_rate):
|
|
@@ -644,7 +641,7 @@ class BloomFilterOnDisk(BloomFilter):
|
|
|
644
641
|
self.__file_pointer.close()
|
|
645
642
|
self.__file_pointer = None
|
|
646
643
|
|
|
647
|
-
def export(self, file:
|
|
644
|
+
def export(self, file: str | Path) -> None: # type: ignore
|
|
648
645
|
"""Export to disk if a different location
|
|
649
646
|
|
|
650
647
|
Args:
|
|
@@ -656,7 +653,7 @@ class BloomFilterOnDisk(BloomFilter):
|
|
|
656
653
|
copyfile(self._filepath, str(file))
|
|
657
654
|
# otherwise, nothing to do!
|
|
658
655
|
|
|
659
|
-
def _load(self, file:
|
|
656
|
+
def _load(self, file: str | Path, hash_function: HashFuncT | None = None): # type: ignore
|
|
660
657
|
"""load the Bloom Filter on disk"""
|
|
661
658
|
# read the file, set the optimal params
|
|
662
659
|
# mmap everything
|
|
@@ -678,7 +675,7 @@ class BloomFilterOnDisk(BloomFilter):
|
|
|
678
675
|
self.__update()
|
|
679
676
|
|
|
680
677
|
@classmethod
|
|
681
|
-
def frombytes(cls, b: ByteString, hash_function:
|
|
678
|
+
def frombytes(cls, b: ByteString, hash_function: HashFuncT | None = None) -> "BloomFilterOnDisk":
|
|
682
679
|
"""
|
|
683
680
|
Raises: NotSupportedError
|
|
684
681
|
"""
|
|
@@ -694,7 +691,7 @@ class BloomFilterOnDisk(BloomFilter):
|
|
|
694
691
|
|
|
695
692
|
def __update(self):
|
|
696
693
|
"""update the on disk Bloom Filter and ensure everything is out to disk"""
|
|
697
|
-
self._bloom.flush()
|
|
698
|
-
self.__file_pointer.seek(-1 * self._UPDATE_OFFSET.size, os.SEEK_END)
|
|
699
|
-
self.__file_pointer.write(self._EXPECTED_ELM_STRUCT.pack(self.elements_added))
|
|
700
|
-
self.__file_pointer.flush()
|
|
694
|
+
self._bloom.flush() # type: ignore
|
|
695
|
+
self.__file_pointer.seek(-1 * self._UPDATE_OFFSET.size, os.SEEK_END) # type: ignore
|
|
696
|
+
self.__file_pointer.write(self._EXPECTED_ELM_STRUCT.pack(self.elements_added)) # type: ignore
|
|
697
|
+
self.__file_pointer.flush() # type: ignore
|
|
@@ -8,11 +8,10 @@ from array import array
|
|
|
8
8
|
from collections.abc import ByteString
|
|
9
9
|
from pathlib import Path
|
|
10
10
|
from struct import Struct
|
|
11
|
-
from typing import Union
|
|
12
11
|
|
|
13
12
|
from probables.blooms.bloom import BloomFilter
|
|
14
13
|
from probables.constants import UINT32_T_MAX, UINT64_T_MAX
|
|
15
|
-
from probables.exceptions import InitializationError
|
|
14
|
+
from probables.exceptions import InitializationError, SimilarityError
|
|
16
15
|
from probables.hashes import HashFuncT, HashResultsT, KeyT
|
|
17
16
|
from probables.utilities import is_hex_string, is_valid_file, resolve_path
|
|
18
17
|
|
|
@@ -48,11 +47,11 @@ class CountingBloomFilter(BloomFilter):
|
|
|
48
47
|
|
|
49
48
|
def __init__(
|
|
50
49
|
self,
|
|
51
|
-
est_elements:
|
|
52
|
-
false_positive_rate:
|
|
53
|
-
filepath:
|
|
54
|
-
hex_string:
|
|
55
|
-
hash_function:
|
|
50
|
+
est_elements: int | None = None,
|
|
51
|
+
false_positive_rate: float | None = None,
|
|
52
|
+
filepath: str | Path | None = None,
|
|
53
|
+
hex_string: str | None = None,
|
|
54
|
+
hash_function: HashFuncT | None = None,
|
|
56
55
|
) -> None:
|
|
57
56
|
"""setup the basic values needed"""
|
|
58
57
|
self._filepath = None
|
|
@@ -81,7 +80,7 @@ class CountingBloomFilter(BloomFilter):
|
|
|
81
80
|
_IMPT_STRUCT = Struct("I")
|
|
82
81
|
|
|
83
82
|
@classmethod
|
|
84
|
-
def frombytes(cls, b: ByteString, hash_function:
|
|
83
|
+
def frombytes(cls, b: ByteString, hash_function: HashFuncT | None = None) -> "CountingBloomFilter":
|
|
85
84
|
"""
|
|
86
85
|
Args:
|
|
87
86
|
b (ByteString): the bytes to load as a Counting Bloom Filter
|
|
@@ -208,7 +207,7 @@ class CountingBloomFilter(BloomFilter):
|
|
|
208
207
|
self.elements_added -= to_remove
|
|
209
208
|
return min_val - to_remove
|
|
210
209
|
|
|
211
|
-
def intersection(self, second: "CountingBloomFilter") ->
|
|
210
|
+
def intersection(self, second: "CountingBloomFilter") -> "CountingBloomFilter": # type: ignore
|
|
212
211
|
"""Take the intersection of two Counting Bloom Filters
|
|
213
212
|
|
|
214
213
|
Args:
|
|
@@ -217,17 +216,16 @@ class CountingBloomFilter(BloomFilter):
|
|
|
217
216
|
CountingBloomFilter: The new Counting Bloom Filter containing the union
|
|
218
217
|
Raises:
|
|
219
218
|
TypeError: When second is not a :class:`CountingBloomFilter`
|
|
219
|
+
SimilarityError: When second is not of the same size (false_positive_rate and est_elements)
|
|
220
220
|
Note:
|
|
221
221
|
The elements_added property will be set to the estimated number of unique elements \
|
|
222
|
-
added as found in estimate_elements()
|
|
223
|
-
Note:
|
|
224
|
-
If `second` is not of the same size (false_positive_rate and est_elements) then \
|
|
225
|
-
this will return `None`"""
|
|
222
|
+
added as found in estimate_elements()"""
|
|
226
223
|
if not _verify_not_type_mismatch(second):
|
|
227
224
|
raise TypeError(MISMATCH_MSG)
|
|
228
225
|
|
|
229
226
|
if self._verify_bloom_similarity(second) is False:
|
|
230
|
-
|
|
227
|
+
raise SimilarityError("Counting Bloom Filters are not similar enough to calculate similarity")
|
|
228
|
+
|
|
231
229
|
res = CountingBloomFilter(
|
|
232
230
|
est_elements=self.estimated_elements,
|
|
233
231
|
false_positive_rate=self.false_positive_rate,
|
|
@@ -237,11 +235,11 @@ class CountingBloomFilter(BloomFilter):
|
|
|
237
235
|
for i in range(self.bloom_length):
|
|
238
236
|
if self._bloom[i] > 0 and second._bloom[i] > 0:
|
|
239
237
|
tmp = self._bloom[i] + second._bloom[i]
|
|
240
|
-
res.bloom[i] = tmp
|
|
238
|
+
res.bloom[i] = min(tmp, UINT32_T_MAX)
|
|
241
239
|
res.elements_added = res.estimate_elements()
|
|
242
240
|
return res
|
|
243
241
|
|
|
244
|
-
def jaccard_index(self, second: "CountingBloomFilter") ->
|
|
242
|
+
def jaccard_index(self, second: "CountingBloomFilter") -> float: # type: ignore
|
|
245
243
|
"""Take the Jaccard Index of two Counting Bloom Filters
|
|
246
244
|
|
|
247
245
|
Args:
|
|
@@ -250,15 +248,14 @@ class CountingBloomFilter(BloomFilter):
|
|
|
250
248
|
float: A numeric value between 0 and 1 where 1 is identical and 0 means completely different
|
|
251
249
|
Raises:
|
|
252
250
|
TypeError: When second is not a :class:`CountingBloomFilter`
|
|
251
|
+
SimilarityError: When second is not of the same size (false_positive_rate and est_elements)
|
|
253
252
|
Note:
|
|
254
|
-
The Jaccard Index is based on the unique set of elements added and not the number of each element added
|
|
255
|
-
Note:
|
|
256
|
-
If `second` is not of the same size (false_positive_rate and est_elements) then this will return `None`"""
|
|
253
|
+
The Jaccard Index is based on the unique set of elements added and not the number of each element added"""
|
|
257
254
|
if not _verify_not_type_mismatch(second):
|
|
258
255
|
raise TypeError(MISMATCH_MSG)
|
|
259
256
|
|
|
260
257
|
if self._verify_bloom_similarity(second) is False:
|
|
261
|
-
|
|
258
|
+
raise SimilarityError("Counting Bloom Filters are not similar enough to calculate similarity")
|
|
262
259
|
|
|
263
260
|
count_union = 0
|
|
264
261
|
count_inter = 0
|
|
@@ -271,7 +268,7 @@ class CountingBloomFilter(BloomFilter):
|
|
|
271
268
|
return 1.0
|
|
272
269
|
return count_inter / count_union
|
|
273
270
|
|
|
274
|
-
def union(self, second: "CountingBloomFilter") ->
|
|
271
|
+
def union(self, second: "CountingBloomFilter") -> "CountingBloomFilter": # type:ignore
|
|
275
272
|
"""Return a new Countiong Bloom Filter that contains the union of
|
|
276
273
|
the two
|
|
277
274
|
|
|
@@ -281,16 +278,16 @@ class CountingBloomFilter(BloomFilter):
|
|
|
281
278
|
CountingBloomFilter: The new Counting Bloom Filter containing the union
|
|
282
279
|
Raises:
|
|
283
280
|
TypeError: When second is not a :class:`CountingBloomFilter`
|
|
281
|
+
SimilarityError: When second is not of the same size (false_positive_rate and est_elements)
|
|
284
282
|
Note:
|
|
285
283
|
The elements_added property will be set to the estimated number of unique elements added as \
|
|
286
|
-
found in estimate_elements()
|
|
287
|
-
Note:
|
|
288
|
-
If `second` is not of the same size (false_positive_rate and est_elements) then this will return `None`"""
|
|
284
|
+
found in estimate_elements()"""
|
|
289
285
|
if not _verify_not_type_mismatch(second):
|
|
290
286
|
raise TypeError(MISMATCH_MSG)
|
|
291
287
|
|
|
292
288
|
if self._verify_bloom_similarity(second) is False:
|
|
293
|
-
|
|
289
|
+
raise SimilarityError("Counting Bloom Filters are not similar enough to calculate similarity")
|
|
290
|
+
|
|
294
291
|
res = CountingBloomFilter(
|
|
295
292
|
est_elements=self.estimated_elements,
|
|
296
293
|
false_positive_rate=self.false_positive_rate,
|
|
@@ -298,7 +295,7 @@ class CountingBloomFilter(BloomFilter):
|
|
|
298
295
|
)
|
|
299
296
|
for i in range(self.bloom_length):
|
|
300
297
|
tmp = self._bloom[i] + second._bloom[i]
|
|
301
|
-
res._bloom[i] = tmp
|
|
298
|
+
res._bloom[i] = min(tmp, UINT32_T_MAX)
|
|
302
299
|
res.elements_added = res.estimate_elements()
|
|
303
300
|
return res
|
|
304
301
|
|
|
@@ -10,7 +10,6 @@ from io import BytesIO, IOBase
|
|
|
10
10
|
from mmap import mmap
|
|
11
11
|
from pathlib import Path
|
|
12
12
|
from struct import Struct
|
|
13
|
-
from typing import Union
|
|
14
13
|
|
|
15
14
|
from probables.blooms.bloom import BloomFilter
|
|
16
15
|
from probables.exceptions import RotatingBloomFilterError
|
|
@@ -46,10 +45,10 @@ class ExpandingBloomFilter:
|
|
|
46
45
|
|
|
47
46
|
def __init__(
|
|
48
47
|
self,
|
|
49
|
-
est_elements:
|
|
50
|
-
false_positive_rate:
|
|
51
|
-
filepath:
|
|
52
|
-
hash_function:
|
|
48
|
+
est_elements: int | None = None,
|
|
49
|
+
false_positive_rate: float | None = None,
|
|
50
|
+
filepath: str | Path | None = None,
|
|
51
|
+
hash_function: HashFuncT | None = None,
|
|
53
52
|
):
|
|
54
53
|
"""initialize"""
|
|
55
54
|
self._blooms = [] # type: ignore
|
|
@@ -74,7 +73,7 @@ class ExpandingBloomFilter:
|
|
|
74
73
|
_BLOOM_ELEMENT_SIZE = Struct("B").size
|
|
75
74
|
|
|
76
75
|
@classmethod
|
|
77
|
-
def frombytes(cls, b: ByteString, hash_function:
|
|
76
|
+
def frombytes(cls, b: ByteString, hash_function: HashFuncT | None = None) -> "ExpandingBloomFilter":
|
|
78
77
|
"""
|
|
79
78
|
Args:
|
|
80
79
|
b (ByteString): The bytes to load as a Expanding Bloom Filter
|
|
@@ -183,12 +182,12 @@ class ExpandingBloomFilter:
|
|
|
183
182
|
if self._blooms[-1].elements_added >= self.__est_elements:
|
|
184
183
|
self.__add_bloom_filter()
|
|
185
184
|
|
|
186
|
-
def export(self, file:
|
|
185
|
+
def export(self, file: Path | str | IOBase | mmap) -> None:
|
|
187
186
|
"""Export an expanding Bloom Filter, or subclass, to disk
|
|
188
187
|
|
|
189
188
|
Args:
|
|
190
189
|
filepath (str): The path to the file to import"""
|
|
191
|
-
if not isinstance(file,
|
|
190
|
+
if not isinstance(file, IOBase | mmap):
|
|
192
191
|
file = resolve_path(file)
|
|
193
192
|
with open(file, "wb") as filepointer:
|
|
194
193
|
self.export(filepointer) # type:ignore
|
|
@@ -207,9 +206,9 @@ class ExpandingBloomFilter:
|
|
|
207
206
|
)
|
|
208
207
|
)
|
|
209
208
|
|
|
210
|
-
def __load(self, file:
|
|
209
|
+
def __load(self, file: Path | str | IOBase | mmap):
|
|
211
210
|
"""load a file"""
|
|
212
|
-
if not isinstance(file,
|
|
211
|
+
if not isinstance(file, IOBase | mmap):
|
|
213
212
|
file = resolve_path(file)
|
|
214
213
|
with MMap(file) as filepointer:
|
|
215
214
|
self.__load(filepointer)
|
|
@@ -272,11 +271,11 @@ class RotatingBloomFilter(ExpandingBloomFilter):
|
|
|
272
271
|
|
|
273
272
|
def __init__(
|
|
274
273
|
self,
|
|
275
|
-
est_elements:
|
|
276
|
-
false_positive_rate:
|
|
274
|
+
est_elements: int | None = None,
|
|
275
|
+
false_positive_rate: float | None = None,
|
|
277
276
|
max_queue_size: int = 10,
|
|
278
|
-
filepath:
|
|
279
|
-
hash_function:
|
|
277
|
+
filepath: str | Path | None = None,
|
|
278
|
+
hash_function: HashFuncT | None = None,
|
|
280
279
|
) -> None:
|
|
281
280
|
"""initialize"""
|
|
282
281
|
super().__init__(
|
|
@@ -289,7 +288,7 @@ class RotatingBloomFilter(ExpandingBloomFilter):
|
|
|
289
288
|
|
|
290
289
|
@classmethod
|
|
291
290
|
def frombytes( # type:ignore
|
|
292
|
-
cls, b: ByteString, max_queue_size: int, hash_function:
|
|
291
|
+
cls, b: ByteString, max_queue_size: int, hash_function: HashFuncT | None = None
|
|
293
292
|
) -> "RotatingBloomFilter":
|
|
294
293
|
"""
|
|
295
294
|
Args:
|