Passed
Pull Request — master (#114)
by
unknown
02:32 queued 01:12
created

benedict.dicts.keylist.keylist_util   B

Complexity

Total Complexity 48

Size/Duplication

Total Lines 150
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 116
dl 0
loc 150
rs 8.5599
c 0
b 0
f 0
wmc 48

10 Functions

Rating   Name   Duplication   Size   Complexity  
A _get_index() 0 4 2
A set_item() 0 12 3
A _new_item_value() 0 3 2
A _get_or_new_item_value() 0 9 3
D _get_item_key_and_value_for_parent_wildcard() 0 30 12
A get_item() 0 3 2
A get_items() 0 20 5
B _set_item_value() 0 16 6
B _get_item_key_and_value() 0 19 8
A _get_item_key_and_value_for_wildcard() 0 8 5

How to fix   Complexity   

Complexity

Complex classes like benedict.dicts.keylist.keylist_util often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

1
# -*- coding: utf-8 -*-
2
3
from itertools import chain
4
5
from benedict.utils import type_util
6
7
8
def _get_index(key):
9
    if type_util.is_integer(key):
10
        return key
11
    return None
12
13
14
def _get_item_key_and_value_for_parent_wildcard(item, index, parent, child):
15
    if type_util.is_list_of_dicts(item) and any(
16
        index in _item.keys() for _item in item
17
    ):
18
        data = [_item.get(index) for _item in item if index in _item.keys()]
19
        if type_util.is_list_of_list(data):
20
            data = list(chain.from_iterable(data))
21
        # eject dict from list to be able to access dict properties
22
        if (
23
            len(data) == 1
24
            and len(item) == 1
25
            and type_util.is_wildcard(parent)
26
            and not type_util.is_wildcard(index)
27
            and not type_util.is_wildcard(child)
28
        ):
29
            data = data[0]
30
        return index, data
31
    elif type_util.is_list_of_list(item):
32
        if type_util.is_integer(index):
33
            return index, item[index]
34
        else:
35
            data = [
36
                _item.get(index)
37
                for _item in chain.from_iterable(item)
38
                if index in _item.keys()
39
            ]
40
            return index, data
41
    elif type_util.is_wildcard(index):
42
        return index, item
43
    return index, None
44
45
46
def _get_item_key_and_value_for_wildcard(item, index):
47
    if type_util.is_list_of_dicts(item) and not type_util.is_integer(index):
48
        data = [_item.get(index) for _item in item if index in _item.keys()]
49
        if type_util.is_list_of_list(data):
50
            data = list(chain.from_iterable(data))
51
        if any(data):
52
            return index, data
53
    return index, item
54
55
56
def _get_item_key_and_value(item, index, parent=None, child=None):
57
    if type_util.is_list_or_tuple(item):
58
        if type_util.is_wildcard(parent):
59
            index, item = _get_item_key_and_value_for_parent_wildcard(
60
                item, index, parent, child
61
            )
62
            if item:
63
                return index, item
64
        elif type_util.is_wildcard(index):
65
            index, item = _get_item_key_and_value_for_wildcard(item, index)
66
            if item:
67
                return index, item
68
        else:
69
            index = _get_index(index)
70
            if index is not None:
71
                return index, item[index]
72
    elif type_util.is_dict(item):
73
        return index, item[index]
74
    raise KeyError(f"Invalid key: '{index}'")
75
76
77
def _get_or_new_item_value(item, key, subkey):
78
    try:
79
        _, value = _get_item_key_and_value(item, key)
80
        if not type_util.is_dict_or_list_or_tuple(value):
81
            raise TypeError
82
    except (IndexError, KeyError, TypeError):
83
        value = _new_item_value(subkey)
84
        _set_item_value(item, key, value)
85
    return value
86
87
88
def _new_item_value(key):
89
    index = _get_index(key)
90
    return {} if index is None else []
91
92
93
def _set_item_value(item, key, value):
94
    index = _get_index(key)
95
    if index is not None:
96
        try:
97
            # overwrite existing index
98
            item[index] = value
99
        except IndexError:
100
            # insert index
101
            item += [None] * (index - len(item))
102
            item.insert(index, value)
103
    elif type_util.is_list(item):
104
        for idx, _item in enumerate(value):
105
            if _item is not None:
106
                item[idx].update({key: _item})
107
    else:
108
        item[key] = value
109
110
111
def get_item(d, keys):
112
    items = get_items(d, keys)
113
    return items[-1] if items else (None, None, None)
114
115
116
def get_items(d, keys):
117
    items = []
118
    item = d
119
    for index, key in enumerate(keys):
120
        try:
121
            if any(items):
122
                parent = items[-1][1]
123
            else:
124
                parent = None
125
            if index < len(keys) - 1:
126
                child = keys[index + 1]
127
            else:
128
                child = None
129
            item_key, item_value = _get_item_key_and_value(item, key, parent, child)
130
            items.append((item, item_key, item_value))
131
            item = item_value
132
        except (IndexError, KeyError):
133
            items.append((None, None, None))
134
            break
135
    return items
136
137
138
def set_item(d, keys, value):
139
    item = d
140
    i = 0
141
    j = len(keys)
142
    while i < j:
143
        key = keys[i]
144
        if i < (j - 1):
145
            item = _get_or_new_item_value(item, key, keys[i + 1])
146
            i += 1
147
            continue
148
        _set_item_value(item, key, value)
149
        break
150