From 3a8bbc41b85711ae509cf716d187484f580dcc9e Mon Sep 17 00:00:00 2001 From: Erin Le Date: Wed, 11 Sep 2024 11:28:55 -0700 Subject: [PATCH] python: refactor base 10 to 2 error message This commit refactors the base 10 to base 2 error message such that it uses the preexisting _split_suffix function instead of a new function based off of _split_suffix. This commit also removes the new helper function used previously. Change-Id: I44d9ac3d8b98bcff33d6bfea7ffbdb5009272ede --- src/python/m5/util/convert.py | 34 ++++------------------------------ 1 file changed, 4 insertions(+), 30 deletions(-) diff --git a/src/python/m5/util/convert.py b/src/python/m5/util/convert.py index 4b7a047904..9349905238 100644 --- a/src/python/m5/util/convert.py +++ b/src/python/m5/util/convert.py @@ -135,32 +135,6 @@ def _split_suffix(value, suffixes): return (value[: -len(matches[0])], matches[0]) if matches else (value, "") -def _split_suffix_by_prefix(value): - """Split a string based on a suffix from a list of suffixes. - This is intended to be used with the keys of binary_prefixes. - - :param value: String value to test for a matching magnitude - (e.g. k, M, Gi, etc). - :param suffixes: Container of suffixes to test. - - :returns: A tuple of (value, suffix). Suffix is the empty string - if there is no match. - - """ - matches = [sfx for sfx in binary_prefixes.keys() if sfx in value] - - if len(matches) == 2: # e.g. matches both M and Mi - matches.sort(key=len, reverse=True) - matches.pop() - assert len(matches) <= 1 - - return ( - (value[: value.find(matches[0])], matches[0]) - if matches - else (value, "") - ) - - def toNum(value, target_type, units, prefixes, converter): """Convert a string using units and prefixes to (typically) a float or integer. @@ -301,15 +275,15 @@ def toMemoryBandwidth(value): return toBinaryFloat(value, "memory bandwidth", "B/s") -def _base_10_to_2(value: str) -> Optional[str]: +def _base_10_to_2(value: str, unit: str) -> Optional[str]: """Convert a base 10 memory/cache size SI prefix strings to base 2. Used in `checkBaseConversion` to provide a warning message to the user. Will return None if no conversion is required. - This function is intentionally separate from `checkBaseConversion` to aid in testing.""" - size, prefix = _split_suffix_by_prefix(value) + size_and_prefix, _ = _split_suffix(value, [unit]) + size, prefix = _split_suffix(size_and_prefix, binary_prefixes) if prefix in base_10_to_2.keys(): return f"{size}{base_10_to_2[prefix]}" return None @@ -317,7 +291,7 @@ def _base_10_to_2(value: str) -> Optional[str]: def checkBaseConversion(value, unit): if type(value) is str: - new_value = _base_10_to_2(value) + new_value = _base_10_to_2(value, unit) if new_value: from m5.util import warn