forked from ryanhaining/lazylist
-
Notifications
You must be signed in to change notification settings - Fork 0
/
iterlist.py
412 lines (337 loc) · 12.1 KB
/
iterlist.py
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
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
"""iterlist is a list-like interface for iterables."""
# pylint: disable=C0103,R0205
try:
from collections.abc import Sequence
except ImportError:
# python 2 compatible
from collections import Sequence
import itertools
import threading
izip = getattr(itertools, "izip", zip) # python2 compatible iter zip
try:
from typing import (
Any,
Callable,
ContextManager,
Iterable,
Iterator,
List,
Optional,
Union,
)
except ImportError:
pass # typing is only used for static analysis
class ConcurrentGeneratorAccess(ValueError):
"""Raised when stepping a generator that is already executing"""
class CachedIterator(object):
"""a tuple-like interface over an iterable that stores iterated values."""
def __init__(self, iterable):
# type: (Iterable) -> None
"""Initialize
:type iterable: Iterable
"""
self._iterable = iter(iterable)
self._list = list() # type: List[Any]
def _positive_index(self, index):
# type: (int) -> int
"""Private
If index is positive, it is returned.
If index is negative, it is converted to a positive index referring to
the same position
:rtype: int
:return: positive list index for index
:raise: IndexError if the magnitude of index is greater than the length
of the iterable
"""
if index >= 0:
return index
self._consume_rest()
pos = len(self._list) - abs(index)
if pos < 0:
raise IndexError("list index out of range")
return pos
@staticmethod
def _warn_concurrent_access(exc):
# type: (BaseException) -> None
if "generator already executing" in str(exc):
# MJF: use raise from when py27 support is dropped
raise ConcurrentGeneratorAccess(
"Concurrent access to iterable detected. When using this interface"
"in a multithreaded environment, use ThreadsafeIterTuple, "
"ThreadsafeIterList, or mix in LockingCachedIterator ahead of "
"IterTuple or IterList bases.\nOriginal Exception: {}".format(exc),
)
def _consume_next(self):
# type: () -> None
try:
self._list.append(next(self._iterable))
except StopIteration:
raise IndexError
except ValueError as ve:
self._warn_concurrent_access(ve)
raise # pragma: no cover
def _consume_rest(self):
# type: () -> None
try:
self._list.extend(self._iterable)
except ValueError as ve:
self._warn_concurrent_access(ve)
raise # pragma: no cover
def _consume_up_to_index(self, index):
# type: (int) -> None
if index < 0:
self._consume_rest()
return
to_consume = index - len(self._list) + 1
for _ in range(to_consume):
self._consume_next()
def _consume_up_to_slice(self, sl):
# type: (slice) -> None
consume_to = None
start, stop, step = sl.start, sl.stop, sl.step
if start is None:
start = 0
if stop is None:
stop = 0
if step is None:
step = 1
if sl.start is None and sl.stop is None:
# full slice
consume_to = -1
elif start < 0 or stop < 0:
# negative slice, must consume the whole iterator
consume_to = min(start, stop)
elif step > 0 and sl.stop is None:
# positive slice, no end specified
consume_to = -1
elif step > 0 and stop > start:
# positive slice
consume_to = stop - ((stop - start) % step) - 1
elif step < 0 and stop < start:
# reversed slice will always include the item at index start
consume_to = start
else:
# empty slice, nothing to consume
pass
self._consume_up_to(consume_to)
def _consume_up_to(self, key):
# type: (Optional[Union[slice, int]]) -> None
if key is None:
return
if isinstance(key, slice):
self._consume_up_to_slice(key)
else:
self._consume_up_to_index(key)
def __getitem__(self, index):
# type: (Union[slice, int]) -> Any
self._consume_up_to(index)
return self._list[index]
def __len__(self):
# type: () -> int
self._consume_rest()
return len(self._list)
def __bool__(self):
# type: () -> bool
if self._list:
return True
try:
self._consume_next()
return True
except IndexError:
return False
__nonzero__ = __bool__
def __repr__(self):
# type: () -> str
return repr(tuple(self))
def __eq__(self, other):
# type: (Any) -> bool
if not isinstance(other, (CachedIterator, Sequence)):
return False
return all(a == b for a, b in izip(self, other)) and len(self) == len(other)
def __ne__(self, other):
# type: (Any) -> bool
# python 2 requires __ne__ or assumes no object is equal
return not self == other
def __lt__(self, other):
# type: (Any) -> bool
for a, b in izip(self, other):
if b < a:
return False
if a < b:
return True
# at this point all elements in both lists are equal
# in this case, the shorter list is considered less
# NOTE: calling len will consume the remainder of both iterables
return len(self) < len(other)
def index(self, item, start=0, stop=None):
# type: (Any, int, Optional[int]) -> int
"""Return first index of item.
Raises ValueError if the value is not present.
"""
start = self._positive_index(start)
if stop:
stop = self._positive_index(stop)
for i, e in enumerate(itertools.islice(self, start, stop)):
if e == item:
return i + start
raise ValueError("{} is not in list".format(item))
def count(self, item):
# type: (Any) -> int
"""Return number of occurrences of item.
Note: this will consume the entire iterable
"""
self._consume_rest()
return self._list.count(item)
def __iter__(self):
# type: () -> Iterator[Any]
ix = 0
try:
# Use a while loop over the list index to ensure all items are
# yielded, even if some of the iterable is consumed while __iter__
# is stopped
while True:
self._consume_up_to_index(ix)
yield self._list[ix]
ix += 1
except IndexError:
return
class IterTuple(CachedIterator):
"""a tuple-like interface over an iterable that stores iterated values."""
def __eq__(self, other):
# type: (Any) -> bool
if not isinstance(other, (tuple, IterTuple)):
return False
return super(IterTuple, self).__eq__(other)
def __lt__(self, other):
# type: (Any) -> bool
if not isinstance(other, (tuple, IterTuple)):
raise TypeError(
"'<' not supported between instances of {!r} and {!r}".format(
type(self).__name__, type(other).__name__,
),
)
return super(IterTuple, self).__lt__(other)
class IterList(CachedIterator):
"""a mutable list-like interface over an iterable that stores iterated values."""
def __repr__(self):
# type: () -> str
return repr(list(self))
def __eq__(self, other):
# type: (Any) -> bool
if not isinstance(other, (list, IterList)):
return False
return super(IterList, self).__eq__(other)
def __lt__(self, other):
# type: (Any) -> bool
if not isinstance(other, (list, IterList)):
raise TypeError(
"'<' not supported between instances of {!r} and {!r}".format(
type(self).__name__, type(other).__name__,
),
)
return super(IterList, self).__lt__(other)
def __setitem__(self, index, value):
# type: (Union[slice, int], Any) -> None
self._consume_up_to(index)
self._list[index] = value
def __delitem__(self, index):
# type: (Union[slice, int]) -> None
self._consume_up_to(index)
del self._list[index]
def extend(self, rest):
# type: (Iterable) -> None
"""Extend the list with an iterable."""
self._iterable = itertools.chain(self._iterable, iter(rest))
def __iadd__(self, rest):
# type: (Iterable) -> IterList
self.extend(rest)
return self
def remove(self, item):
# type: (Any) -> None
"""Remove first occurrence of item.
Raises ValueError if the value is not present.
"""
del self[self.index(item)]
def insert(self, index, item):
# type: (int, Any) -> None
"""Insert item before index."""
self._consume_up_to(index)
self._list.insert(index, item)
def append(self, item):
# type: (Any) -> None
"""Append item to end.
Note: this will consume the entire iterable
"""
self._consume_rest()
self._list.append(item)
def clear(self):
# type: () -> None
"""Clear the list
Any unevaluated parts of the list will not be evaluated. This
behavior may produce unexpected results if the evaluation of
the remaining items has side effects.
"""
del self._list[:] # self._list.clear() for py3.3+
self._iterable = iter([])
def sort(self, key=None, reverse=False):
# type: (Optional[Callable[[Any], bool]], bool) -> None
"""Stable sort in-place.
Note: this will consume the entire iterable
"""
self._consume_rest()
self._list.sort(key=key, reverse=reverse)
def reverse(self):
# type: () -> None
"""Reverse in-place.
Note: this will consume the entire iterable
"""
self._consume_rest()
self._list.reverse()
def pop(self, index=-1):
# type: (int) -> Any
"""Remove and return item at index (default last).
Raises IndexError if list is empty or index is out of range.
"""
self._consume_up_to(index)
item = self._list[index]
del self._list[index]
return item
class LockingCachedIterator(CachedIterator):
"""protect CachedIterator generator execution with an RLock"""
@staticmethod
def lock_factory():
# type: () -> ContextManager[Any]
"""
Return a contextmanager-like lock implementation.
The default lock is threading.RLock.
Subclasses may use a different lock implementation as long as it
follows contextmanager protocol and is re-entrant.
:return: the lock used to protect generator access
"""
return threading.RLock()
def __init__(self, iterable):
# type: (Iterable) -> None
"""Initialize
:type iterable: Iterable
"""
super(LockingCachedIterator, self).__init__(iterable)
self._lock = self.lock_factory()
def _consume_next(self):
# type: () -> None
with self._lock:
super(LockingCachedIterator, self)._consume_next()
def _consume_rest(self):
# type: () -> None
with self._lock:
super(LockingCachedIterator, self)._consume_rest()
def _consume_up_to_index(self, index):
# type: (int) -> None
with self._lock:
super(LockingCachedIterator, self)._consume_up_to_index(index)
class ThreadsafeIterTuple(LockingCachedIterator, IterTuple):
"""IterTuple which can be safely accessed from multiple threads"""
class ThreadsafeIterList(LockingCachedIterator, IterTuple):
"""IterList which can be safely accessed from multiple threads.
Note that regular list manipulations are NOT protected by lock, only generator
access is protected
"""