Skip to content

Commit e5d44da

Browse files
authored
Ruff cleanup (#133)
1 parent b9e8833 commit e5d44da

27 files changed

Lines changed: 191 additions & 205 deletions

.github/workflows/ruff.yml

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
name: Ruff
2+
on: [workflow_dispatch, pull_request]
3+
jobs:
4+
build:
5+
runs-on: ubuntu-latest
6+
steps:
7+
- uses: actions/checkout@v4
8+
- name: Install Python
9+
uses: actions/setup-python@v5
10+
with:
11+
python-version: "3.13"
12+
- uses: astral-sh/ruff-action@v3
13+
with:
14+
args: "check --fix"
15+
continue-on-error: false

probables/__init__.py

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
1-
""" pyprobables module """
2-
3-
from typing import List
1+
"""pyprobables module"""
42

53
from probables.blooms import (
64
BloomFilter,
@@ -9,13 +7,7 @@
97
ExpandingBloomFilter,
108
RotatingBloomFilter,
119
)
12-
from probables.countminsketch import (
13-
CountMeanMinSketch,
14-
CountMeanSketch,
15-
CountMinSketch,
16-
HeavyHitters,
17-
StreamThreshold,
18-
)
10+
from probables.countminsketch import CountMeanMinSketch, CountMeanSketch, CountMinSketch, HeavyHitters, StreamThreshold
1911
from probables.cuckoo import CountingCuckooFilter, CuckooFilter
2012
from probables.exceptions import (
2113
CuckooFilterFullError,

probables/blooms/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
""" Bloom Filters """
1+
"""Bloom Filters"""
22

33
from probables.blooms.bloom import BloomFilter, BloomFilterOnDisk
44
from probables.blooms.countingbloom import CountingBloomFilter

probables/blooms/bloom.py

Lines changed: 11 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,15 @@
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
55
"""
6+
67
import math
78
import os
89
from array import array
910
from binascii import hexlify, unhexlify
1011
from collections.abc import ByteString
11-
from io import BytesIO, IOBase
12+
from io import BufferedRandom, BytesIO, IOBase
1213
from mmap import mmap
1314
from numbers import Number
1415
from pathlib import Path
@@ -308,10 +309,7 @@ def export_c_header(self, filename: Union[str, Path]) -> None:
308309
Args:
309310
filename (str): The filename to which the Bloom Filter will be written."""
310311
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"
315313

316314
with open(filename, "w", encoding="utf-8") as file:
317315
print(f"/* BloomFilter Export of a {bloom_type} */", file=file)
@@ -570,9 +568,7 @@ def _verify_bloom_similarity(self, second: SimpleBloomT) -> bool:
570568
hash_match = self.number_hashes != second.number_hashes
571569
same_bits = self.number_bits != second.number_bits
572570
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)
576572

577573

578574
class BloomFilterOnDisk(BloomFilter):
@@ -609,7 +605,7 @@ def __init__(
609605
) -> None:
610606
# set some things up
611607
self._filepath = resolve_path(filepath)
612-
self.__file_pointer = None
608+
self.__file_pointer: Union[BufferedRandom, None] = None
613609
super().__init__(est_elements, false_positive_rate, filepath, hex_string, hash_function)
614610

615611
def _load_init(self, filepath, hash_function, hex_string, est_elements, false_positive_rate):
@@ -644,7 +640,7 @@ def close(self) -> None:
644640
"""Clean up the BloomFilterOnDisk object"""
645641
if self.__file_pointer is not None and not self.__file_pointer.closed:
646642
self.__update()
647-
self._bloom.close()
643+
self._bloom.close() # type: ignore
648644
self.__file_pointer.close()
649645
self.__file_pointer = None
650646

@@ -673,7 +669,7 @@ def _load(self, file: Union[str, Path], hash_function: Union[HashFuncT, None] =
673669
fpr, n_hashes, n_bits = self._get_optimized_params(est_els, fpr)
674670
self._set_values(est_els, fpr, n_hashes, n_bits, hash_function)
675671
# 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
677673
self._bloom = mmap(self.__file_pointer.fileno(), 0) # type: ignore
678674
self._on_disk = True
679675

probables/blooms/countingbloom.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
1-
""" CountingBloomFilter, python implementation
2-
License: MIT
3-
Author: Tyler Barrus (barrust@gmail.com)
4-
URL: https://github.com/barrust/counting_bloom
1+
"""CountingBloomFilter, python implementation
2+
License: MIT
3+
Author: Tyler Barrus (barrust@gmail.com)
4+
URL: https://github.com/barrust/counting_bloom
55
"""
6+
67
from array import array
78
from collections.abc import ByteString
89
from pathlib import Path

probables/blooms/expandingbloom.py

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
""" Expanding and Rotating BloomFilter, python implementations
2-
License: MIT
3-
Author: Tyler Barrus (barrust@gmail.com)
4-
URL: https://github.com/barrust/pyprobables
1+
"""Expanding and Rotating BloomFilter, python implementations
2+
License: MIT
3+
Author: Tyler Barrus (barrust@gmail.com)
4+
URL: https://github.com/barrust/pyprobables
55
"""
66

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

153150
def add(self, key: KeyT, force: bool = False) -> None:
154151
"""Add the key to the Bloom Filter

probables/constants.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
""" Project Constants (or basic numerical constants...) """
1+
"""Project Constants (or basic numerical constants...)"""
2+
23
INT32_T_MIN = -2147483648
34
INT32_T_MAX = 2147483647
45
INT64_T_MIN = -9223372036854775808

probables/countminsketch/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
""" Count-Min Sketchs """
1+
"""Count-Min Sketchs"""
22

33
from probables.countminsketch.countminsketch import (
44
CountMeanMinSketch,

probables/countminsketch/countminsketch.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
""" Count-Min Sketch, Heavy Hitters, and Stream Threshold, python implementations
2-
License: MIT
3-
Author: Tyler Barrus (barrust@gmail.com)
4-
URL: https://github.com/barrust/count-min-sketch
1+
"""Count-Min Sketch, Heavy Hitters, and Stream Threshold, python implementations
2+
License: MIT
3+
Author: Tyler Barrus (barrust@gmail.com)
4+
URL: https://github.com/barrust/count-min-sketch
55
"""
66

77
import math

probables/cuckoo/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
""" Cuckoo Filters """
1+
"""Cuckoo Filters"""
22

33
from probables.cuckoo.countingcuckoo import CountingCuckooFilter
44
from probables.cuckoo.cuckoo import CuckooFilter

0 commit comments

Comments
 (0)