coding utf-8 from PyQt4 import import re regular expressions from ui_m

 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
# -*- coding: utf-8 -*-
from PyQt4 import *
import re #regular expressions
from ui_mainwindow import *
from ui_help import *
from ui_about import *
from parser import *
class MainWindow(QtGui.QMainWindow):
def __init__(self,parent=None):
QtGui.QWidget.__init__(self,parent)
self.ui=Ui_MainWindow()
self.ui.setupUi(self)
self.ui.tableWidget1.setColumnCount(5)
self.ui.tableWidget1.setRowCount(5)
self.calculated = []
self.connect(self.ui.pushButton_calc, QtCore.SIGNAL("clicked()"), self.calc_this)
def get_cell_str(self, row, column):
a = self.ui.tableWidget1.item(row, column)
if a is None:
return ''
print "get:", a.text()
return str(a.text())
def set_cell_str(self, row, column, s):
print s
def calc_this(self):
row = self.ui.tableWidget1.currentRow()
column = self.ui.tableWidget1.currentColumn()
self.calculated = []
try:
val = self.calc(row, column)
self.set_cell_str( row, column, str(val) )
except ParseException as e:
return "Помилка бiля сымволу №"+str(e.loc+1)
#except MyException2:
# self.set_cell_str(row, column, "Зациклювання")
#except:
# self.set_cell_str(row, column, "Перевірте правильність виразу")
def calc(self, row, column):
s = self.get_cell_str(row, column)
if len(s) == 0:
return 0.
if (row, column) in self.calculated:
raise Exception('MyException2', 'MyException2')
else:
self.calculated.append((row,column))
r= re.search("\[(\d+);(\d+)\]",s)
while r:
x = int(r.groups()[0])
y = int(r.groups()[1])
val = self.calc( x, y )
s = s[:r.start()] + "(" + str(val) + ")" + s[r.end():]
r= re.search("\[(\d+);(\d+)\]",s)
print s
if ']' in s or '[' in s:
raise Exception('MyException', 'MyException')
self.calculated.remove((row,column))
return parse(s)