Skip to content

Commit

Permalink
move to f-string
Browse files Browse the repository at this point in the history
  • Loading branch information
barrust committed Dec 29, 2023
1 parent 685cde7 commit fd0f4b0
Show file tree
Hide file tree
Showing 6 changed files with 49 additions and 87 deletions.
37 changes: 12 additions & 25 deletions probables/blooms/bloom.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
37 changes: 12 additions & 25 deletions probables/blooms/countingbloom.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
15 changes: 10 additions & 5 deletions probables/countminsketch/countminsketch.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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]:
Expand Down Expand Up @@ -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]:
Expand Down
31 changes: 10 additions & 21 deletions probables/cuckoo/cuckoo.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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):
Expand Down
7 changes: 1 addition & 6 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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"
9 changes: 4 additions & 5 deletions scripts/version_bump.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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)

0 comments on commit fd0f4b0

Please sign in to comment.