Passed
Push — master ( db2481...3bd527 )
by Fabio
03:28
created

benedict.core.traverse._traverse_collection()   A

Complexity

Conditions 3

Size

Total Lines 5
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 5
dl 0
loc 5
rs 10
c 0
b 0
f 0
cc 3
nop 2
1
# -*- coding: utf-8 -*-
2
3
from benedict.utils import type_util
4
5
6
def _traverse_collection(d, callback):
7
    if type_util.is_dict(d):
8
        _traverse_dict(d, callback)
9
    elif type_util.is_list_or_tuple(d):
10
        _traverse_list(d, callback)
11
12
13
def _traverse_dict(d, callback):
14
    keys = list(d.keys())
15
    for key in keys:
16
        value = d.get(key, None)
17
        callback(d, key, value)
18
        _traverse_collection(value, callback)
19
20
21
def _traverse_list(l, callback):
22
    for index in range(len(l)):
23
        value = l[index]
24
        callback(l, index, value)
25
        _traverse_collection(value, callback)
26
27
28
def traverse(d, callback):
29
    if not callable(callback):
30
        raise ValueError('callback argument must be a callable.')
31
    _traverse_collection(d, callback)
32