-
Notifications
You must be signed in to change notification settings - Fork 12
/
calculate.py
313 lines (261 loc) · 10.6 KB
/
calculate.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
from functools import cmp_to_key
import math
import random
import re
from locale import (atof, str as locale_str)
import sublime
import sublime_plugin
def mean(numbers, *more):
if more:
return mean([numbers] + list(more))
return sum(numbers) / len(numbers)
def is_number(view, sel):
try:
substr = view.substr(sel)
atof(substr)
return True
except ValueError:
return False
class SelectionListener(sublime_plugin.EventListener):
"""
If all selections are numbers, show sum and average in status bar
"""
def on_selection_modified_async(self, view):
number_selections = list(filter(lambda sel: is_number(view, sel), view.sel()))
if len(number_selections) <= 1:
return
numbers = [atof(view.substr(sel)) for sel in number_selections]
sublime.status_message("Sum: {:n}\tAverage: {:n}".format(sum(numbers), mean(numbers)))
class CalculateCommand(sublime_plugin.TextCommand):
def __init__(self, *args, **kwargs):
sublime_plugin.TextCommand.__init__(self, *args, **kwargs)
self.dict = {}
for key in dir(random):
self.dict[key] = getattr(random, key)
for key in dir(math):
self.dict[key] = getattr(math, key)
def average(nums):
return sum(nums) / len(nums)
self.dict['avg'] = average
self.dict['average'] = average
def password(length=20):
pwdchrs = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789'
return ''.join(random.choice(pwdchrs) for _ in range(length))
self.dict['pwd'] = password
self.dict['password'] = password
self.dict['mean'] = mean
builtins = {}
for key in [
'abs', 'all', 'any', 'ascii', 'bin', 'bool', 'bytearray', 'bytes',
'callable', 'chr', 'compile', 'complex', 'dict', 'dir', 'divmod', 'enumerate',
'filter', 'float', 'format', 'frozenset', 'getattr', 'hasattr', 'hash',
'hex', 'id', 'int', 'isinstance', 'issubclass', 'iter', 'len', 'list', 'map',
'max', 'memoryview', 'min', 'next', 'object', 'oct', 'ord', 'pow', 'range',
'repr', 'reversed', 'round', 'set', 'slice', 'sorted', 'str', 'sum', 'tuple',
'type', 'vars', 'zip'
]:
try:
builtins[key] = __builtins__[key]
except KeyError:
pass
self.dict['__builtins__'] = builtins
def run(self, edit, **kwargs):
self.dict['i'] = 0
for region in self.view.sel():
try:
error = self.run_each(edit, region, **kwargs)
except Exception as exception:
error = str(exception)
self.dict['i'] = self.dict['i'] + 1
if error:
self.view.show_popup(error)
def calculate(self, formula):
# remove newlines
formula = formula.replace('\n',' ').replace('\r',' ')
# replace × by * and ÷ by /
formula = formula.replace('×', '*').replace('÷', '/')
# remove preceding 0s (preserve hex digits)
formula = re.sub(r'\b(?<![\d\.])0*(\d+)\b', r'\1', formula)
# finally evaluate it
result = eval(formula, self.dict, {})
# convert result to string if needed
if not isinstance(result, str):
result = str(result)
# if result is integer
if result[-2:] == '.0':
result = result[:-2]
return result
def run_each(self, edit, region, replace=False, prompt=True):
if not region.empty():
formula = self.view.substr(region)
value = self.calculate(formula)
if not replace:
value = "%s = %s" % (formula, value)
self.view.replace(edit, region, value)
elif prompt == 'always':
self.get_formula()
else:
line = self.view.line(region.a)
formula = self.view.substr(line)
try:
value = self.calculate(formula)
except SyntaxError as ex:
if prompt and len(self.view.sel()) == 1:
self.get_formula()
else:
raise ex
else:
self.view.sel().subtract(region)
if not replace:
value = " = %s" % (value)
self.view.replace(edit, sublime.Region(line.b, line.b), value)
else:
self.view.replace(edit, line, value)
line = self.view.line(line.a)
self.view.sel().add(sublime.Region(line.b, line.b))
def get_formula(self):
self.view.window().show_input_panel('Calculate:', '', self.on_calculate, None, None)
def on_calculate(self, formula):
value = self.calculate(formula)
self.view.run_command('insert_snippet', {'contents': '${0:%s}' % value})
class CalculateCountCommand(sublime_plugin.TextCommand):
def run(self, edit, index=1):
def generate_integer_counter(initial):
def count():
offset = initial
while True:
yield str(offset)
offset += 1
return iter(count()).__next__
def generate_hexadecimal_counter(initial):
def count():
offset = initial
while True:
yield u"0x%x" % offset
offset += 1
return iter(count()).__next__
def generate_octal_counter(initial):
def count():
offset = initial
while True:
yield u"0%o" % offset
offset += 1
return iter(count()).__next__
def generate_string_counter(initial):
if re.match('[a-z]+$', initial):
maxord = ord('z')
letter = 'a'
else:
maxord = ord('Z')
letter = 'A'
def count():
offset = initial
while True:
yield offset
up = 1 # increase last character
while True:
o = ord(offset[-up])
o += 1
tail = ''
if up > 1:
tail = offset[-up + 1:]
if o > maxord:
offset = offset[:-up] + letter + tail
up += 1
if len(offset) < up:
offset = letter + offset
break
else:
offset = offset[:-up] + chr(o) + tail
break
return iter(count()).__next__
def generate_plusminus_counter(initial):
def count():
offset = initial
while True:
yield str(offset)
if offset == '-':
offset = '+'
else:
offset = '-'
return iter(count()).__next__
is_first = True
subs = []
for region in self.view.sel():
if is_first:
# see if the region is a number or alphanumerics
content = self.view.substr(region)
if re.match('0[xX][0-9a-fA-F]+$', content):
counter = generate_hexadecimal_counter(int(content[2:], 16))
elif re.match('0[oO]?[0-7]+$', content):
if re.match('0[oO]', content):
start = 2
else:
start = 1
counter = generate_octal_counter(int(content[start:], 8))
elif re.match('[+-]?[0-9]+$', content):
counter = generate_integer_counter(int(content))
elif re.match('[A-Za-z]+$', content):
counter = generate_string_counter(content)
elif re.match('[+-]+$', content):
counter = generate_plusminus_counter(content)
else:
counter = generate_integer_counter(index)
subs.append((region, str(counter())))
is_first = False
# any edits that are performed will happen in reverse; this makes it
# easy to keep region.a and region.b pointing to the correct locations
def get_end(region_tuple):
return region_tuple[0].end()
subs.sort(key=get_end, reverse=True)
for sub in subs:
self.view.sel().subtract(sub[0])
self.view.replace(edit, sub[0], sub[1])
self.view.sel().add(sublime.Region(sub[0].begin() + len(sub[1]), sub[0].begin() + len(sub[1])))
class CalculateMathCommand(sublime_plugin.TextCommand):
def run(self, edit):
insert_region = None
numbers = []
for region in self.view.sel():
if not region:
insert_region = region
else:
try:
only_number = self.view.substr(region).replace('$', '')
number = atof(only_number)
except ValueError:
pass
numbers.append(number)
result = repr(self.operation(numbers))
if insert_region is None:
# self.view.show_popup('Select an empty region where the output will be inserted')
self.view.show_popup(result)
else:
self.view.replace(edit, insert_region, result)
def operation(self, numbers):
raise Exception("Implement 'def operation(self, numbers)' in {}".format(type(self).__name__))
class CalculateAddCommand(CalculateMathCommand):
def operation(self, numbers):
return sum(numbers)
class CalculateMeanCommand(CalculateMathCommand):
def operation(self, numbers):
return mean(numbers)
class CalculateIncrementCommand(sublime_plugin.TextCommand):
DELTA = 1
def run(self, edit):
for region in self.view.sel():
if not region:
start = end = region.a
while is_number(self.view, sublime.Region(start - 1, start)):
start -= 1
while is_number(self.view, sublime.Region(end, end + 1)):
end += 1
region = sublime.Region(start, end)
try:
number = atof(self.view.substr(region))
except ValueError:
continue
number += self.DELTA
self.view.replace(edit, region, locale_str(number))
class CalculateDecrementCommand(CalculateIncrementCommand):
DELTA = -1