Andreas Sandberg has uploaded this change for review. (
https://gem5-review.googlesource.com/c/public/gem5/+/39217 )
Change subject: python: Refactor toNum to support a selection of units
......................................................................
python: Refactor toNum to support a selection of units
Add support for matching one of several different units in toNum. The
units parameter can now either be a tuple or a string describing the
supported unit(s). The function now returns a (magnitude, unit) tuple.
Change-Id: I683819722a93ade91a6def2bfa77209c29b4b39e
Signed-off-by: Andreas Sandberg <andreas.sandb...@arm.com>
---
M src/python/m5/util/convert.py
1 file changed, 36 insertions(+), 12 deletions(-)
diff --git a/src/python/m5/util/convert.py b/src/python/m5/util/convert.py
index d3088f6..772fba2 100644
--- a/src/python/m5/util/convert.py
+++ b/src/python/m5/util/convert.py
@@ -1,3 +1,15 @@
+# Copyright (c) 2021 Arm Limited
+# All rights reserved.
+#
+# The license below extends only to copyright in the software and shall
+# not be construed as granting a license to any other intellectual
+# property including but not limited to intellectual property relating
+# to a hardware implementation of the functionality of the software
+# licensed hereunder. You may use the software subject to the license
+# terms below provided that you ensure that this notice is replicated
+# unmodified and in its entirety in all distributions of the software,
+# modified or unmodified, in source code or in binary form.
+#
# Copyright (c) 2005 The Regents of The University of Michigan
# Copyright (c) 2010 Advanced Micro Devices, Inc.
# All rights reserved.
@@ -92,34 +104,46 @@
if not isinstance(value, str):
raise TypeError("wrong type '%s' should be str" % type(value))
+def _find_suffix(value, suffixes, default=''):
+ matches = [ sfx for sfx in suffixes if value.endswith(sfx) ]
+ assert len(matches) <= 1
+
+ return matches[0] if matches else default
# memory size configuration stuff
def toNum(value, target_type, units, prefixes, converter):
assertStr(value)
def convert(val):
+ return converter(val)
try:
return converter(val)
except ValueError:
raise ValueError(
"cannot convert '%s' to %s" % (value, target_type))
- if units and not value.endswith(units):
- units = None
+ # Units can be None, the empty string, or an a list/tuple. Convert
+ # to a tuple for consistent handling.
if not units:
- return convert(value)
+ units = tuple()
+ elif isinstance(units, str):
+ units = (units,)
+ else:
+ units = tuple(units)
- value = value[:-len(units)]
+ unit = _find_suffix(value, units)
- prefix = next((p for p in prefixes.keys() if value.endswith(p)), None)
- if not prefix:
- return convert(value)
- value = value[:-len(prefix)]
+ # We only allow a prefix if there is a unit
+ if unit:
+ prefix = _find_suffix(value[:-len(unit)], prefixes)
+ scale = prefixes[prefix] if prefix else 1
+ else:
+ prefix, scale = '', 1
- return convert(value) * prefixes[prefix]
+ return convert(value[:len(value) - len(unit) - len(prefix)]) * scale,
unit
def toFloat(value, target_type='float', units=None, prefixes=[]):
- return toNum(value, target_type, units, prefixes, float)
+ return toNum(value, target_type, units, prefixes, float)[0]
def toMetricFloat(value, target_type='float', units=None):
return toFloat(value, target_type, units, metric_prefixes)
@@ -128,8 +152,8 @@
return toFloat(value, target_type, units, binary_prefixes)
def toInteger(value, target_type='integer', units=None, prefixes=[]):
- intifier = lambda x: int(x, 0)
- return toNum(value, target_type, units, prefixes, intifier)
+ return toNum(value, target_type, units, prefixes,
+ lambda x: int(x, 0))[0]
def toMetricInteger(value, target_type='integer', units=None):
return toInteger(value, target_type, units, metric_prefixes)
--
To view, visit https://gem5-review.googlesource.com/c/public/gem5/+/39217
To unsubscribe, or for help writing mail filters, visit
https://gem5-review.googlesource.com/settings
Gerrit-Project: public/gem5
Gerrit-Branch: develop
Gerrit-Change-Id: I683819722a93ade91a6def2bfa77209c29b4b39e
Gerrit-Change-Number: 39217
Gerrit-PatchSet: 1
Gerrit-Owner: Andreas Sandberg <andreas.sandb...@arm.com>
Gerrit-MessageType: newchange
_______________________________________________
gem5-dev mailing list -- gem5-dev@gem5.org
To unsubscribe send an email to gem5-dev-le...@gem5.org
%(web_page_url)slistinfo%(cgiext)s/%(_internal_name)s