Completed
Pull Request — master (#32)
by Oleg
01:55
created

Enumerable.get_string()   A

Complexity

Conditions 4

Size

Total Lines 17

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 17
rs 9.2
cc 4
1
"""
2
SDoc
3
4
Copyright 2016 Set Based IT Consultancy
5
6
Licence MIT
7
"""
8
# ----------------------------------------------------------------------------------------------------------------------
9
10
11
class Enumerable:
12
    """
13
    Class for storing information about numeration of heading nodes.
14
    """
15
16
    # ------------------------------------------------------------------------------------------------------------------
17
    def __init__(self):
18
        """
19
        Object constructor.
20
        """
21
22
        self._numerate_data = {}
23
24
    # ------------------------------------------------------------------------------------------------------------------
25
    def get_numeration(self, level):
26
        """
27
        Sets current enumeration of headings at a heading level.
28
29
        :param int level: The level of nested heading.
30
        """
31
        if not len(self._numerate_data):
32
            self._numerate_data[level] = 0
33
        else:
34
            if level not in self._numerate_data:
35
                self._numerate_data[level] = 0
36
37
        # Truncate levels.
38
        new_numerate_data = {}
39
        for key in self._numerate_data:
40
            if key <= level:
41
                new_numerate_data[key] = self._numerate_data[key]
42
43
        self._numerate_data = new_numerate_data
44
45
    # ------------------------------------------------------------------------------------------------------------------
46
    def increment_last_level(self):
47
        """
48
        Increments the last level in number of a heading number.
49
        """
50
        last_level = max(self._numerate_data)
51
        self._numerate_data[last_level] += 1
52
53
    # ------------------------------------------------------------------------------------------------------------------
54
    def get_string(self):
55
        """
56
        Returns the string equivalent of levels for future output.
57
58
        :rtype: str
59
        """
60
        numbering = []
61
62
        if max(self._numerate_data) == 0:
63
            return self._numerate_data[0]
64
        else:
65
            for key in self._numerate_data:
66
                # If we need to generate chapter, subsection ... etc. we omit outputting part number.
67
                if key != 0:
68
                    numbering.append(self._numerate_data[key])
69
70
            return '.'.join(map(str, numbering))
0 ignored issues
show
introduced by
Used builtin function 'map'
Loading history...
71
72
# ----------------------------------------------------------------------------------------------------------------------
73