• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

manoss96 / pregex / 4356417821

pending completion
4356417821

push

github

manoss96
add logo and funding

1625 of 1625 relevant lines covered (100.0%)

3.0 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

100.0
/src/pregex/core/operators.py
1
__doc__ = """
3✔
2
This module contains various classes representing operators \
3
that are typically applied between two or more patterns.
4

5
Classes & methods
6
-------------------------------------------
7

8
Below are listed all classes within :py:mod:`pregex.core.operators`
9
along with any possible methods they may possess.
10
"""
11

12

13
import pregex.core.pre as _pre
3✔
14
import pregex.core.exceptions as _ex
3✔
15
from typing import Union as _Union
3✔
16

17

18
class __Operator(_pre.Pregex):
3✔
19
    '''
3✔
20
    Constitutes the base class for all classes that are part of this module.
21

22
    :param tuple[Pregex | str] pres: A tuple of strings or Pregex instances representing \
23
        the patterns to which the operator is to be applied.
24
    :param (tuple[Pregex | str] => str) transform: A `transform` function for the provided pattern.
25

26
    :raises InvalidArgumentTypeException: At least one of the provided arguments \
27
        through ``pres`` is neither a ``Pregex`` instance nor a string.
28

29
    :note: If no arguments are provided, then the resulting ``Pregex`` instance \
30
        corresponds to the "empty string" pattern, whereas if a single argument is \
31
        provided, it is simply returned wrapped within a ``Pregex`` instance.
32
    '''
33
    def __init__(self, pres: tuple[_Union[_pre.Pregex, str]], transform) -> _pre.Pregex:
3✔
34
        '''
35
        Constitutes the base class for all classes that are part of this module.
36

37
        :param tuple[Pregex | str] pres: A tuple of strings or Pregex instances representing \
38
            the patterns to which the operator is to be applied.
39
        :param (tuple[Pregex | str] => str) transform: A `transform` function for the provided pattern.
40

41
        :raises InvalidArgumentTypeException: At least one of the provided arguments \
42
            through ``pres`` is neither a ``Pregex`` instance nor a string.
43

44
        :note: If no arguments are provided, then the resulting ``Pregex`` instance \
45
            corresponds to the "empty string" pattern, whereas if a single argument is \
46
            provided, it is simply returned wrapped within a ``Pregex`` instance.
47
        '''
48
        if len(pres) == 0:
3✔
49
            result = ''
3✔
50
        else:
51
            result = __class__._to_pregex(pres[0])
3✔
52
            if len(pres) > 1:
3✔
53
                for pre in pres[1:]:
3✔
54
                    result = transform(result, pre)
3✔
55
        super().__init__(str(result), escape=False)
3✔
56

57

58
class Concat(__Operator):
3✔
59
    '''
3✔
60
    Matches the concatenation of the provided patterns.
61

62
    :param Pregex | str \*pres: Two or more patterns that are to be concatenated.
63

64
    :raises NotEnoughArgumentsException: Less than two arguments are provided.
65
    :raises InvalidArgumentTypeException: At least one of the provided arguments \
66
        is neither a ``Pregex`` instance nor a string.
67

68
    :note: If no arguments are provided, then the resulting ``Pregex`` instance \
69
        corresponds to the "empty string" pattern, whereas if a single argument is \
70
        provided, it is simply returned wrapped within a ``Pregex`` instance.
71
    '''
72

73
    def __init__(self, *pres: _Union[_pre.Pregex, str]) -> _pre.Pregex:
3✔
74
        '''
75
        Matches the concatenation of the provided patterns.
76

77
        :param Pregex | str \*pres: Two or more patterns that are to be concatenated.
78

79
        :raises NotEnoughArgumentsException: Less than two arguments are provided.
80
        :raises InvalidArgumentTypeException: At least one of the provided arguments \
81
            is neither a ``Pregex`` instance nor a string.
82

83
        :note: If no arguments are provided, then the resulting ``Pregex`` instance \
84
            corresponds to the "empty string" pattern, whereas if a single argument is \
85
            provided, it is simply returned wrapped within a ``Pregex`` instance.
86
        '''
87
        super().__init__(pres, lambda pre1, pre2: pre1.concat(pre2))
3✔
88

89

90
class Either(__Operator):
3✔
91
    '''
3✔
92
    Matches either one of the provided patterns.
93

94
    :param Pregex | str \*pres: Two or more patterns that constitute the \
95
        operator's alternatives.
96

97
    :raises NotEnoughArgumentsException: Less than two arguments are provided.
98
    :raises InvalidArgumentTypeException: At least one of the provided arguments \
99
        is neither a ``Pregex`` instance nor a string.
100

101
    :note:
102
        - If no arguments are provided, then the resulting ``Pregex`` instance \
103
          corresponds to the "empty string" pattern, whereas if a single argument is \
104
          provided, it is simply returned wrapped within a ``Pregex`` instance.
105
        - One should be aware that ``Either`` is eager, meaning that the regex engine will \
106
          stop the moment it matches either one of the alternatives, starting from \
107
          the left-most pattern and continuing on to the right until a match occurs.
108
    '''
109
    
110
    def __init__(self, *pres: _Union[_pre.Pregex, str]):
3✔
111
        '''
112
        Matches either one of the provided patterns.
113

114
        :param Pregex | str \*pres: Two or more patterns that constitute the \
115
            operator's alternatives.
116

117
        :raises NotEnoughArgumentsException: Less than two arguments are provided.
118
        :raises InvalidArgumentTypeException: At least one of the provided arguments \
119
            is neither a ``Pregex`` instance nor a string.
120

121
        :note:
122
          - If no arguments are provided, then the resulting ``Pregex`` instance \
123
            corresponds to the "empty string" pattern, whereas if a single argument is \
124
            provided, it is simply returned wrapped within a ``Pregex`` instance.
125
          - One should be aware that ``Either`` is eager, meaning that the regex engine will \
126
            stop the moment it matches either one of the alternatives, starting from \
127
            the left-most pattern and continuing on to the right until a match occurs.
128
        '''
129
        super().__init__(pres, lambda pre1, pre2: pre1.either(pre2))
3✔
130

131

132
class Enclose(__Operator):
3✔
133
    '''
3✔
134
    Matches the pattern that results from concatenating the ``enclosing`` \
135
    pattern(s) to both sides of pattern ``pre``.
136

137
    :param Pregex | str pre: The pattern that is to be at the center \
138
        of the concatenation.
139
    :param Pregex | str enclosing: One or more patterns that are to *enclose* \
140
        pattern ``pre`` one by one.
141

142
    :raises NotEnoughArgumentsException: Less than two arguments are provided.
143
    :raises InvalidArgumentTypeException: Either ``pre`` or at least one of the \
144
        ``enclosing`` patterns is neither a ``Pregex`` instance nor a string.
145
    '''
146

147
    def __init__(self, pre: _Union[_pre.Pregex, str], *enclosing:_Union[_pre.Pregex, str]) -> _pre.Pregex:
3✔
148
        '''
149
        Matches the pattern that results from concatenating the ``enclosing`` \
150
        pattern(s) to both sides of pattern ``pre``.
151

152
        :param Pregex | str pre: The pattern that is to be at the center \
153
            of the concatenation.
154
        :param Pregex | str enclosing: One or more patterns that are to *enclose* \
155
            pattern ``pre`` one by one.
156

157
        :raises NotEnoughArgumentsException: Less than two arguments are provided.
158
        :raises InvalidArgumentTypeException: Either ``pre`` or at least one of the \
159
            ``enclosing`` patterns is neither a ``Pregex`` instance nor a string.
160
        '''
161
        super().__init__((pre, *enclosing), lambda pre1, pre2: pre1.enclose(pre2))
3✔
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc