|
1 | 1 | from math import *
|
2 | 2 | from random import *
|
| 3 | +from hashlib import md5 as _md5 |
| 4 | +from hashlib import sha1 as _sha1 |
| 5 | +from collections import Counter |
| 6 | +import datetime |
3 | 7 | import re
|
4 |
| -import sublime, sublime_plugin |
| 8 | +import sublime |
| 9 | +import sublime_plugin |
| 10 | + |
| 11 | + |
| 12 | +def dnow(): |
| 13 | + return datetime.datetime.strftime(datetime.datetime.now(), '%d/%m/%Y') |
| 14 | + |
| 15 | + |
| 16 | +def tnow(): |
| 17 | + return datetime.datetime.strftime(datetime.datetime.now(), '%H:%M:%S') |
| 18 | + |
| 19 | + |
| 20 | +def dtnow(): |
| 21 | + return datetime.datetime.strftime(datetime.datetime.now(), '%d/%m/%Y %H:%M:%S') |
| 22 | + |
| 23 | + |
| 24 | +def _hashfunc(hashfunc, obj): |
| 25 | + s = str(obj) |
| 26 | + h = hashfunc(s.encode('utf-8')) |
| 27 | + return h.hexdigest() |
| 28 | + |
| 29 | + |
| 30 | +def md5(obj): |
| 31 | + return _hashfunc(_md5, obj) |
| 32 | + |
| 33 | + |
| 34 | +def sha1(obj): |
| 35 | + return _hashfunc(_sha1, obj) |
| 36 | + |
| 37 | + |
| 38 | +def set_intersect(itr0, itr1): |
| 39 | + s0, s1 = set(itr0), set(itr1) |
| 40 | + return s0.intersection(s1) |
| 41 | + |
| 42 | + |
| 43 | +def set_difference(itr0, itr1): |
| 44 | + s0, s1 = set(itr0), set(itr1) |
| 45 | + return s0 - s1 |
| 46 | + |
| 47 | + |
| 48 | +def set_symdiff(itr0, itr1): |
| 49 | + s0, s1 = set(itr0), set(itr1) |
| 50 | + return s0.symmetric_difference(s1) |
| 51 | + |
| 52 | + |
| 53 | +def formatnum(num, digits, scientificNotation=None): |
| 54 | + |
| 55 | + def normalFormatting(num, digits): |
| 56 | + return ('{:.%df}' % digits).format(num) |
| 57 | + |
| 58 | + def scientificFormatting(num, digits): |
| 59 | + return ('{:.%de}' % digits).format(num) |
| 60 | + |
| 61 | + if scientificNotation is False: |
| 62 | + return normalFormatting(num, digits) |
| 63 | + |
| 64 | + if scientificNotation is True: |
| 65 | + return scientificFormatting(num, digits) |
| 66 | + |
| 67 | + if scientificNotation is None: |
| 68 | + scientificNotation = 8 |
| 69 | + |
| 70 | + if isinstance(scientificNotation, int): |
| 71 | + if len(normalFormatting(num, digits)) > scientificNotation: |
| 72 | + return scientificFormatting(num, digits) |
| 73 | + return normalFormatting(num, digits) |
| 74 | + |
| 75 | + |
| 76 | +# reverse() in python3 |
| 77 | +def rev(s): |
| 78 | + return s[::-1] |
5 | 79 |
|
6 |
| -# reverse() in python3 |
7 |
| -def rev(s): return s[::-1] |
8 | 80 |
|
9 | 81 | class Minipy_evalCommand(sublime_plugin.TextCommand):
|
10 |
| - def run(self, edit, user_input=None): |
11 |
| - self.edit = edit |
12 |
| - view = self.view |
13 |
| - script = "" |
14 |
| - |
15 |
| - # sum the total number of special char $ |
16 |
| - total = 0 |
17 |
| - for region in view.sel(): |
18 |
| - total += view.substr(region).count("$") |
19 |
| - |
20 |
| - print("total = ", total) |
21 |
| - # build a list from 1 to the number of special chars |
22 |
| - serial_number = list(range(1, total+1)) |
23 |
| - # serial_number.reverse() |
24 |
| - serial_number = rev(serial_number) |
25 |
| - print(repr(serial_number)) |
26 |
| - |
27 |
| - # replace each special char $ with the next index from serial_number list |
28 |
| - # and eval the expression |
29 |
| - for region in view.sel(): |
30 |
| - if region.begin() != region.end(): |
31 |
| - script = view.substr(region) |
32 |
| - for n in range(script.count("$")): |
33 |
| - script = re.sub(r"\$", str(serial_number.pop()), script, 1) |
34 |
| - # print(eval(script)) |
35 |
| - view.replace(edit, region, str(eval(script))) |
| 82 | + def run(self, edit, user_input=None): |
| 83 | + self.edit = edit |
| 84 | + view = self.view |
| 85 | + script = "" |
| 86 | + |
| 87 | + # sum the total number of special char $ |
| 88 | + total = 0 |
| 89 | + for region in view.sel(): |
| 90 | + total += view.substr(region).count("$") |
| 91 | + |
| 92 | + print("total = ", total) |
| 93 | + # build a list from 1 to the number of special chars |
| 94 | + serial_number = list(range(1, total + 1)) |
| 95 | + # serial_number.reverse() |
| 96 | + serial_number = rev(serial_number) |
| 97 | + print(repr(serial_number)) |
| 98 | + |
| 99 | + # replace each special char $ with the next index from serial_number list |
| 100 | + # and eval the expression |
| 101 | + for region in view.sel(): |
| 102 | + if region.begin() != region.end(): |
| 103 | + script = view.substr(region) |
| 104 | + for n in range(script.count("$")): |
| 105 | + script = re.sub(r"\$", str(serial_number.pop()), script, 1) |
| 106 | + # print(eval(script)) |
| 107 | + view.replace(edit, region, str(eval(script))) |
0 commit comments