You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

25 lines
789 B

import splunklib.six as six
def accumulate_to_dict(dict, keys, value):
"""
Initialize hierarchical dict with giving keys and value.
If dict has giving keys, add value to dict's existing value.
Keys could be a string like "a.b.c" or a array like ["a", "b", "c"]
"""
try:
value = float(value)
except:
value = 0
if isinstance(keys, six.string_types):
keys = keys.split('.')
pre_dict = dict
for i in range(len(keys)):
if keys[i] not in pre_dict:
if i == len(keys) - 1:
pre_dict[keys[i]] = value
else:
pre_dict[keys[i]] = {}
else:
if i == len(keys) - 1:
pre_dict[keys[i]] += value
pre_dict = pre_dict[keys[i]]