Failed Conditions
Pull Request — master (#2076)
by Abdeali
02:04
created

coalib/results/AbsolutePosition.py (11 issues)

1
from coalib.results.TextPosition import TextPosition
2
from coalib.misc.Decorators import enforce_signature
3
4
5
class AbsolutePosition(TextPosition):
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable TextPosition does not seem to be defined.
Loading history...
6
7
    @enforce_signature
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable enforce_signature does not seem to be defined.
Loading history...
8
    def __init__(self,
9
                 text: (tuple, list, None)=None,
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable list does not seem to be defined.
Loading history...
Comprehensibility Best Practice introduced by
The variable tuple does not seem to be defined.
Loading history...
10
                 position: (int, None)=None):
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable int does not seem to be defined.
Loading history...
11
        """
12
        Creates an AbsolutePosition object that represents the index of a
13
        character in a string.
14
15
        :param text:     The text containing the character.
16
        :param position: Position identifying the index of character
17
                         in text.
18
        """
19
        line = column = None
20
        if position is not None and text is not None:
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable position does not seem to be defined.
Loading history...
Comprehensibility Best Practice introduced by
The variable text does not seem to be defined.
Loading history...
21
            line, column = calc_line_col(text, position)
22
        self._text = text
23
        self._position = position
24
        super().__init__(line, column)
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable line does not seem to be defined.
Loading history...
Comprehensibility Best Practice introduced by
The variable column does not seem to be defined.
Loading history...
25
26
    @property
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable property does not seem to be defined.
Loading history...
27
    def position(self):
28
        return self._position
0 ignored issues
show
Comprehensibility Best Practice introduced by
The variable self does not seem to be defined.
Loading history...
29
30
31
def calc_line_col(text, position):
32
    r"""
33
    Creates a tuple containing (line, column) by calculating line number
34
    and column in the text, from position.
35
36
    The position represents the index of a character. In the following
37
    example 'a' is at position '0' and it's corresponding line and column are:
38
39
    >>> calc_line_col(('a\n',), 0)
40
    (1, 1)
41
42
    All special characters(including the newline character) belong in the same
43
    line, and have their own position. A line is an item in the tuple:
44
45
    >>> calc_line_col(('a\n', 'b\n'), 1)
46
    (1, 2)
47
    >>> calc_line_col(('a\n', 'b\n'), 2)
48
    (2, 1)
49
50
    :param text:          A tuple/list of lines in which position is to
51
                          be calculated.
52
    :param position:      Position (starting from 0) of character to be found
53
                          in the (line, column) form.
54
    :return:              A tuple of the form (line, column), where both line
55
                          and column start from 1.
56
    """
57
    for linenum, line in enumerate(text, start=1):
58
        linelen = len(line)
59
        if position < linelen:
60
            return linenum, position + 1
61
        position -= linelen
62
63
    raise ValueError("Position not found in text")
64