• 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/quantifiers.py
1
__doc__ = """
3✔
2
Every class within this module is used to declare that a pattern is to be
3
matched a number of times, with each class representing a slightly different
4
pattern-repetition rule.
5

6
Classes & methods
7
-------------------------------------------
8

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

13

14
import pregex.core.pre as _pre
3✔
15
from typing import Union as _Union
3✔
16
from typing import Optional as _Optional
3✔
17

18

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

23
    :param Pregex | str pre: A Pregex instance or string representing the pattern \
24
        that is to be quantified.
25
    :param (Pregex => str) transform: A `transform` function for the provided pattern.
26

27
    :raises InvalidArgumentTypeException: Parameter ``pre`` is neither a \
28
        ``Pregex`` instance nor a string.
29
    :raises CannotBeRepeatedException: Parameter ``pre`` represents a non-repeatable \
30
        pattern. Whether this exception is thrown also depends on certain parameter values.
31

32
    '''
33
    def __init__(self, pre: _Union[_pre.Pregex, str], is_greedy: bool, transform) -> '__Quantifier':
3✔
34
        '''
35
        Constitutes the base class for all classes that are part of this module.
36

37
        :param Pregex | str pre: A Pregex instance or string representing the pattern \
38
            that is to be quantified.
39
        :param (Pregex => str) transform: A `transform` function for the provided pattern.
40

41
        :raises InvalidArgumentTypeException: Parameter ``pre`` is neither a \
42
            ``Pregex`` instance nor a string.
43
        :raises CannotBeRepeatedException: Parameter ``pre`` represents a non-repeatable \
44
            pattern. Whether this exception is thrown also depends on certain parameter values.
45
        '''
46
        pattern = transform(__class__._to_pregex(pre), is_greedy)
3✔
47
        super().__init__(str(pattern), escape=False)
3✔
48

49

50
class Optional(__Quantifier):
3✔
51
    '''
3✔
52
    Matches the provided pattern once or not at all.
53

54
    :param Pregex | str pre: The pattern that is to be quantified.
55
    :param bool is_greedy: Indicates whether to declare this quantifier as greedy. \
56
        When declared as such, the regex engine will try to match \
57
        the expression as many times as possible. Defaults to ``True``.
58

59
    :raises InvalidArgumentTypeException: Parameter ``pre`` is neither a \
60
        ``Pregex`` instance nor a string.
61
    '''
62

63
    def __init__(self, pre: _Union[_pre.Pregex, str], is_greedy: bool = True) -> _pre.Pregex:
3✔
64
        '''
65
        Matches the provided pattern once or not at all.
66

67
        :param Pregex | str pre: The pattern that is to be quantified.
68
        :param bool is_greedy: Indicates whether to declare this quantifier as greedy. \
69
            When declared as such, the regex engine will try to match \
70
            the expression as many times as possible. Defaults to ``True``.
71

72
        :raises InvalidArgumentTypeException: Parameter ``pre`` is neither a \
73
            ``Pregex`` instance nor a string.
74
        '''
75
        super().__init__(pre, is_greedy, lambda pre, is_greedy: pre.optional(is_greedy))
3✔
76

77

78
class Indefinite(__Quantifier):
3✔
79
    '''
3✔
80
    Matches the provided pattern zero or more times.
81

82
    :param Pregex | str pre: The pattern that is to be quantified.
83
    :param bool is_greedy: Indicates whether to declare this quantifier as greedy. \
84
        When declared as such, the regex engine will try to match \
85
        the expression as many times as possible. Defaults to ``True``.
86

87
    :raises InvalidArgumentTypeException: Parameter ``pre`` is neither a \
88
        ``Pregex`` instance nor a string.
89
    :raises CannotBeRepeatedException: Parameter ``pre`` represents a non-repeatable pattern.
90
    '''
91

92
    def __init__(self, pre: _Union[_pre.Pregex, str], is_greedy: bool = True) -> _pre.Pregex:
3✔
93
        '''
94
        Matches the provided pattern zero or more times.
95

96
        :param Pregex | str pre: The pattern that is to be quantified.
97
        :param bool is_greedy: Indicates whether to declare this quantifier as greedy. \
98
            When declared as such, the regex engine will try to match \
99
            the expression as many times as possible. Defaults to ``True``.
100

101
        :raises CannotBeRepeatedException: Parameter ``pre`` represents a non-repeatable pattern.
102
        '''
103
        super().__init__(pre, is_greedy, lambda pre, is_greedy: pre.indefinite(is_greedy))
3✔
104

105

106
class OneOrMore(__Quantifier):
3✔
107
    '''
3✔
108
    Matches the provided pattern one or more times.
109

110
    :param Pregex | str pre: The pattern that is to be quantified.
111
    :param bool is_greedy: Indicates whether to declare this quantifier as greedy. \
112
        When declared as such, the regex engine will try to match \
113
        the expression as many times as possible. Defaults to ``True``.
114

115
    :raises InvalidArgumentTypeException: Parameter ``pre`` is neither a \
116
        ``Pregex`` instance nor a string.
117
    :raises CannotBeRepeatedException: Parameter ``pre`` represents a non-repeatable pattern.
118
    '''
119

120
    def __init__(self, pre: _Union[_pre.Pregex, str], is_greedy: bool = True) -> _pre.Pregex:
3✔
121
        '''
122
        Matches the provided pattern one or more times.
123

124
        :param Pregex | str pre: The pattern that is to be quantified.
125
        :param bool is_greedy: Indicates whether to declare this quantifier as greedy. \
126
            When declared as such, the regex engine will try to match \
127
            the expression as many times as possible. Defaults to ``True``.
128

129
        :raises InvalidArgumentTypeException: Parameter ``pre`` is neither a \
130
            ``Pregex`` instance nor a string.
131
        :raises CannotBeRepeatedException: Parameter ``pre`` represents a non-repeatable pattern.
132
        '''
133
        super().__init__(pre, is_greedy, lambda pre, is_greedy: pre.one_or_more(is_greedy))
3✔
134

135

136
class Exactly(__Quantifier):
3✔
137
    '''
3✔
138
    Matches the provided pattern an exact number of times.
139

140
    :param Pregex | str pre: The pattern that is to be quantified.
141
    :param int n: The exact number of times that the provided pattern is to be matched.
142

143
    :raises InvalidArgumentTypeException: 
144
        - Parameter ``pre`` is neither a ``Pregex`` instance nor a string.
145
        - Parameter ``n`` is not an integer.
146
    :raises InvalidArgumentValueException: Parameter ``n`` has a value of less than zero.
147
    :raises CannotBeRepeatedException: Parameter ``pre`` represents a non-repeatable \
148
        pattern while parameter ``n`` has been set to a value of greater than ``1``.
149
    '''
150

151
    def __init__(self, pre: _Union[_pre.Pregex, str], n: int) -> _pre.Pregex:
3✔
152
        '''
153
        Matches the provided pattern an exact number of times.
154

155
        :param Pregex | str pre: The pattern that is to be quantified.
156
        :param int n: The exact number of times that the provided pattern is to be matched.
157

158
        :raises InvalidArgumentTypeException: 
159
            - Parameter ``pre`` is neither a ``Pregex`` instance nor a string.
160
            - Parameter ``n`` is not an integer.
161
        :raises InvalidArgumentValueException: Parameter ``n`` has a value of less than zero.
162
        :raises CannotBeRepeatedException: Parameter ``pre`` represents a non-repeatable \
163
            pattern while parameter ``n`` has been set to a value of greater than ``1``.
164
        '''
165
        super().__init__(pre, False, lambda pre, _: pre.exactly(n))
3✔
166

167

168
class AtLeast(__Quantifier):
3✔
169
    '''
3✔
170
    Matches the provided pattern a minimum number of times.
171

172
    :param Pregex | str pre: The pattern that is to be quantified.
173
    :param int n: The minimum number of times that the provided pattern is to be matched.
174
    :param bool is_greedy: Determines whether to declare this quantifier as greedy. \
175
        When declared as such, the regex engine will try to match \
176
        the expression as many times as possible. Defaults to ``True``.
177

178
    :raises InvalidArgumentTypeException: 
179
        - Parameter ``pre`` is neither a ``Pregex`` instance nor a string.
180
        - Parameter ``n`` is not an integer.
181
    :raises InvalidArgumentValueException: Parameter ``n`` has a value of less than zero.
182
    :raises CannotBeRepeatedException: Parameter ``pre`` represents a non-repeatable pattern.
183
    '''
184

185
    def __init__(self, pre: _Union[_pre.Pregex, str], n: int, is_greedy: bool = True) -> _pre.Pregex:
3✔
186
        '''
187
        Matches the provided pattern a minimum number of times.
188

189
        :param Pregex | str pre: The pattern that is to be quantified.
190
        :param int n: The minimum number of times that the provided pattern is to be matched.
191
        :param bool is_greedy: Determines whether to declare this quantifier as greedy. \
192
            When declared as such, the regex engine will try to match \
193
            the expression as many times as possible. Defaults to ``True``.
194

195
        :raises InvalidArgumentTypeException: 
196
            - Parameter ``pre`` is neither a ``Pregex`` instance nor a string.
197
            - Parameter ``n`` is not an integer.
198
        :raises InvalidArgumentValueException: Parameter ``n`` has a value of less than zero.
199
        :raises CannotBeRepeatedException: Parameter ``pre`` represents a non-repeatable pattern.
200
        '''
201
        super().__init__(pre, is_greedy, lambda pre, is_greedy: pre.at_least(n, is_greedy))
3✔
202

203

204
class AtMost(__Quantifier):
3✔
205
    '''
3✔
206
    Matches the provided pattern up to a maximum number of times.
207

208
    :param Pregex | str pre: The pattern that is to be quantified.
209
    :param int n: The maximum number of times that the provided pattern is to be matched.
210
    :param bool is_greedy: Indicates whether to declare this quantifier as greedy. \
211
        When declared as such, the regex engine will try to match \
212
        the expression as many times as possible. Defaults to ``True``.
213

214
    :raises InvalidArgumentTypeException: 
215
        - Parameter ``pre`` is neither a ``Pregex`` instance nor a string.
216
        - Parameter ``n`` is neither an integer nor ``None``.
217
    :raises InvalidArgumentValueException: Parameter ``n`` has a value of less than zero.
218
    :raises CannotBeRepeatedException: Parameter ``pre`` represents a non-repeatable \
219
        pattern while parameter ``n`` has been set to a value of greater than ``1``.
220

221
    :note: Setting ``n`` equal to ``None`` indicates that there is no upper limit to the number of \
222
        times the pattern is to be repeated.
223
    '''
224

225
    def __init__(self, pre: _Union[_pre.Pregex, str], n: _Optional[int], is_greedy: bool = True) -> _pre.Pregex:
3✔
226
        '''
227
        Matches the provided pattern up to a maximum number of times.
228

229
        :param Pregex | str pre: The pattern that is to be quantified.
230
        :param int n: The maximum number of times that the provided pattern is to be matched.
231
        :param bool is_greedy: Indicates whether to declare this quantifier as greedy. \
232
            When declared as such, the regex engine will try to match \
233
            the expression as many times as possible. Defaults to ``True``.
234

235
        :raises InvalidArgumentTypeException: 
236
            - Parameter ``pre`` is neither a ``Pregex`` instance nor a string.
237
            - Parameter ``n`` is neither an integer nor ``None``.
238
        :raises InvalidArgumentValueException: Parameter ``n`` has a value of less than zero.
239
        :raises CannotBeRepeatedException: Parameter ``pre`` represents a non-repeatable \
240
        pattern while parameter ``n`` has been set to a value of greater than ``1``.
241

242
        :note: Setting ``n`` equal to ``None`` indicates that there is no upper limit to the number of \
243
            times the pattern is to be repeated.
244
        '''
245
        super().__init__(pre, is_greedy, lambda pre, is_greedy: pre.at_most(n, is_greedy))
3✔
246

247

248
class AtLeastAtMost(__Quantifier):
3✔
249
    '''
3✔
250
    Matches the provided expression between a minimum and a maximum number of times.
251

252
    :param Pregex | str pre: The pattern that is to be quantified.
253
    :param int n: The minimum number of times that the provided pattern is to be matched.
254
    :param int m: The maximum number of times that the provided pattern is to be matched.
255
    :param bool is_greedy: Indicates whether to declare this quantifier as greedy. \
256
        When declared as such, the regex engine will try to match \
257
        the expression as many times as possible. Defaults to ``True``.
258

259
    :raises InvalidArgumentTypeException: 
260
        - Parameter ``pre`` is neither a ``Pregex`` instance nor a string.
261
        - Parameter ``n`` is not an integer.
262
        - Parameter ``m`` is neither an integer nor ``None``.
263
    :raises InvalidArgumentValueException:
264
        - Either parameter ``n`` or ``m`` has a value of less than zero.
265
        - Parameter ``n`` has a greater value than that of parameter ``m``.
266
    :raises CannotBeRepeatedException: Parameter ``pre`` represents a non-repeatable \
267
        pattern while parameter ``m`` has been set to a value of greater than ``1``.
268

269
    :note: 
270
        - Parameter ``is_greedy`` has no effect in the case that ``n`` equals ``m``.
271
        - Setting ``m`` equal to ``None`` indicates that there is no upper limit to the \
272
            number of times the pattern is to be repeated.
273
    '''
274

275
    def __init__(self, pre: _Union[_pre.Pregex, str], n: int, m: _Optional[int], is_greedy: bool = True) -> _pre.Pregex:
3✔
276
        '''
277
        Matches the provided expression between a minimum and a maximum number of times.
278

279
        :param Pregex | str pre: The pattern that is to be quantified.
280
        :param int n: The minimum number of times that the provided pattern is to be matched.
281
        :param int m: The maximum number of times that the provided pattern is to be matched.
282
        :param bool is_greedy: Indicates whether to declare this quantifier as greedy. \
283
            When declared as such, the regex engine will try to match \
284
            the expression as many times as possible. Defaults to ``True``.
285

286
        :raises InvalidArgumentTypeException: 
287
            - Parameter ``pre`` is neither a ``Pregex`` instance nor a string.
288
            - Parameter ``n`` is not an integer.
289
            - Parameter ``m`` is neither an integer nor ``None``.
290
        :raises InvalidArgumentValueException:
291
            - Either parameter ``n`` or ``m`` has a value of less than zero.
292
            - Parameter ``n`` has a greater value than that of parameter ``m``.
293
        :raises CannotBeRepeatedException: Parameter ``pre`` represents a non-repeatable \
294
            pattern while parameter ``m`` has been set to a value of greater than ``1``.
295

296
        :note: 
297
            - Parameter ``is_greedy`` has no effect in the case that ``n`` equals ``m``.
298
            - Setting ``m`` equal to ``None`` indicates that there is no upper limit to the \
299
                number of times the pattern is to be repeated.
300
        '''
301
        super().__init__(pre, is_greedy, lambda pre, is_greedy: pre.at_least_at_most(n, m, is_greedy))
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