Passed
Push — master ( 7d6f78...eb825e )
by Konstantin
11:20
created

extend_AllIndexed.extend_AllIndexed()   C

Complexity

Conditions 11

Size

Total Lines 28
Code Lines 21

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 21
dl 0
loc 28
rs 5.4
c 0
b 0
f 0
cc 11
nop 3

How to fix   Complexity   

Complexity

Complex classes like extend_AllIndexed.extend_AllIndexed() 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
# pylint: disable=line-too-long,invalid-name,missing-module-docstring
2
def extend_AllIndexed(self, elements, validate_continuity=False):
3
    """
4
    Add all elements in list ``elements``, respecting ``@index`` order.
5
    With ``validate_continuity``, check that all new elements come after all old elements
6
    (or raise an exception). 
7
    Otherwise, ensure this condition silently (by increasing ``@index`` accordingly).
8
    """
9
    if not isinstance(elements, list):
10
        elements = [elements]
11
    siblings = self.get_AllIndexed()
12
    highest_sibling_index = siblings[-1].index if siblings else -1
13
    if validate_continuity:
14
        elements = sorted(elements, key=lambda x: x.index)
15
        lowest_element_index = elements[0].index
16
        if lowest_element_index <= highest_sibling_index:
17
            raise Exception("@index already used: {}".format(lowest_element_index))
18
    else:
19
        for element in elements:
20
            highest_sibling_index += 1
21
            element.index = highest_sibling_index
22
    for element in elements:
23
        if isinstance(element, RegionRefIndexedType): # pylint: disable=undefined-variable
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable RegionRefIndexedType does not seem to be defined.
Loading history...
24
            self.add_RegionRefIndexed(element)
25
        elif isinstance(element, OrderedGroupIndexedType): # pylint: disable=undefined-variable
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable OrderedGroupIndexedType does not seem to be defined.
Loading history...
26
            self.add_OrderedGroupIndexed(element)
27
        elif isinstance(element, UnorderedGroupIndexedType): # pylint: disable=undefined-variable
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable UnorderedGroupIndexedType does not seem to be defined.
Loading history...
28
            self.add_UnorderedGroupIndexed(element)
29
    return self.get_AllIndexed()
30