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

benedict.core.nest.nest()   A

Complexity

Conditions 3

Size

Total Lines 10
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 10
dl 0
loc 10
rs 9.9
c 0
b 0
f 0
cc 3
nop 4
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