def complete_pv(pathvalues: MutableMapping[Path, float]) -> MutableMapping[Path, float]:
""" Consider a pathvalue dictionary of the form Dict[Path, float] e.g.
{1.1.1: 12.0} (here: only one entry). This function will disect each path
and assign its value to the truncated path: e.g. here 1, 1.1 and 1.1.1.
Thus we get {1: 12.0, 1.1: 12.0, 1.1.1: 12.0}. For more items the values
will be summed accordingly.
Furthermore the total sum of the items of
the topmost level will be assigned to the empty path. For this to make
sense we require that no empy path is in the data beforehand""
:param pathvalues: {path: value} dictionary
:return: {path: value}
dictionary
"""
if Path(()) in pathvalues:
raise ValueError("This function does not allow the empty path as item"
"in the data list.")
completed = collections.defaultdict(float)
for path, value in pathvalues.items():
# len(path) +1 ensures that also the whole tag is considered
# starting point 0: also add to empty path.
for level in range(0, len(path) + 1):
completed[path[:level]] += value
return completed
评论列表
文章目录