From fd0f4b0891ce956b7b3947c6275ca0444e18c77a Mon Sep 17 00:00:00 2001 From: barrust Date: Thu, 28 Dec 2023 21:31:39 -0500 Subject: [PATCH] move to f-string --- probables/blooms/bloom.py | 37 +++++++--------------- probables/blooms/countingbloom.py | 37 +++++++--------------- probables/countminsketch/countminsketch.py | 15 ++++++--- probables/cuckoo/cuckoo.py | 31 ++++++------------ pyproject.toml | 7 +--- scripts/version_bump.py | 9 +++--- 6 files changed, 49 insertions(+), 87 deletions(-) diff --git a/probables/blooms/bloom.py b/probables/blooms/bloom.py index 6021583..edb4658 100644 --- a/probables/blooms/bloom.py +++ b/probables/blooms/bloom.py @@ -115,32 +115,19 @@ def __contains__(self, key: KeyT) -> Union[int, bool]: def __str__(self) -> str: """output statistics of the bloom filter""" on_disk = "no" if self.is_on_disk is False else "yes" - stats = ( + return ( "BloomFilter:\n" - "\tbits: {0}\n" - "\testimated elements: {1}\n" - "\tnumber hashes: {2}\n" - "\tmax false positive rate: {3:.6f}\n" - "\tbloom length (8 bits): {4}\n" - "\telements added: {5}\n" - "\testimated elements added: {6}\n" - "\tcurrent false positive rate: {7:.6f}\n" - "\texport size (bytes): {8}\n" - "\tnumber bits set: {9}\n" - "\tis on disk: {10}\n" - ) - return stats.format( - self.number_bits, - self.estimated_elements, - self.number_hashes, - self.false_positive_rate, - self.bloom_length, - self.elements_added, - self.estimate_elements(), - self.current_false_positive_rate(), - self.export_size(), - self._cnt_number_bits_set(), - on_disk, + f"\tbits: {self.number_bits}\n" + f"\testimated elements: {self.estimated_elements}\n" + f"\tnumber hashes: {self.number_hashes}\n" + f"\tmax false positive rate: {self.false_positive_rate:.6f}\n" + f"\tbloom length (8 bits): {self.bloom_length}\n" + f"\telements added: {self.elements_added}\n" + f"\testimated elements added: {self.estimate_elements()}\n" + f"\tcurrent false positive rate: {self.current_false_positive_rate():.6f}\n" + f"\texport size (bytes): {self.export_size()}\n" + f"\tnumber bits set: {self._cnt_number_bits_set()}\n" + f"\tis on disk: {on_disk}\n" ) def __bytes__(self) -> bytes: diff --git a/probables/blooms/countingbloom.py b/probables/blooms/countingbloom.py index 78401e8..24b9682 100644 --- a/probables/blooms/countingbloom.py +++ b/probables/blooms/countingbloom.py @@ -106,32 +106,19 @@ def __str__(self) -> str: fullness = cnt / self.number_bits els_added = total // self.number_hashes - stats = ( + return ( "CountingBloom:\n" - "\tbits: {0}\n" - "\testimated elements: {1}\n" - "\tnumber hashes: {2}\n" - "\tmax false positive rate: {3:.6f}\n" - "\telements added: {4}\n" - "\tcurrent false positive rate: {5:.6f}\n" - "\tis on disk: {6}\n" - "\tindex fullness: {7:.6}\n" - "\tmax index usage: {8}\n" - "\tmax index id: {9}\n" - "\tcalculated elements: {10}\n" - ) - return stats.format( - self.number_bits, - self.estimated_elements, - self.number_hashes, - self.false_positive_rate, - self.elements_added, - self.current_false_positive_rate(), - on_disk, - fullness, - largest, - largest_idx, - els_added, + f"\tbits: {self.number_bits}\n" + f"\testimated elements: {self.estimated_elements}\n" + f"\tnumber hashes: {self.number_hashes}\n" + f"\tmax false positive rate: {self.false_positive_rate:.6f}\n" + f"\telements added: {self.elements_added}\n" + f"\tcurrent false positive rate: {self.current_false_positive_rate():.6f}\n" + f"\tis on disk: {on_disk}\n" + f"\tindex fullness: {fullness:.6}\n" + f"\tmax index usage: {largest}\n" + f"\tmax index id: {largest_idx}\n" + f"\tcalculated elements: {els_added}\n" ) def add(self, key: KeyT, num_els: int = 1) -> int: # type: ignore diff --git a/probables/countminsketch/countminsketch.py b/probables/countminsketch/countminsketch.py index 4b8300a..5c11a6a 100644 --- a/probables/countminsketch/countminsketch.py +++ b/probables/countminsketch/countminsketch.py @@ -566,7 +566,6 @@ def __init__( filepath: Union[str, Path, None] = None, hash_function: Union[HashFuncT, None] = None, ) -> None: - super().__init__(width, depth, confidence, error_rate, filepath, hash_function) self.__top_x = {} # type: ignore self.__top_x_size = 0 @@ -593,8 +592,11 @@ def frombytes( # type: ignore def __str__(self) -> str: """heavy hitters string rep""" msg = super().__str__() - tmp = "Heavy Hitters {0}\n\tNumber Hitters: {1}\n\tNumber Recorded: {2}" - return tmp.format(msg, self.number_heavy_hitters, self.__top_x_size) + return ( + f"Heavy Hitters {msg}\n" + f"\tNumber Hitters: {self.number_heavy_hitters}\n" + f"\tNumber Recorded: {self.__top_x_size}" + ) @property def heavy_hitters(self) -> Dict[str, int]: @@ -749,8 +751,11 @@ def frombytes( # type: ignore def __str__(self) -> str: """stream threshold string rep""" msg = super().__str__() - tmp = "Stream Threshold {0}\n\tThreshold: {1}\n\tNumber Meeting Threshold: {2}" - return tmp.format(msg, self.threshold, len(self.__meets_threshold)) + return ( + f"Stream Threshold {msg}\n" + f"\tThreshold: {self.threshold}\n" + f"\tNumber Meeting Threshold: {len(self.__meets_threshold)}" + ) @property def meets_threshold(self) -> Dict[str, int]: diff --git a/probables/cuckoo/cuckoo.py b/probables/cuckoo/cuckoo.py index 3600ac8..6e31562 100644 --- a/probables/cuckoo/cuckoo.py +++ b/probables/cuckoo/cuckoo.py @@ -182,25 +182,15 @@ def __contains__(self, key: KeyT) -> bool: def __str__(self): """setup what it will print""" - msg = ( - "{0}:\n" - "\tCapacity: {1}\n" - "\tTotal Bins: {2}\n" - "\tLoad Factor: {3}%\n" - "\tInserted Elements: {4}\n" - "\tMax Swaps: {5}\n" - "\tExpansion Rate: {6}\n" - "\tAuto Expand: {7}" - ) - return msg.format( - self.__class__.__name__, - self.capacity, - self.capacity * self.bucket_size, - self.load_factor() * 100, - self.elements_added, - self.max_swaps, - self.expansion_rate, - self.auto_expand, + return ( + f"{self.__class__.__name__}:\n" + f"\tCapacity: {self.capacity}\n" + f"\tTotal Bins: {self.capacity * self.bucket_size}\n" + f"\tLoad Factor: {self.load_factor() * 100}%\n" + f"\tInserted Elements: {self.elements_added}\n" + f"\tMax Swaps: {self.max_swaps}\n" + f"\tExpansion Rate: {self.expansion_rate}\n" + f"\tAuto Expand: {self.auto_expand}" ) @property @@ -514,8 +504,7 @@ def _generate_fingerprint_info(self, key: KeyT) -> Tuple[int, int, int]: # NOTE: This should never happen... if idx_1 > self.capacity or idx_2 > self.capacity: - msg = "Either idx_1 {0} or idx_2 {1} is greater than {2}" - raise ValueError(msg.format(idx_1, idx_2, self.capacity)) + raise ValueError(f"Either idx_1 {idx_1} or idx_2 {idx_2} is greater than {self.capacity}") return idx_1, idx_2, fingerprint def _deal_with_insertion(self, finger): diff --git a/pyproject.toml b/pyproject.toml index 85475e4..0b9eb79 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -77,15 +77,10 @@ profile = "black" [tool.black] line-length = 120 -target-version = ['py36'] +target-version = ['py38'] include = '\.pyi?$' [build-system] -#requires = ["poetry-core>=1.0.0"] -#build-backend = "poetry.core.masonry.api" - -#requires = ["flit_core>=3.2"] -#build-backend = "flit_core.buildapi" requires = ["setuptools>=61.2.0", "wheel"] build-backend = "setuptools.build_meta" diff --git a/scripts/version_bump.py b/scripts/version_bump.py index f8cf438..dad5d28 100644 --- a/scripts/version_bump.py +++ b/scripts/version_bump.py @@ -10,7 +10,7 @@ def read_and_write(func): def wrapper(**kwargs): path = kwargs["path"] - with open(path, "r") as fobj: + with open(path) as fobj: data = fobj.readlines() func(data, **kwargs) @@ -60,18 +60,17 @@ def _parse_args(): if __name__ == "__main__": - args = _parse_args() # get current path to find where the script is currently script_path = os.path.dirname(os.path.abspath(__file__)) - module_path = os.path.abspath("{}/../".format(script_path)) + module_path = os.path.abspath(f"{script_path}/../") # update the package __init__ file - init_file = "{}/probables/__init__.py".format(module_path) + init_file = f"{module_path}/probables/__init__.py" update_file(path=init_file, k="__version__", v=args.new_version) # update the citation file - citation_file = "{}/CITATION.cff".format(module_path) + citation_file = f"{module_path}/CITATION.cff" update_citation_file(path=citation_file, v=args.new_version)