Skip to content

Commit 266b335

Browse files
authored
Merge pull request #3 from AllanLRH/master
Added functionality
2 parents cd909b7 + 91a6770 commit 266b335

File tree

2 files changed

+186
-30
lines changed

2 files changed

+186
-30
lines changed

MiniPy.py

Lines changed: 101 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,107 @@
11
from math import *
22
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
37
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]
579

6-
# reverse() in python3
7-
def rev(s): return s[::-1]
880

981
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)))

README.md

Lines changed: 85 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,22 @@
22

33
Sublime Text 3 plugin - inline python evaluation.
44

5+
## Functionality
6+
7+
### As a calculator
8+
59
For example you can write 3.14*0.6 and get the result (1.884) in your text.
610
It also supports multiple selection.
711

12+
### Incremnt counter at cursor positions
13+
814
Another feature is the use of $ as accumolation variable, e.g. having the following multipe selection:
915

1016
arr[$]
1117
arr[$]
1218
arr[$]
1319

14-
will result with
20+
will result with
1521

1622
arr[1]
1723
arr[2]
@@ -23,6 +29,84 @@ similarly:
2329
arr[$+2] -> arr[3]
2430
arr[$*3] arr[6]
2531

32+
### General Python evalueator
33+
34+
Besides that, you have the following imports avaiable:
35+
36+
from math import *
37+
from random import *
38+
from collections import Counter
39+
import datetime
40+
import re # though you should probably use the build in regex features of ST instead.
41+
42+
So you can do:
43+
44+
Counter(('Ann', 'Bob', 'Bob', 'Michael')) -> Counter({'Bob': 2, 'Ann': 1, 'Michael': 1})
45+
Counter(('Ann', 'Bob', 'Bob', 'Michael', 'michael')) -> Counter({'Bob': 2, 'Ann': 1, 'michael': 1, 'Michael': 1})
46+
Counter(name.title() for name in ('Ann', 'Bob', 'Bob', 'Michael', 'michael')) -> Counter({'Bob': 2, 'Michael': 2, 'Ann': 1})
47+
48+
### Computing checksums
49+
50+
And the functions `md5` and `sha1` returns the correspondingly hex-digest of the stringified version of the inputs, e.g. `md5(['foo', 'bar', 'baz']) = dbb432a3f0ac1a2687911715dfbf7502`. Notice that it's possible to hash the list _because it's the string-representation of the list which are being hashed!_
51+
52+
The python `hashlib.md5` and `hashlib.sha1` functions are avaiable under the names `_md5` and `_sha1`.
53+
54+
### Inserting datatimes
55+
56+
The functions `dnow`, `tnow` and `dtnow` return respectively the current string-formatted date, time and datetime:
57+
58+
dnow() -> 03/05/2017
59+
tnow() -> 09:36:03
60+
dtnow() -> 03/05/2017 09:36:03
61+
62+
Notice that you need to have parenthesis after the function name to invoke the function call.
63+
64+
65+
### Computing with sets
66+
67+
While you can just use the regular python to do set computations, there's a few functions included for convinience: `set_intersect`, `set_difference` and `set_symdiff`.
68+
69+
The functions takes two iterable arguments, which are turned into sets, and the computations are performed:
70+
71+
set_intersect('foo bar', 'foo baz') -> {'b', 'a', ' ', 'o', 'f'}
72+
set_intersect('foo baz', 'foo bar') -> {'b', 'a', ' ', 'o', 'f'}
73+
set_difference('foo baz', 'foo bar') -> {'z'}
74+
set_difference('foo bar', 'foo baz') -> {'r'}
75+
set_symdiff('foo baz', 'foo bar') -> {'z', 'r'}
76+
set_symdiff('foo bar', 'foo baz') -> {'z', 'r'}
77+
78+
79+
### Formatting numbers
80+
81+
The fnuction `formatnum` formats numbers, and takes two mandatory and an optional argument:
82+
83+
num
84+
: The number bieng formatted.
85+
86+
digits
87+
: The number of desired digits in the formatted number.
88+
89+
scientificNotation
90+
: Wether of not to use scientific notation.
91+
: Can be True, False or int, where int is the threshold for how many characters the number may contain when formatted un-scientifically, before switching to scientific notation.
92+
: This is the default behaviour, and it's set to 8.
93+
94+
Example usage:
95+
96+
formatnum(0.123456789, 4) -> 0.1235
97+
formatnum(0.123456789, 9) -> 1.234567890e-01
98+
formatnum(123456789.0, 9) -> 1.234567890e+08
99+
formatnum(123456789.0, 2) -> 1.23e+08
100+
formatnum(123.456789, 12) -> 1.234567890000e+02
101+
formatnum(123.456789, 12, False) -> 123.456789000000
102+
formatnum(123.456789, 3) -> 123.457
103+
formatnum(3.14159, 4) -> 3.1416
104+
formatnum(3.14159, 3) -> 3.142
105+
formatnum(3.14159, 2) -> 3.14
106+
formatnum(3.14159, 2, True) -> 3.14e+00
107+
formatnum(3.141592653589793238462643, 3) -> 3.142
108+
109+
26110
## Usage
27111

28112
To evaluate term, highlight and:

0 commit comments

Comments
 (0)