Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions .github/workflows/ruff.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
name: Ruff
on: [workflow_dispatch, pull_request]
jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- name: Install Python
uses: actions/setup-python@v5
with:
python-version: "3.13"
- uses: astral-sh/ruff-action@v3
with:
args: "check --fix"
continue-on-error: false
12 changes: 2 additions & 10 deletions probables/__init__.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
""" pyprobables module """

from typing import List
"""pyprobables module"""

from probables.blooms import (
BloomFilter,
Expand All @@ -9,13 +7,7 @@
ExpandingBloomFilter,
RotatingBloomFilter,
)
from probables.countminsketch import (
CountMeanMinSketch,
CountMeanSketch,
CountMinSketch,
HeavyHitters,
StreamThreshold,
)
from probables.countminsketch import CountMeanMinSketch, CountMeanSketch, CountMinSketch, HeavyHitters, StreamThreshold
from probables.cuckoo import CountingCuckooFilter, CuckooFilter
from probables.exceptions import (
CuckooFilterFullError,
Expand Down
2 changes: 1 addition & 1 deletion probables/blooms/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
""" Bloom Filters """
"""Bloom Filters"""

from probables.blooms.bloom import BloomFilter, BloomFilterOnDisk
from probables.blooms.countingbloom import CountingBloomFilter
Expand Down
26 changes: 11 additions & 15 deletions probables/blooms/bloom.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
""" BloomFilter and BloomFiter on Disk, python implementation
License: MIT
Author: Tyler Barrus (barrust@gmail.com)
URL: https://github.com/barrust/bloom
"""BloomFilter and BloomFiter on Disk, python implementation
License: MIT
Author: Tyler Barrus (barrust@gmail.com)
URL: https://github.com/barrust/bloom
"""

import math
import os
from array import array
from binascii import hexlify, unhexlify
from collections.abc import ByteString
from io import BytesIO, IOBase
from io import BufferedRandom, BytesIO, IOBase
from mmap import mmap
from numbers import Number
from pathlib import Path
Expand Down Expand Up @@ -308,10 +309,7 @@ def export_c_header(self, filename: Union[str, Path]) -> None:
Args:
filename (str): The filename to which the Bloom Filter will be written."""
data = (" " + line for line in wrap(", ".join(f"0x{e:02x}" for e in bytearray.fromhex(self.export_hex())), 80))
if self._type in ["regular", "regular-on-disk"]:
bloom_type = "standard BloomFilter"
else:
bloom_type = "CountingBloomFilter"
bloom_type = "standard BloomFilter" if self._type in ["regular", "regular-on-disk"] else "CountingBloomFilter"

with open(filename, "w", encoding="utf-8") as file:
print(f"/* BloomFilter Export of a {bloom_type} */", file=file)
Expand Down Expand Up @@ -570,9 +568,7 @@ def _verify_bloom_similarity(self, second: SimpleBloomT) -> bool:
hash_match = self.number_hashes != second.number_hashes
same_bits = self.number_bits != second.number_bits
next_hash = self.hashes("test") != second.hashes("test")
if hash_match or same_bits or next_hash:
return False
return True
return not (hash_match or same_bits or next_hash)


class BloomFilterOnDisk(BloomFilter):
Expand Down Expand Up @@ -609,7 +605,7 @@ def __init__(
) -> None:
# set some things up
self._filepath = resolve_path(filepath)
self.__file_pointer = None
self.__file_pointer: Union[BufferedRandom, None] = None
super().__init__(est_elements, false_positive_rate, filepath, hex_string, hash_function)

def _load_init(self, filepath, hash_function, hex_string, est_elements, false_positive_rate):
Expand Down Expand Up @@ -644,7 +640,7 @@ def close(self) -> None:
"""Clean up the BloomFilterOnDisk object"""
if self.__file_pointer is not None and not self.__file_pointer.closed:
self.__update()
self._bloom.close()
self._bloom.close() # type: ignore
self.__file_pointer.close()
self.__file_pointer = None

Expand Down Expand Up @@ -673,7 +669,7 @@ def _load(self, file: Union[str, Path], hash_function: Union[HashFuncT, None] =
fpr, n_hashes, n_bits = self._get_optimized_params(est_els, fpr)
self._set_values(est_els, fpr, n_hashes, n_bits, hash_function)
# setup a few additional items
self.__file_pointer = open(file, "r+b") # type: ignore
self.__file_pointer = open(file, "r+b") # noqa: SIM115
self._bloom = mmap(self.__file_pointer.fileno(), 0) # type: ignore
self._on_disk = True

Expand Down
9 changes: 5 additions & 4 deletions probables/blooms/countingbloom.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
""" CountingBloomFilter, python implementation
License: MIT
Author: Tyler Barrus (barrust@gmail.com)
URL: https://github.com/barrust/counting_bloom
"""CountingBloomFilter, python implementation
License: MIT
Author: Tyler Barrus (barrust@gmail.com)
URL: https://github.com/barrust/counting_bloom
"""

from array import array
from collections.abc import ByteString
from pathlib import Path
Expand Down
13 changes: 5 additions & 8 deletions probables/blooms/expandingbloom.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
""" Expanding and Rotating BloomFilter, python implementations
License: MIT
Author: Tyler Barrus (barrust@gmail.com)
URL: https://github.com/barrust/pyprobables
"""Expanding and Rotating BloomFilter, python implementations
License: MIT
Author: Tyler Barrus (barrust@gmail.com)
URL: https://github.com/barrust/pyprobables
"""

from array import array
Expand Down Expand Up @@ -145,10 +145,7 @@ def check_alt(self, hashes: HashResultsT) -> bool:
hashes (list): The hash representation to check for in the Bloom Filter
Returns:
bool: `True` if the element is likely present; `False` if definately not present"""
for blm in self._blooms:
if blm.check_alt(hashes):
return True
return False
return any(blm.check_alt(hashes) for blm in self._blooms)

def add(self, key: KeyT, force: bool = False) -> None:
"""Add the key to the Bloom Filter
Expand Down
3 changes: 2 additions & 1 deletion probables/constants.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
""" Project Constants (or basic numerical constants...) """
"""Project Constants (or basic numerical constants...)"""

INT32_T_MIN = -2147483648
INT32_T_MAX = 2147483647
INT64_T_MIN = -9223372036854775808
Expand Down
2 changes: 1 addition & 1 deletion probables/countminsketch/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
""" Count-Min Sketchs """
"""Count-Min Sketchs"""

from probables.countminsketch.countminsketch import (
CountMeanMinSketch,
Expand Down
8 changes: 4 additions & 4 deletions probables/countminsketch/countminsketch.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
""" Count-Min Sketch, Heavy Hitters, and Stream Threshold, python implementations
License: MIT
Author: Tyler Barrus (barrust@gmail.com)
URL: https://github.com/barrust/count-min-sketch
"""Count-Min Sketch, Heavy Hitters, and Stream Threshold, python implementations
License: MIT
Author: Tyler Barrus (barrust@gmail.com)
URL: https://github.com/barrust/count-min-sketch
"""

import math
Expand Down
2 changes: 1 addition & 1 deletion probables/cuckoo/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
""" Cuckoo Filters """
"""Cuckoo Filters"""

from probables.cuckoo.countingcuckoo import CountingCuckooFilter
from probables.cuckoo.cuckoo import CuckooFilter
Expand Down
10 changes: 4 additions & 6 deletions probables/cuckoo/countingcuckoo.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
""" Counting Cuckoo Filter, python implementation
License: MIT
Author: Tyler Barrus (barrust@gmail.com)
"""Counting Cuckoo Filter, python implementation
License: MIT
Author: Tyler Barrus (barrust@gmail.com)
"""

import random
Expand Down Expand Up @@ -136,9 +136,7 @@ def frombytes(

def __contains__(self, val: KeyT) -> bool:
"""setup the `in` keyword"""
if self.check(val) > 0:
return True
return False
return self.check(val) > 0

@property
def unique_elements(self) -> int:
Expand Down
10 changes: 4 additions & 6 deletions probables/cuckoo/cuckoo.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
""" Cuckoo Filter, python implementation
License: MIT
Author: Tyler Barrus (barrust@gmail.com)
"""Cuckoo Filter, python implementation
License: MIT
Author: Tyler Barrus (barrust@gmail.com)
"""

import math
Expand Down Expand Up @@ -313,9 +313,7 @@ def check(self, key: KeyT) -> bool:
bool: True if likely present, False if definately not"""
idx_1, idx_2, fingerprint = self._generate_fingerprint_info(key)
is_present = self._check_if_present(idx_1, idx_2, fingerprint)
if is_present is not None:
return True
return False
return is_present is not None

def remove(self, key: KeyT) -> bool:
"""Remove an element from the filter
Expand Down
2 changes: 1 addition & 1 deletion probables/exceptions.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
""" PyProbables Exceptions """
"""PyProbables Exceptions"""


class ProbablesBaseException(Exception):
Expand Down
2 changes: 1 addition & 1 deletion probables/hashes.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
""" Probables Hashing Utilities """
"""Probables Hashing Utilities"""

from functools import wraps
from hashlib import md5, sha256
Expand Down
3 changes: 1 addition & 2 deletions probables/quotientfilter/__init__.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
""" Quotient Filters """

"""Quotient Filters"""

from probables.quotientfilter.quotientfilter import QuotientFilter

Expand Down
10 changes: 4 additions & 6 deletions probables/quotientfilter/quotientfilter.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
""" Quotient Filter, python implementation
License: MIT
Author: Tyler Barrus (barrust@gmail.com)
"""Quotient Filter, python implementation
License: MIT
Author: Tyler Barrus (barrust@gmail.com)
"""

import sys
Expand Down Expand Up @@ -496,9 +496,7 @@ def _is_run_start(self, elt: int) -> bool:
def _is_run_or_cluster_start(self, elt: int) -> bool:
if self._is_cluster_start(elt):
return True
if self._is_run_start(elt):
return True
return False
return bool(self._is_run_start(elt))

def _is_empty_element(self, elt: int) -> bool:
"""Is this an empty element?"""
Expand Down
4 changes: 2 additions & 2 deletions probables/utilities.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
""" Utility Functions """
"""Utility Functions"""

import math
import mmap
Expand Down Expand Up @@ -41,7 +41,7 @@ class MMap:

def __init__(self, path: Union[Path, str]):
self.__p = Path(path)
self.__f = self.path.open("rb")
self.__f = self.path.open("rb") # noqa: SIM115
self.__m = mmap.mmap(self.__f.fileno(), 0, access=mmap.ACCESS_READ)
self._closed = False

Expand Down
Loading
Loading