Skip to content

Commit 79e9948

Browse files
committed
ruff recommendations
1 parent b9e8833 commit 79e9948

13 files changed

Lines changed: 125 additions & 144 deletions

probables/blooms/bloom.py

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -308,10 +308,7 @@ def export_c_header(self, filename: Union[str, Path]) -> None:
308308
Args:
309309
filename (str): The filename to which the Bloom Filter will be written."""
310310
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"
311+
bloom_type = "standard BloomFilter" if self._type in ["regular", "regular-on-disk"] else "CountingBloomFilter"
315312

316313
with open(filename, "w", encoding="utf-8") as file:
317314
print(f"/* BloomFilter Export of a {bloom_type} */", file=file)
@@ -570,9 +567,7 @@ def _verify_bloom_similarity(self, second: SimpleBloomT) -> bool:
570567
hash_match = self.number_hashes != second.number_hashes
571568
same_bits = self.number_bits != second.number_bits
572569
next_hash = self.hashes("test") != second.hashes("test")
573-
if hash_match or same_bits or next_hash:
574-
return False
575-
return True
570+
return not (hash_match or same_bits or next_hash)
576571

577572

578573
class BloomFilterOnDisk(BloomFilter):
@@ -673,7 +668,7 @@ def _load(self, file: Union[str, Path], hash_function: Union[HashFuncT, None] =
673668
fpr, n_hashes, n_bits = self._get_optimized_params(est_els, fpr)
674669
self._set_values(est_els, fpr, n_hashes, n_bits, hash_function)
675670
# setup a few additional items
676-
self.__file_pointer = open(file, "r+b") # type: ignore
671+
self.__file_pointer = open(file, "r+b") # type: ignore # noqa: SIM115
677672
self._bloom = mmap(self.__file_pointer.fileno(), 0) # type: ignore
678673
self._on_disk = True
679674

probables/cuckoo/countingcuckoo.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -136,9 +136,7 @@ def frombytes(
136136

137137
def __contains__(self, val: KeyT) -> bool:
138138
"""setup the `in` keyword"""
139-
if self.check(val) > 0:
140-
return True
141-
return False
139+
return self.check(val) > 0
142140

143141
@property
144142
def unique_elements(self) -> int:

probables/cuckoo/cuckoo.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -313,9 +313,7 @@ def check(self, key: KeyT) -> bool:
313313
bool: True if likely present, False if definately not"""
314314
idx_1, idx_2, fingerprint = self._generate_fingerprint_info(key)
315315
is_present = self._check_if_present(idx_1, idx_2, fingerprint)
316-
if is_present is not None:
317-
return True
318-
return False
316+
return is_present is not None
319317

320318
def remove(self, key: KeyT) -> bool:
321319
"""Remove an element from the filter

tests/bloom_test.py

Lines changed: 30 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
#!/usr/bin/env python3
2-
# -*- coding: utf-8 -*-
32
""" Unittest class """
43

54
import hashlib
@@ -13,11 +12,11 @@
1312
sys.path.insert(0, str(this_dir))
1413
sys.path.insert(0, str(this_dir.parent))
1514

16-
from probables import BloomFilter, BloomFilterOnDisk
17-
from probables.constants import UINT64_T_MAX
18-
from probables.exceptions import InitializationError, NotSupportedError
19-
from probables.hashes import hash_with_depth_int
20-
from tests.utilities import calc_file_md5, different_hash
15+
from probables import BloomFilter, BloomFilterOnDisk # noqa: E402
16+
from probables.constants import UINT64_T_MAX # noqa: E402
17+
from probables.exceptions import InitializationError, NotSupportedError # noqa: E402
18+
from probables.hashes import hash_with_depth_int # noqa: E402
19+
from tests.utilities import calc_file_md5, different_hash # noqa: E402
2120

2221
DELETE_TEMP_FILES = True
2322

@@ -252,7 +251,7 @@ def test_bf_stats(self):
252251
)
253252
blm = BloomFilter(est_elements=10, false_positive_rate=0.05)
254253
for i in range(0, 10):
255-
tmp = "this is a test {0}".format(i)
254+
tmp = f"this is a test {i}"
256255
blm.add(tmp)
257256
stats = str(blm)
258257
self.assertEqual(stats, msg)
@@ -262,7 +261,7 @@ def test_bf_export_hex(self):
262261
hex_val = "6da491461a6bba4d000000000000000a000000000000000a3d4ccccd"
263262
blm = BloomFilter(est_elements=10, false_positive_rate=0.05)
264263
for i in range(0, 10):
265-
tmp = "this is a test {0}".format(i)
264+
tmp = f"this is a test {i}"
266265
blm.add(tmp)
267266
hex_out = blm.export_hex()
268267

@@ -295,23 +294,23 @@ def test_bf_export_c_header(self):
295294
hex_val = "6da491461a6bba4d000000000000000a000000000000000a3d4ccccd"
296295
blm = BloomFilter(est_elements=10, false_positive_rate=0.05)
297296
for i in range(0, 10):
298-
tmp = "this is a test {0}".format(i)
297+
tmp = f"this is a test {i}"
299298
blm.add(tmp)
300299
with NamedTemporaryFile(dir=os.getcwd(), suffix=".blm", delete=DELETE_TEMP_FILES) as fobj:
301300
blm.export_c_header(fobj.name)
302301

303302
# now load the file, parse it and do some tests!
304-
with open(fobj.name, "r") as fobj:
303+
with open(fobj.name) as fobj:
305304
data = fobj.readlines()
306305
data = [x.strip() for x in data]
307306

308307
self.assertEqual("/* BloomFilter Export of a standard BloomFilter */", data[0])
309308
self.assertEqual("#include <inttypes.h>", data[1])
310-
self.assertEqual("const uint64_t estimated_elements = {};".format(blm.estimated_elements), data[2])
311-
self.assertEqual("const uint64_t elements_added = {};".format(blm.elements_added), data[3])
312-
self.assertEqual("const float false_positive_rate = {};".format(blm.false_positive_rate), data[4])
313-
self.assertEqual("const uint64_t number_bits = {};".format(blm.number_bits), data[5])
314-
self.assertEqual("const unsigned int number_hashes = {};".format(blm.number_hashes), data[6])
309+
self.assertEqual(f"const uint64_t estimated_elements = {blm.estimated_elements};", data[2])
310+
self.assertEqual(f"const uint64_t elements_added = {blm.elements_added};", data[3])
311+
self.assertEqual(f"const float false_positive_rate = {blm.false_positive_rate};", data[4])
312+
self.assertEqual(f"const uint64_t number_bits = {blm.number_bits};", data[5])
313+
self.assertEqual(f"const unsigned int number_hashes = {blm.number_hashes};", data[6])
315314
self.assertEqual("const unsigned char bloom[] = {", data[7])
316315
self.assertEqual("};", data[-1])
317316

@@ -481,7 +480,7 @@ def test_bf_clear(self):
481480
blm = BloomFilter(est_elements=10, false_positive_rate=0.05)
482481
self.assertEqual(blm.elements_added, 0)
483482
for i in range(0, 10):
484-
tmp = "this is a test {0}".format(i)
483+
tmp = f"this is a test {i}"
485484
blm.add(tmp)
486485
self.assertEqual(blm.elements_added, 10)
487486

@@ -518,13 +517,13 @@ def my_hash(key, depth=1, encoding="utf-8"):
518517
self.assertNotEqual(md5_out, md5_val)
519518

520519
for i in range(0, 10):
521-
tmp = "this is a test {0}".format(i)
520+
tmp = f"this is a test {i}"
522521
blm.add(tmp)
523522

524523
self.assertEqual(blm.elements_added, 11)
525524

526525
for i in range(0, 10):
527-
tmp = "this is a test {0}".format(i)
526+
tmp = f"this is a test {i}"
528527
self.assertTrue(blm.check(tmp))
529528

530529
self.assertEqual(blm.hashes("this is a test", 5), results)
@@ -563,13 +562,13 @@ def my_hash(key, depth, encoding="utf-8"):
563562
self.assertNotEqual(md5_out, md5_val)
564563

565564
for i in range(0, 10):
566-
tmp = "this is a test {0}".format(i)
565+
tmp = f"this is a test {i}"
567566
blm.add(tmp)
568567

569568
self.assertEqual(blm.elements_added, 11)
570569

571570
for i in range(0, 10):
572-
tmp = "this is a test {0}".format(i)
571+
tmp = f"this is a test {i}"
573572
self.assertTrue(blm.check(tmp))
574573

575574
self.assertEqual(blm.hashes("this is a test", 5), results)
@@ -714,7 +713,7 @@ def test_bfod_close_del(self):
714713
blm.add("this is a test")
715714
del blm
716715
try:
717-
self.assertEqual(True, blm)
716+
self.assertEqual(True, blm) # noqa: F821
718717
except UnboundLocalError as ex:
719718
msg1 = "local variable 'blm' referenced before assignment"
720719
msg2 = "cannot access local variable 'blm' where it is not associated with a value"
@@ -731,7 +730,7 @@ def test_bfod_close_del(self):
731730
# export to new file
732731
def test_bfod_export(self):
733732
"""export to on disk to new file"""
734-
with NamedTemporaryFile(dir=os.getcwd(), suffix=".blm", delete=DELETE_TEMP_FILES) as fobj:
733+
with NamedTemporaryFile(dir=os.getcwd(), suffix=".blm", delete=DELETE_TEMP_FILES) as fobj: # noqa: SIM117
735734
with NamedTemporaryFile(dir=os.getcwd(), suffix=".blm", delete=DELETE_TEMP_FILES) as fobj1:
736735
blm = BloomFilterOnDisk(fobj.name, 10, 0.05)
737736
blm.add("this is a test")
@@ -781,7 +780,7 @@ def test_bfod_export_hex(self):
781780
with NamedTemporaryFile(dir=os.getcwd(), suffix=".blm", delete=DELETE_TEMP_FILES) as fobj:
782781
blm = BloomFilterOnDisk(fobj.name, est_elements=10, false_positive_rate=0.05)
783782
for i in range(0, 10):
784-
tmp = "this is a test {0}".format(i)
783+
tmp = f"this is a test {i}"
785784
blm.add(tmp)
786785
hex_out = blm.export_hex()
787786
self.assertEqual(hex_out, hex_val)
@@ -813,24 +812,24 @@ def test_bfod_export_c_header(self):
813812
with NamedTemporaryFile(dir=os.getcwd(), suffix=".blm", delete=DELETE_TEMP_FILES) as fobj:
814813
blm = BloomFilterOnDisk(fobj.name, est_elements=10, false_positive_rate=0.05)
815814
for i in range(0, 10):
816-
tmp = "this is a test {0}".format(i)
815+
tmp = f"this is a test {i}"
817816
blm.add(tmp)
818817
with NamedTemporaryFile(dir=os.getcwd(), suffix=".blm", delete=DELETE_TEMP_FILES) as fobj:
819818
blm.export_c_header(fobj.name)
820819

821820
# now load the file, parse it and do some tests!
822-
with open(fobj.name, "r") as fobj:
821+
with open(fobj.name) as fobj:
823822
data = fobj.readlines()
824823

825824
data = [x.strip() for x in data]
826825

827826
self.assertEqual("/* BloomFilter Export of a standard BloomFilter */", data[0])
828827
self.assertEqual("#include <inttypes.h>", data[1])
829-
self.assertEqual("const uint64_t estimated_elements = {};".format(blm.estimated_elements), data[2])
830-
self.assertEqual("const uint64_t elements_added = {};".format(blm.elements_added), data[3])
831-
self.assertEqual("const float false_positive_rate = {};".format(blm.false_positive_rate), data[4])
832-
self.assertEqual("const uint64_t number_bits = {};".format(blm.number_bits), data[5])
833-
self.assertEqual("const unsigned int number_hashes = {};".format(blm.number_hashes), data[6])
828+
self.assertEqual(f"const uint64_t estimated_elements = {blm.estimated_elements};", data[2])
829+
self.assertEqual(f"const uint64_t elements_added = {blm.elements_added};", data[3])
830+
self.assertEqual(f"const float false_positive_rate = {blm.false_positive_rate};", data[4])
831+
self.assertEqual(f"const uint64_t number_bits = {blm.number_bits};", data[5])
832+
self.assertEqual(f"const unsigned int number_hashes = {blm.number_hashes};", data[6])
834833
self.assertEqual("const unsigned char bloom[] = {", data[7])
835834
self.assertEqual("};", data[-1])
836835

@@ -844,7 +843,7 @@ def test_bfod_clear(self):
844843
blm = BloomFilterOnDisk(filepath=fobj.name, est_elements=10, false_positive_rate=0.05)
845844
self.assertEqual(blm.elements_added, 0)
846845
for i in range(0, 10):
847-
tmp = "this is a test {0}".format(i)
846+
tmp = f"this is a test {i}"
848847
blm.add(tmp)
849848
self.assertEqual(blm.elements_added, 10)
850849

tests/countingbloom_test.py

Lines changed: 14 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
#!/usr/bin/env python3
2-
# -*- coding: utf-8 -*-
32
""" Unittest class """
43

54
import hashlib
@@ -13,9 +12,9 @@
1312
sys.path.insert(0, str(this_dir))
1413
sys.path.insert(0, str(this_dir.parent))
1514

16-
from probables import CountingBloomFilter
17-
from probables.exceptions import InitializationError
18-
from tests.utilities import calc_file_md5, different_hash
15+
from probables import CountingBloomFilter # noqa: E402
16+
from probables.exceptions import InitializationError # noqa: E402
17+
from tests.utilities import calc_file_md5, different_hash # noqa: E402
1918

2019
DELETE_TEMP_FILES = True
2120

@@ -87,7 +86,7 @@ def test_cbf_stats(self):
8786
)
8887
blm = CountingBloomFilter(est_elements=10, false_positive_rate=0.05)
8988
for i in range(0, 10):
90-
blm.add("this is a test {0}".format(i))
89+
blm.add(f"this is a test {i}")
9190
stats = str(blm)
9291
self.assertEqual(stats, msg)
9392

@@ -96,7 +95,7 @@ def test_cbf_clear(self):
9695
blm = CountingBloomFilter(est_elements=10, false_positive_rate=0.05)
9796
self.assertEqual(blm.elements_added, 0)
9897
for i in range(0, 10):
99-
blm.add("this is a test {0}".format(i))
98+
blm.add(f"this is a test {i}")
10099
self.assertEqual(blm.elements_added, 10)
101100

102101
blm.clear()
@@ -215,7 +214,7 @@ def test_cbf_export_hex(self):
215214

216215
blm = CountingBloomFilter(est_elements=10, false_positive_rate=0.05)
217216
for i in range(0, 10):
218-
tmp = "this is a test {0}".format(i)
217+
tmp = f"this is a test {i}"
219218
blm.add(tmp)
220219
hex_out = blm.export_hex()
221220

@@ -277,25 +276,25 @@ def test_cbf_export_c_header(self):
277276
)
278277
blm = CountingBloomFilter(est_elements=10, false_positive_rate=0.05)
279278
for i in range(0, 10):
280-
tmp = "this is a test {0}".format(i)
279+
tmp = f"this is a test {i}"
281280
blm.add(tmp)
282281

283282
with NamedTemporaryFile(dir=os.getcwd(), suffix=".blm", delete=DELETE_TEMP_FILES) as fobj:
284283
blm.export_c_header(fobj.name)
285284

286285
# now load the file, parse it and do some tests!
287-
with open(fobj.name, "r") as fobj:
286+
with open(fobj.name) as fobj:
288287
data = fobj.readlines()
289288

290289
data = [x.strip() for x in data]
291290

292291
self.assertEqual("/* BloomFilter Export of a CountingBloomFilter */", data[0])
293292
self.assertEqual("#include <inttypes.h>", data[1])
294-
self.assertEqual("const uint64_t estimated_elements = {};".format(blm.estimated_elements), data[2])
295-
self.assertEqual("const uint64_t elements_added = {};".format(blm.elements_added), data[3])
296-
self.assertEqual("const float false_positive_rate = {};".format(blm.false_positive_rate), data[4])
297-
self.assertEqual("const uint64_t number_bits = {};".format(blm.number_bits), data[5])
298-
self.assertEqual("const unsigned int number_hashes = {};".format(blm.number_hashes), data[6])
293+
self.assertEqual(f"const uint64_t estimated_elements = {blm.estimated_elements};", data[2])
294+
self.assertEqual(f"const uint64_t elements_added = {blm.elements_added};", data[3])
295+
self.assertEqual(f"const float false_positive_rate = {blm.false_positive_rate};", data[4])
296+
self.assertEqual(f"const uint64_t number_bits = {blm.number_bits};", data[5])
297+
self.assertEqual(f"const unsigned int number_hashes = {blm.number_hashes};", data[6])
299298
self.assertEqual("const unsigned char bloom[] = {", data[7])
300299
self.assertEqual("};", data[-1])
301300

@@ -410,7 +409,7 @@ def test_cbf_remove(self):
410409
blm = CountingBloomFilter(est_elements=10, false_positive_rate=0.05)
411410
self.assertEqual(blm.elements_added, 0)
412411
for i in range(0, 5):
413-
tmp = "this is a test {0}".format(i)
412+
tmp = f"this is a test {i}"
414413
blm.add(tmp)
415414
self.assertEqual(blm.elements_added, 5)
416415
res = blm.remove("this is a test 0")

tests/countingcuckoo_test.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
#!/usr/bin/env python3
2-
# -*- coding: utf-8 -*-
32
""" Unittest class """
43

54
import hashlib
@@ -13,8 +12,8 @@
1312
sys.path.insert(0, str(this_dir))
1413
sys.path.insert(0, str(this_dir.parent))
1514

16-
from probables import CountingCuckooFilter, CuckooFilterFullError
17-
from tests.utilities import calc_file_md5
15+
from probables import CountingCuckooFilter, CuckooFilterFullError # noqa: E402
16+
from tests.utilities import calc_file_md5 # noqa: E402
1817

1918
DELETE_TEMP_FILES = True
2019

tests/countminsketch_test.py

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
#!/usr/bin/env python3
2-
# -*- coding: utf-8 -*-
32
""" Unittest class """
43

54
import hashlib
@@ -13,10 +12,10 @@
1312
sys.path.insert(0, str(this_dir))
1413
sys.path.insert(0, str(this_dir.parent))
1514

16-
from probables import CountMeanMinSketch, CountMeanSketch, CountMinSketch, HeavyHitters, StreamThreshold
17-
from probables.constants import INT32_T_MAX, INT32_T_MIN, INT64_T_MAX, INT64_T_MIN
18-
from probables.exceptions import CountMinSketchError, InitializationError, NotSupportedError
19-
from tests.utilities import calc_file_md5, different_hash
15+
from probables import CountMeanMinSketch, CountMeanSketch, CountMinSketch, HeavyHitters, StreamThreshold # noqa: E402
16+
from probables.constants import INT32_T_MAX, INT32_T_MIN, INT64_T_MAX, INT64_T_MIN # noqa: E402
17+
from probables.exceptions import CountMinSketchError, InitializationError, NotSupportedError # noqa: E402
18+
from tests.utilities import calc_file_md5, different_hash # noqa: E402
2019

2120
DELETE_TEMP_FILES = True
2221

tests/cuckoo_test.py

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
#!/usr/bin/env python3
2-
# -*- coding: utf-8 -*-
32
""" Unittest class """
43

54
import hashlib
@@ -13,8 +12,8 @@
1312
sys.path.insert(0, str(this_dir))
1413
sys.path.insert(0, str(this_dir.parent))
1514

16-
from probables import CuckooFilter, CuckooFilterFullError, InitializationError
17-
from tests.utilities import calc_file_md5
15+
from probables import CuckooFilter, CuckooFilterFullError, InitializationError # noqa: E402
16+
from tests.utilities import calc_file_md5 # noqa: E402
1817

1918
DELETE_TEMP_FILES = True
2019

@@ -77,10 +76,10 @@ def my_hash(key):
7776
hash_function=my_hash,
7877
)
7978
for i in range(50):
80-
cko.add("this is a test - {}".format(i))
79+
cko.add(f"this is a test - {i}")
8180

8281
for i in range(50):
83-
self.assertTrue("this is a test - {}".format(i) in cko)
82+
self.assertTrue(f"this is a test - {i}" in cko)
8483

8584
def test_cuckoo_filter_remove(self):
8685
"""test removing from the cuckoo filter"""

0 commit comments

Comments
 (0)