diff options
author | rexlee8776 <limingjiang@huawei.com> | 2017-07-05 02:50:06 +0000 |
---|---|---|
committer | rexlee8776 <limingjiang@huawei.com> | 2017-07-06 07:49:10 +0000 |
commit | 25a37b2048281c64719bd6ad67860f65f6c31546 (patch) | |
tree | 726e1ff64ea3470ab6b91db8fdbc386383f08903 /yardstick/common/utils.py | |
parent | 3369461d0e51b22aa96e5bfd0d80b3f0f7f82c67 (diff) |
move flatten dict key to common utils
So it can easily be used by other testcase to unify result
JIRA: YARDSTICK-702
Change-Id: Id4fde38a9a0c2a87a6c870bdb7b0c8f3a3b371ac
Signed-off-by: rexlee8776 <limingjiang@huawei.com>
Diffstat (limited to 'yardstick/common/utils.py')
-rw-r--r-- | yardstick/common/utils.py | 23 |
1 files changed, 23 insertions, 0 deletions
diff --git a/yardstick/common/utils.py b/yardstick/common/utils.py index 7aab46942..7633777ae 100644 --- a/yardstick/common/utils.py +++ b/yardstick/common/utils.py @@ -23,6 +23,8 @@ import logging import os import subprocess import sys +import collections +import six from functools import reduce import yaml @@ -189,3 +191,24 @@ def get_port_ip(sshclient, port): if status: raise RuntimeError(stderr) return stdout.rstrip() + + +def flatten_dict_key(data): + next_data = {} + + # use list, because iterable is too generic + if not any(isinstance(v, (collections.Mapping, list)) for v in data.values()): + return data + + for k, v in six.iteritems(data): + if isinstance(v, collections.Mapping): + for n_k, n_v in six.iteritems(v): + next_data["%s.%s" % (k, n_k)] = n_v + # use list because iterable is too generic + elif isinstance(v, list): + for index, item in enumerate(v): + next_data["%s%d" % (k, index)] = item + else: + next_data[k] = v + + return flatten_dict_key(next_data) |