def _init_info_base(self): f = open('base') lines = [line for line in f.readlines() if line.split()] count = int(lines[0]) self._names = {} for line in lines[1:1 + count]: line = line.split(' ', 1) self._names[line[1].rstrip()] = line[0] model = [Formula(info=line1, code=line2) for line1, line2 in zip(lines[1 + count::2], lines[2 + count::2])] self._info_base = InfoBase(model)
class SolverGenerator(QWidget): def __init__(self): super().__init__() self._init_info_base() self._init_ui() def _init_info_base(self): f = open('base') lines = [line for line in f.readlines() if line.split()] count = int(lines[0]) self._names = {} for line in lines[1:1 + count]: line = line.split(' ', 1) self._names[line[1].rstrip()] = line[0] model = [Formula(info=line1, code=line2) for line1, line2 in zip(lines[1 + count::2], lines[2 + count::2])] self._info_base = InfoBase(model) def _init_ui(self): main = QVBoxLayout(self) up_label = QLabel('Входные параметры:') up_label.setAlignment(Qt.AlignCenter) main.addWidget(up_label) up = QHBoxLayout(self) self.up_left = QListWidget(self) self.up_left.addItems(list(self._names.keys())) self.up_left.sortItems() up.addWidget(self.up_left) up_buttons = QVBoxLayout(self) up_to_right = QPushButton('>') self.connect(up_to_right, SIGNAL('pressed()'), self._up_to_right) up_buttons.addWidget(up_to_right) up_to_left = QPushButton('<') self.connect(up_to_left, SIGNAL('pressed()'), self._up_to_left) up_buttons.addWidget(up_to_left) up_buttons.setAlignment(Qt.AlignCenter) up.addLayout(up_buttons) self.up_right = QListWidget(self) up.addWidget(self.up_right) self.up_right.sortItems() main.addLayout(up) down_label = QLabel('Выходные параметры:') down_label.setAlignment(Qt.AlignCenter) main.addWidget(down_label) down = QHBoxLayout(self) self.down_left = QListWidget(self) self.down_left.addItems(list(self._names.keys())) self.down_left.sortItems() down.addWidget(self.down_left) down_buttons = QVBoxLayout(self) down_to_right = QPushButton('>') self.connect(down_to_right, SIGNAL('pressed()'), self._down_to_right) down_buttons.addWidget(down_to_right) down_to_left = QPushButton('<') self.connect(down_to_left, SIGNAL('pressed()'), self._down_to_left) down_buttons.addWidget(down_to_left) down_buttons.setAlignment(Qt.AlignCenter) down.addLayout(down_buttons) self.down_right = QListWidget(self) down.addWidget(self.down_right) self.down_right.sortItems() main.addLayout(down) solve = QPushButton('Решить') self.connect(solve, SIGNAL('pressed()'), self._solve) main.addWidget(solve) self.setLayout(main) self.setWindowTitle('SolverGenerator') self.resize(600, 600) self._center() self.show() def _solve(self): input_definition = [self.up_right.item(i).text() for i in range(self.up_right.count())] input_params = [self._names[item] for item in input_definition] output_definition = [self.down_right.item(i).text() for i in range(self.down_right.count())] output_params = [self._names[item] for item in output_definition] algorithm = self._info_base.get_algorithm(input_params, output_params) if algorithm: self._generate_code(input_definition, input_params, output_definition, output_params, algorithm) else: QMessageBox().warning(self, 'Решения нет', 'Для выбранных данных решения нет') def _up_to_right(self): item = self.up_left.takeItem(self.up_left.currentRow()) self.up_right.addItem(item) self.up_right.sortItems() def _up_to_left(self): item = self.up_right.takeItem(self.up_right.currentRow()) self.up_left.addItem(item) self.up_left.sortItems() def _down_to_right(self): item = self.down_left.takeItem(self.down_left.currentRow()) self.down_right.addItem(item) self.down_right.sortItems() def _down_to_left(self): item = self.down_right.takeItem(self.down_right.currentRow()) self.down_left.addItem(item) self.down_left.sortItems() def _center(self): qr = self.frameGeometry() cp = QDesktopWidget().availableGeometry().center() qr.moveCenter(cp) self.move(qr.topLeft()) @staticmethod def _generate_code(input_definition, input_params, output_definition, output_params, algorithm): code = [] if any('math' in formula.code for formula in algorithm): code.append('import math') for name, definition in zip(input_params, input_definition): code.append( '{0} = float(input("Введите {0} ({1}): "))'.format(name, definition.lower())) for formula in algorithm: code.append(formula.code) for name, definition in zip(output_params, output_definition): code.append('print("{0} = %f" % {1})'.format(definition, name)) code.append('input("Нажмите Enter для завершения.")') code.append('') code = '\n'.join(code) file = open('program.py', 'w') file.write(code) file.close() xterm = sh.Command('xterm') xterm('-e', 'python3 program.py')