On Tue, Feb 26, 2013 at 3:58 AM, Yiqiao Pu <[email protected]> wrote:
> Convert values based on different values based on one specified
> unit.
This looks like a slightly more flexible version of the function
autotest.client.shared.utils.convert_data_size().
def convert_data_size(size, default_sufix='B'):
'''
Convert data size from human readable units to an int of arbitrary size.
@param size: Human readable data size representation (string).
@param default_sufix: Default sufix used to represent data.
@return: Int with data size in the appropriate order of magnitude.
'''
orders = {'B': 1,
'K': 1024,
'M': 1024 * 1024,
'G': 1024 * 1024 * 1024,
'T': 1024 * 1024 * 1024 * 1024,
}
order = re.findall("([BbKkMmGgTt])", size[-1])
if not order:
size += default_sufix
order = [default_sufix]
return int(float(size[0:-1]) * orders[order[0].upper()])
Eventually we'd have to merge both and have them in autotest, but, for
now, let's go with the function in virt tests. Anyway, this is great
work, see comments below:
> Signed-off-by: Yiqiao Pu <[email protected]>
> ---
> virttest/utils_misc.py | 43 +++++++++++++++++++++++++++++++++++++++++++
> 1 files changed, 43 insertions(+), 0 deletions(-)
>
> diff --git a/virttest/utils_misc.py b/virttest/utils_misc.py
> index d525a6c..af58091 100644
> --- a/virttest/utils_misc.py
> +++ b/virttest/utils_misc.py
> @@ -1359,3 +1359,46 @@ def signal_program(program_name, sig=signal.SIGTERM,
> pid_files_dir=None):
> pid = get_pid_from_file(program_name, pid_files_dir)
> if pid:
> utils.signal_pid(pid, sig)
> +
> +
> +def standard_value(value_str, standard_unit="M", base="1024"):
See, when we say later in the function 'B', we're mostly talking about
data sizes, so the function name is way too generic. I'd say
normalize_data_size is a better API name.
> + """
> + return the value based on the standard unit given
Normalize a data size in one order of magnitude to another (MB to GB,
for example).
> +
> + @param value_str: a string include the data and unit
> + @param standard_unit: the unit of the result based
> + @param base: the base between two adjacent unit. Normally could be 1024
> + or 1000
> + """
The usage of standard_unit doesn't seem right, as in fact we're
talking about a single unit (B), with different orders of magnitude,
and the factor you have to multiply to get to one order of magnitude
to the other, so I'd say order_magnitude rather than standard_unit,
and factor rather than base.
> + def _get_unit_index(unit_list, unit_value):
> + for i in unit_list:
> + stand_unit = re.findall("[\s\d](%s)" % i, str(unit_value), re.I)
> + if stand_unit:
> + return unit_list.index(stand_unit[0].upper())
> + return -1
> +
> + unit_list = ['B', 'K', 'M', 'G', 'T']
> + try:
> + data = float(re.findall("[\d\.]+",value_str)[0])
> + except IndexError:
> + logging.warn("The format is not right. Please check %s"
This should be an error message, and it'd be better to be "Incorrect
data size format..."
> + " has both data and unit." % value_str)
> + return ""
> +
> + unit_index = _get_unit_index(unit_list, value_str)
> + stand_index = _get_unit_index(unit_list, " %s" % standard_unit)
> +
> + if unit_index < 0 or stand_index < 0:
> + logging.warn("Unknown unit. Please check your value '%s' and
> standard"
Unknown input order of magnitude. Please check your value and desired
order of magnitude.
> + " unit '%s'" % (value_str, standard_unit))
> + return ""
> +
> + if unit_index > stand_index:
> + multiple = float(base)
> + else:
> + multiple = float(1) / float(base)
or float(base) ** -1
> +
> + for _ in range(abs(unit_index - stand_index)):
> + data *= multiple
> +
> + return str(data)
> --
> 1.7.7.6
>
> _______________________________________________
> Virt-test-devel mailing list
> [email protected]
> https://www.redhat.com/mailman/listinfo/virt-test-devel
--
Lucas
_______________________________________________
Virt-test-devel mailing list
[email protected]
https://www.redhat.com/mailman/listinfo/virt-test-devel