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

extend_AllIndexed   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 30
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
wmc 11
eloc 22
dl 0
loc 30
rs 10
c 0
b 0
f 0

1 Function

Rating   Name   Duplication   Size   Complexity  
C extend_AllIndexed() 0 28 11
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