Passed
Push — master ( b3caf6...57589b )
by Fabio
02:22
created

benedict.core.nest   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 23
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 5
eloc 17
dl 0
loc 23
rs 10
c 0
b 0
f 0

2 Functions

Rating   Name   Duplication   Size   Complexity  
A _nest_items() 0 5 2
A nest() 0 10 3
1
# -*- coding: utf-8 -*-
2
3
from benedict.core.groupby import groupby
4
5
6
def _nest_items(nested_items, item, id_key, children_key):
7
    children_items = nested_items.pop(item[id_key], [])
8
    item[children_key] = children_items
9
    for child_item in children_items:
10
        _nest_items(nested_items, child_item, id_key, children_key)
11
12
13
def nest(items, id_key, parent_id_key, children_key):
14
    if any([id_key == parent_id_key,
15
            id_key == children_key,
16
            parent_id_key == children_key]):
17
        raise ValueError('keys should be different.')
18
    nested_items = groupby(items, parent_id_key)
19
    root_items = nested_items.get(None, [])
20
    for item in root_items:
21
        _nest_items(nested_items, item, id_key, children_key)
22
    return nested_items.get(None)
23