-
Notifications
You must be signed in to change notification settings - Fork 1
/
circularlist.py
320 lines (271 loc) · 12.2 KB
/
circularlist.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
import itertools
import re
from pprint import PrettyPrinter
from mypprint import MyPrettyPrinter
from collections import defaultdict
from exectools import execfile, _import, make_refresh
from attrdict1 import SimpleAttrDict
# _import('from sfida.sf_string_between import *')
# _import('from hotkey_utils import stutter_chunk')
# execfile('hotkey_utils')
import os
refresh_circular = make_refresh(os.path.abspath(__file__))
refresh = make_refresh(os.path.abspath(__file__))
def re_search(pattern, string, flags=0):
try:
return re.search(pattern, string, flags)
except Exception as e:
print("{}: {}: in pattern-{} {} processing-{} {}".format(e.__class__.__name__, str(e), type(pattern), pattern, type(string), string))
raise
def strip_pattern(pattern):
if pattern.endswith(('++', '**', '++?', '**?')):
multi = True
greedy = True
if pattern.endswith('?'):
greedy = False
pattern = pattern[0:-1]
if pattern.endswith('++'):
min = 1
elif pattern.endswith('**'):
min = 0
pattern = pattern[0:-2]
return pattern
class CircularList(object):
"""
https://stackoverflow.com/questions/4151320/efficient-circular-buffer/40784706#40784706
"""
def __init__(self, size, data=[]):
"""Initialization"""
if callable(getattr(size, 'append', None)):
self.size = len(size)
self._data = [x for x in size]
else:
self.size = size
self._data = list(data)[-size:]
self.end = len(self._data) % self.size
self.on_remove = None
def clear(self):
self.__init__(self.size)
def extend(self, data):
self._data.extend(data)
self._data = self._data[-self.size:]
self.end = len(self._data) % self.size
def resize(self, size):
self.__init__(size, self.as_list())
def copy(self):
return type(self)(self.size, self.as_list())
def restart(self, size):
self.__init__(self.size, self.as_list()[0:size])
def pop(self, index=-1):
"""
Remove and return item at index (default last).
Raises IndexError if list is empty or index is out of range.
"""
if not self._data:
raise IndexError("pop from empty list")
try:
idx = (index + self.end) % self.size # len(self._data)
result = self._data.pop(idx)
self.end = (self.end - 1) % self.size
return result
except IndexError as ex:
print(("Exception: ", ex))
print(("index", index, "size", len(self._data)))
raise IndexError(ex)
def index(self, value):
try:
idx = self._data.index(value)
idx = (idx - self.end) % self.size
except ValueError:
idx = -1
return idx
def remove(self, value):
idx = self.index(value)
if ~idx:
self.pop(idx)
def append(self, value):
"""Append an element"""
if len(self._data) == self.size:
if callable(self.on_remove):
self.on_remove(self._data[self.end])
self._data[self.end] = value
else:
self._data.insert(self.end, value)
self.end = (self.end + 1) % self.size
def __iter__(self):
for v in self.as_list():
yield v
def pattern_transform(self, pattern):
return string_between('({', '}', pattern, inclusive=1, repl= \
lambda v: '(?P<{}>'.format(string_between('({', '}', v)))
def multimatch(self, pattern_list, flags=0, predicate=None, iteratee=None, gettext=None, anchor='end', groupiter=None):
if gettext is None:
gettext=lambda o: o if isinstance(o, str) else o.insn
# if groupiter is None:
# groupiter = lambda o: o.text
matches = []
group_matches = defaultdict(list)
group_matches['default'] = matches
last_pattern = self.pattern_transform(pattern_list[-1])
if anchor == 'end':
if not re_search(strip_pattern(last_pattern), call_if_callable(gettext, self[-1], default=self[-1]), flags):
return None
pattern_iter = iter(stutter_chunk([self.pattern_transform(x) for x in pattern_list], 2, 1))
_pattern, peek = next(pattern_iter)
pattern_count = 0
pattern_size = len(pattern_list)
bsize = len(self._data)
repetitions = defaultdict(list)
i = -1
while i+1 < bsize:
i += 1
line = self[i]
if not call_if_callable(predicate, line, default=True):
continue
text = call_if_callable(gettext, line, default=line)
line = call_if_callable(iteratee, line, default=line)
multi = greedy = False
min = 1
pattern = _pattern
if pattern.endswith(('++', '**', '++?', '**?')):
multi = True
greedy = True
if pattern.endswith('?'):
greedy = False
pattern = pattern[0:-1]
if pattern.endswith('++'):
min = 1
elif pattern.endswith('**'):
min = 0
pattern = pattern[0:-2]
if debug:
# dprint("[multimatch] pattern, multi, greedy, line, i, bsize")
print("[multimatch] pattern:{}, multi:{}, greedy:{}, line:{}, i:{}, bsize:{}".format(pattern, multi, greedy, line, i, bsize))
m = re_search(pattern, text, flags)
matchline = line
if multi and not greedy and peek:
mpeek = re_search(peek, text, flags)
else:
mpeek = None
if not m:
if debug: print("[fail-] m:{}, multi:{}, min:{}, pattern:{}, line:{}".format(False, multi, min, pattern, text))
if multi and len(repetitions[pattern_count]) >= min:
try:
_pattern, peek = next(pattern_iter)
# dprint("[multi] pattern")
# print("[multi] pattern:{}".format(pattern))
pattern_count += 1
i -= 1
continue
except StopIteration:
pattern_count += 1
if i == bsize - 1:
if len(group_matches):
return SimpleAttrDict(group_matches)
return matches
else:
return None
elif m and mpeek and multi and not greedy and len(repetitions[pattern_count]) >= min:
if debug: print("m,multi,not greedy")
# dprint("[multimatch] greedy, multi, line")
# print("[multimatch] greedy:{}, multi:{}, line:{}".format(greedy, multi, line))
m = mpeek
try:
_pattern, peek = next(pattern_iter)
pattern = _pattern
pattern_count += 1
i -= 1
continue
except StopIteration:
pattern_count += 1
# dprint("[multimatch] ")
if debug: print("[multimatch] :stopiter".format())
if i == bsize - 1:
if len(group_matches):
return SimpleAttrDict(group_matches)
return matches
# dprint("[multi-peek-advance] m")
# print("[multi-peek-advance] m:{}".format(m))
pattern_count += 1
if m:
# dprint("[multi] multi, min, pattern")
if debug: print("[multi] m:{}, multi:{}, min:{}, pattern:{}, text:{}".format(True, multi, min, pattern, text))
matches.append(matchline)
# dprint("[multi] m")
# print("[either] m:{}".format(m))
for k, v in m.groupdict().items():
group_matches[k].append( call_if_callable(groupiter, matchline, default=v) )
group_matches[k + "_insn"].append(line)
# dprint("[multi] k, v")
# print("[either] k:{}, v:{}".format(k, v))
if multi:
repetitions[pattern_count].append(matchline)
# dprint("[multi] repetitions[pattern_count]")
# print("[multi] repetitions[pattern_count]:{}".format(repetitions[pattern_count]))
elif not multi:
try:
_pattern, peek = next(pattern_iter)
# print("[non-multi] pattern:{}".format(pattern))
pattern_count += 1
# i -= 1
continue
except StopIteration:
if debug: print("multistopiter")
if i == bsize - 1:
if len(group_matches):
return SimpleAttrDict(group_matches)
return matches
if debug: print("not matched as there are {} items remaining".format(bsize - i - 1))
if multi and len(repetitions[pattern_count]) >= min:
print("[multimatch] adding 1 for repeating pattern at end")
pattern_count += 1
if debug:
print("[multimatch] fell through: i:{}, bsize:{}, pattern_count:{}, pattern_size:{}".format(i, bsize, pattern_count, pattern_size))
if pattern_count < pattern_size:
# dprint("[multimatch] pattern_list[pattern_count]")
print("[multimatch] pattern_list[pattern_count]:{}".format(pattern_list[pattern_count]))
if pattern_count >= pattern_size:
if debug:
print("but saved by being at end of pattern_list")
# dprint("[multimatch] i, bsize, pattern_count, pattern_size")
if len(group_matches):
return SimpleAttrDict(group_matches)
return matches
return None
def as_list(self):
return self._data[self.end:] + self._data[:self.end]
def __len__(self):
return len(self._data)
def __getitem__(self, key):
"""Get element by end, relative to the current end"""
# https://stackoverflow.com/questions/2936863/implementing-slicing-in-getitem
if isinstance(key, slice):
# Get the start, stop, and step from the slice
return [self[ii] for ii in range(*key.indices(len(self)))]
try:
if len(self._data) == self.size:
idx = (key + self.end) % self.size
else:
idx = key % len(self._data)
return self._data[idx]
# if len(self._data) == self.size:
# return self._data[(key + self.end) % self.size]
# else:
# return self._data[key]
except IndexError as ex:
print(("Exception: ", ex))
print(("key", key, "size", len(self._data)))
raise IndexError(ex)
def __repr__(self):
"""Return string representation"""
return 'Circular List: ' + self.as_list().__repr__() + ' (' + str(len(self._data)) + 'out of ' + str(
self.size) + ' items)'
# produce pprint compatible object, easy as pie!
def __pprint_repr__(self, *args, **kwargs):
return { 'CircularList': self.as_list() }
# to take total control (python 3)
def __pprint__(self, object, stream, indent, allowance, context, level):
stream.write('CircularList(\n')
self._format(object._data, stream, indent, allowance + 1, context, level)
stream.write(')')
pass