convert_data_size('1M') == 1024 * 1024 bytes Signed-off-by: Jiří Župka <jzupka@xxxxxxxxxx> --- client/shared/base_utils.py | 58 ++++++++++++++++++++++++++++++++++ client/shared/base_utils_unittest.py | 41 ++++++++++++++++++++++++ 2 files changed, 99 insertions(+), 0 deletions(-) diff --git a/client/shared/base_utils.py b/client/shared/base_utils.py index 8e38413..8c23c8e 100644 --- a/client/shared/base_utils.py +++ b/client/shared/base_utils.py @@ -400,6 +400,41 @@ def matrix_to_string(matrix, header=None): return matrix_str +class Statistic(object): + """ + Class to display and collect average, + max and min values of a given data set. + """ + def __init__(self): + self._sum = 0 + self._count = 0 + self._max = None + self._min = None + + def get_average(self): + if self._count != 0: + return self._sum / self._count + else: + return None + + def get_min(self): + return self._min + + def get_max(self): + return self._max + + def record(self, value): + """ + Record new value to statistic. + """ + self._count += 1 + self._sum += value + if not self._max or self._max < value: + self._max = value + if not self._min or self._min > value: + self._min = value + + def read_keyval(path): """ Read a key-value pair format file into a dictionary, and return it. @@ -1972,6 +2007,29 @@ def display_data_size(size): return '%.2f %s' % (size, prefixes[i]) +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()]) + + def interactive_download(url, output_file, title='', chunk_size=100*1024): ''' Interactively downloads a given file url to a given output file diff --git a/client/shared/base_utils_unittest.py b/client/shared/base_utils_unittest.py index a78cdb6..604c441 100755 --- a/client/shared/base_utils_unittest.py +++ b/client/shared/base_utils_unittest.py @@ -150,6 +150,47 @@ class test_open_write_close(unittest.TestCase): self.assertEqual(data, test_file.final_data) +class test_Statisctic(unittest.TestCase): + def setUp(self): + self.god = mock.mock_god(ut=self) + + + def tearDown(self): + self.god.unstub_all() + + + def test_simple_functionality(self): + stat = base_utils.Statistic() + stat.record(5) + stat.record(15) + stat.record(10) + self.assertEqual(10, stat.get_average()) + self.assertEqual(5, stat.get_min()) + self.assertEqual(15, stat.get_max()) + + +class test_ConvertDataSize(unittest.TestCase): + def setUp(self): + self.god = mock.mock_god(ut=self) + + + def tearDown(self): + self.god.unstub_all() + + + def test_simple_functionality(self): + size = 12 + self.assertEqual(size, base_utils.convert_data_size('12')) + size *= 1024 + self.assertEqual(size, base_utils.convert_data_size('12K')) + size *= 1024 + self.assertEqual(size, base_utils.convert_data_size('12m')) + size *= 1024 + self.assertEqual(size, base_utils.convert_data_size('12G')) + size *= 1024 + self.assertEqual(size, base_utils.convert_data_size('12T')) + + class test_read_keyval(unittest.TestCase): def setUp(self): self.god = mock.mock_god(ut=self) -- 1.7.7.6 -- To unsubscribe from this list: send the line "unsubscribe kvm" in the body of a message to majordomo@xxxxxxxxxxxxxxx More majordomo info at http://vger.kernel.org/majordomo-info.html