Completed
Pull Request — master (#59)
by Oleg
02:10
created

Enumerable   A

Complexity

Total Complexity 18

Size/Duplication

Total Lines 93
Duplicated Lines 0 %

Test Coverage

Coverage 91.18%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 18
c 1
b 0
f 0
dl 0
loc 93
ccs 31
cts 34
cp 0.9118
rs 10

6 Methods

Rating   Name   Duplication   Size   Complexity  
B remove_starting_zeros() 0 14 5
A increment_last_level() 0 6 1
B generate_numeration() 0 17 5
A get_level() 0 12 2
A __init__() 0 11 1
A get_string() 0 17 4
1
"""
2
SDoc
3
4
Copyright 2016 Set Based IT Consultancy
5
6
Licence MIT
7
"""
8
# ----------------------------------------------------------------------------------------------------------------------
9
10
11 1
class Enumerable:
12
    """
13
    Class for storing information about numeration of heading nodes.
14
    """
15
16
    # ------------------------------------------------------------------------------------------------------------------
17 1
    def __init__(self):
18
        """
19
        Object constructor.
20
        """
21
22 1
        self._numerate_data = {}
23 1
        """
24
        The info about node numeration.
25
26
        :type: dict[int,int]
27
        """
28
29
    # ------------------------------------------------------------------------------------------------------------------
30 1
    def get_level(self, level):
31
        """
32
        Gets the level from numerate data.
33
34
        :param: int level: The level.
35
36
        :rtype: int|None
37
        """
38 1
        if level in self._numerate_data:
39 1
            return self._numerate_data[level]
40
        else:
41
            return None
42
43
    # ------------------------------------------------------------------------------------------------------------------
44 1
    def generate_numeration(self, level):
45
        """
46
        Sets current enumeration of headings at a heading level.
47
48
        :param int level: The level of nested heading.
49
        """
50 1
        for num in range(level + 1):
51 1
            if num not in self._numerate_data:
52 1
                self._numerate_data[num] = 0
53
54
        # Truncate levels.
55 1
        new_numerate_data = {}
56 1
        for key in self._numerate_data:
57 1
            if key <= level:
58 1
                new_numerate_data[key] = self._numerate_data[key]
59
60 1
        self._numerate_data = new_numerate_data
61
62
    # ------------------------------------------------------------------------------------------------------------------
63 1
    def increment_last_level(self):
64
        """
65
        Increments the last level in number of a heading number.
66
        """
67 1
        last_level = max(self._numerate_data)
68 1
        self._numerate_data[last_level] += 1
69
70
    # ------------------------------------------------------------------------------------------------------------------
71 1
    def remove_starting_zeros(self):
72
        """
73
        Removes starting multiple zero symbols. And lefts one if we have omitted levels.
74
        """
75
        # Check if we have level 'bigger' than part and
76
        # if first encountered level is equal 0, we start passing from level '2' to max level.
77 1
        if 1 in self._numerate_data and self._numerate_data[1] == 0:
78 1
            for key in range(2, max(self._numerate_data)):
79
80
                # If we have '0' we remove them until we not encountered '0', otherwise we go out the loop.
81 1
                if self._numerate_data[key] == 0:
82
                    del self._numerate_data[key]
83
                else:
84 1
                    break
85
86
    # ------------------------------------------------------------------------------------------------------------------
87 1
    def get_string(self):
88
        """
89
        Returns the string equivalent of levels for future output.
90
91
        :rtype: str
92
        """
93 1
        numbering = []
94
95 1
        if max(self._numerate_data) == 0:
96
            return self._numerate_data[0]
97
        else:
98 1
            for key in self._numerate_data:
99
                # If we need to generate chapter, subsection ... etc. we omit outputting part number.
100 1
                if key != 0:
101 1
                    numbering.append(self._numerate_data[key])
102
103 1
            return '.'.join(map(str, numbering))
0 ignored issues
show
introduced by
Used builtin function 'map'
Loading history...
104
105
# ----------------------------------------------------------------------------------------------------------------------
106