This repository was archived by the owner on Oct 3, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrofi-python-calculator
executable file
·72 lines (59 loc) · 2.42 KB
/
rofi-python-calculator
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
#!/usr/bin/python3
"""
Python numpy calculator imbed in a rofi interface (python-rofi):
- can evaluate all numpy expression
- create a variable containing your result_lists for further usage
"""
from numpy import *
from rofi import Rofi
import string
from itertools import islice
from collections import deque
MEMORY_SIZE = 20 # number of calculation kept in memory
ALPHABET = list(string.ascii_lowercase) # used for variable name generation
r = Rofi(lines=1) # initiate rofi interface
# iterable = slicing window of 3 letters lenght over the math expression
def read_by_3(iterable, size=3):
iterable = iter(" " + iterable + " ")
d = deque(islice(iterable, size), size)
yield d
for x in iterable:
d.append(x)
yield d
# generate variable name by cycling over alphabet
def gen_var_letter(number=20):
ALPHABET = list(string.ascii_lowercase)[0:number]
while 1 :
i=0
while i<number :
yield ALPHABET[i]
i+=1
vals = list(zeros(0))
result_list = deque(vals,MEMORY_SIZE)
result_name = deque(vals,MEMORY_SIZE)
algebric_list = deque(vals,MEMORY_SIZE)
var_name_gen = gen_var_letter(number=MEMORY_SIZE)
msg="start typing a numpy expression\ntip : you can use generated variable name in your formulas"
while 1 :
# get user's expression
algebric_expression = r.text_entry('calc', msg) # with variables, ex : 2*a + b**2
numeric_expression = str() # with numerical values for evaluation : 2*4 + 7**2
# for each character, if it's a variable, replace by its numerical value
for [prev_char, character, follow_char] in read_by_3(algebric_expression) :
# variable is an alphabet letter
if not(character in ALPHABET) :
numeric_expression += character # = keep the character
# let's do not confuse with mathematical tools (cos, exp, abs...)
# variable is an isolated letter
elif prev_char in ALPHABET or follow_char in ALPHABET:
numeric_expression += character
else :
index = result_name.index(character)
numeric_expression += str(result_list[index])
result_list.append(eval(numeric_expression))
result_name.append(var_name_gen.__next__())
algebric_list.append(algebric_expression)
msg=str()
for i,j,k in zip(list(result_list)[::-1], list(result_name)[::-1], list(algebric_list)[::-1]):
print(i,j,k)
msg += "{} : {} = {} \n".format(j, k, i)