forked from cool-RR/python_toolbox
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcute_range.py
More file actions
237 lines (182 loc) · 7.66 KB
/
cute_range.py
File metadata and controls
237 lines (182 loc) · 7.66 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
# Copyright 2009-2017 Ram Rachum.
# This program is distributed under the MIT license.
import abc
import builtins
import types
import collections
import numbers
from python_toolbox import caching
from .misc import CuteSequence
infinity = float('inf')
infinities = (infinity, -infinity)
NoneType = type(None)
def parse_range_args(*args):
assert 0 <= len(args) <= 3
if len(args) == 0:
return (0, infinity, 1)
elif len(args) == 1:
(stop,) = args
if stop == -infinity: raise TypeError
elif stop is None: stop = infinity
return (0, stop, 1)
elif len(args) == 2:
(start, stop) = args
if start in infinities: raise TypeError
elif start is None: start = 0
if stop == -infinity: raise TypeError
elif stop is None: stop = infinity
return (start, stop, 1)
else:
assert len(args) == 3
(start, stop, step) = args
if step == 0: raise TypeError
if start in infinities:
raise TypeError(
f"Can't have `start={start}` because then what would the "
f"first item be, {start}? And the second item, {start + 1}? "
f"No can do."
)
if step in infinities:
raise TypeError(
f"Can't have `step={step}` because then what would the second "
f"item be, {step}? No can do."
)
elif start is None: start = 0
elif step > 0:
if stop == -infinity: raise TypeError
elif stop is None: stop = infinity
else:
assert step < 0
if stop == infinity: raise TypeError
elif stop is None: stop = (-infinity)
return (start, stop, step)
def _is_integral_or_none(thing):
return isinstance(thing, (numbers.Integral, NoneType))
class CuteRange(CuteSequence):
'''
Improved version of Python's `range` that has extra features.
`CuteRange` is like Python's built-in `range`, except (1) it's cute and (2)
it's completely different. LOL, just kidding.
`CuteRange` takes `start`, `stop` and `step` arguments just like `range`,
but it allows you to use floating-point numbers (or decimals), and it
allows you to use infinite numbers to produce infinite ranges.
Obviously, `CuteRange` allows iteration, index access, searching for a
number's index number, checking whether a number is in the range or not,
and slicing.
Examples:
`CuteRange(float('inf'))` is an infinite range starting at zero and
never ending.
`CuteRange(7, float('inf'))` is an infinite range starting at 7 and
never ending. (Like `itertools.count(7)` except it has all the
amenities of a sequence, you can get items using list notation, you can
slice it, you can get index numbers of items, etc.)
`CuteRange(-1.6, 7.3)` is the finite range of numbers `(-1.6, -0.6,
0.4, 1.4, 2.4, 3.4, 4.4, 5.4, 6.4)`.
`CuteRange(10.4, -float('inf'), -7.1)` is the infinite range of numbers
`(10.4, 3.3, -3.8, -10.9, -18.0, -25.1, ... )`.
'''
def __init__(self, *args):
self.start, self.stop, self.step = parse_range_args(*args)
_reduced = property(lambda self: (type(self), (self.start, self.stop,
self.step)))
__hash__ = lambda self: hash(self._reduced)
__eq__ = lambda self, other: (type(self) == type(other) and
(self._reduced == other._reduced))
distance_to_cover = caching.CachedProperty(lambda self:
self.stop - self.start)
@caching.CachedProperty
def length(self):
'''
The length of the `CuteRange`.
We're using a property `.length` rather than the built-in `__len__`
because `__len__` can't handle infinite values or floats.
'''
from python_toolbox import math_tools
if math_tools.get_sign(self.distance_to_cover) != \
math_tools.get_sign(self.step):
return 0
else:
raw_length, remainder = math_tools.cute_divmod(
self.distance_to_cover, self.step
)
raw_length += (remainder != 0)
return raw_length
__repr__ = lambda self: self._repr
@caching.CachedProperty
def _repr(self):
return '%s(%s%s%s)' % (
type(self).__name__,
f'{self.start}, ',
str(self.stop),
f', {self.step}' if self.step != 1 else '',
)
@caching.CachedProperty
def short_repr(self):
'''
A shorter representation of the `CuteRange`.
This is different than `repr(cute_range)` only in cases where `step=1`.
In these cases, while `repr(cute_range)` would be something like
`CuteRange(7, 20)`, `cute_range.short_repr` would be `7..20`.
'''
if self.step != 1:
return self._repr
else:
return f'{self.start}..{self.stop - 1}'
def __getitem__(self, i, allow_out_of_range=False):
from python_toolbox import sequence_tools
if isinstance(i, numbers.Integral):
if i < 0:
if i < (-self.length) and not allow_out_of_range:
raise IndexError
i += self.length
if 0 <= i < self.length or allow_out_of_range:
return self.start + (self.step * i)
else:
raise IndexError
elif i == infinity:
if self.length == infinity:
return self.stop
else:
raise IndexError
elif i == -infinity:
raise IndexError
elif isinstance(i, (slice, sequence_tools.CanonicalSlice)):
canonical_slice = sequence_tools.CanonicalSlice(
i, iterable_or_length=self
)
if not ((0 <= canonical_slice.start <= self.length) and
((0 <= canonical_slice.stop <= self.length) or
(canonical_slice.stop == self.length == infinity))):
raise TypeError
return CuteRange(
self.__getitem__(canonical_slice.start,
allow_out_of_range=True),
self.__getitem__(canonical_slice.stop,
allow_out_of_range=True),
self.step * canonical_slice.step
)
else:
raise TypeError
def __len__(self):
# Sadly Python doesn't allow infinity or floats here.
return self.length if isinstance(self.length, numbers.Integral) else 0
def index(self, i, start=-infinity, stop=infinity):
'''Get the index number of `i` in this `CuteRange`.'''
from python_toolbox import math_tools
if not isinstance(i, numbers.Number):
raise ValueError
else:
distance = i - self.start
if distance == 0 and self:
if start <= 0 < stop: return 0
else: raise ValueError("Found but not within range.")
if math_tools.get_sign(distance) != math_tools.get_sign(self.step):
raise ValueError
index, remainder = math_tools.cute_divmod(distance, self.step)
if remainder == 0 and (0 <= index < self.length or
index == self.length == infinity):
if start <= index < stop: return index
else: raise ValueError("Found but not within range.")
else:
raise ValueError
is_infinite = caching.CachedProperty(lambda self: self.length == infinity)