11#!/usr/bin/env python3
2- # -*- coding: utf-8 -*-
32""" Unittest class """
43
54import hashlib
1312sys .path .insert (0 , str (this_dir ))
1413sys .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
2221DELETE_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
0 commit comments