将简单代码解释为python代码

将简单代码解释为python代码,python,pyqt,Python,Pyqt,我用QT python构建了很多非常基本的QT重复程序,然后用py2exe编译它们。这些是我在控制器的同事在另一个程序中使用的。我想知道如何构建一个python解释器,将简单命令转换为实际的python代码,就像处理将简化代码转换为java一样 例如,简单代码文件可以读取: slider('Distance', 'dist', 50, 100, 'int') button('Apply Changes', 'apply') 然后,我将使用以下类型将其解释为pyQT程序形式: slider(la

我用QT python构建了很多非常基本的QT重复程序,然后用py2exe编译它们。这些是我在控制器的同事在另一个程序中使用的。我想知道如何构建一个python解释器,将简单命令转换为实际的python代码,就像处理将简化代码转换为java一样

例如,简单代码文件可以读取:

slider('Distance', 'dist', 50, 100, 'int')
button('Apply Changes', 'apply')
然后,我将使用以下类型将其解释为pyQT程序形式:

slider(label, name, min, max, type)
button(label, name)
这些都将被写入新的python文件,运行该文件时将生成适当的表单。我一直关注的部分是如何将简单代码解释为python代码

提前谢谢

解决方案1

下面的代码是SPEN zar使用正则表达式和split idea来识别小部件类型,然后解析输入以生成必要的输出。显然,这将被扩展以生成真正的pyQT文件,但这是基本逻辑

谢谢你的帮助

import re

input = ["slider('Distance', 'dist', 50, 100, 'int')", "button('Apply Changes', 'apply')"]

pattern = r"([a-z]+)\s*\((.*)\)"
rexp = re.compile(pattern)

for line in input:
    content = rexp.findall(line)
    if content[0][0] == 'slider':
        params = content[0][1].split(',')
        name = params[0]
        label = params[1]
        minimum = float(params[2])
        maximum = float(params[3])
        print 'Slider Type: name-%s, label-%s, min-%f, max-%f' % (name, label, minimum, maximum)
    elif content[0][0] == 'button':
        params = content[0][1].split(',')
        name = params[0]
        label = params[1]
        print 'Button Type: name-%s, label-%s' % (name, label)
    else:
        print 'This widget type is not recognized'
解决方案2

在进一步研究blender的建议之后,我修改了下面的代码,该代码使用一个类来定义按钮。然后,可以根据需要多次轻松地将此类添加到表单中。通过为所有需要的类型构建类,可以很容易地生成表单以及维护和添加到库中

from PyQt4 import QtGui, QtCore
import sys

class Main(QtGui.QMainWindow):
    def __init__(self, parent = None):
        super(Main, self).__init__(parent)

        # main button
        self.addButton = QtGui.QPushButton('button to add other widgets')
        self.addButton.clicked.connect(self.addWidget)

        # scroll area widget contents - layout
        self.scrollLayout = QtGui.QFormLayout()

        # scroll area widget contents
        self.scrollWidget = QtGui.QWidget()
        self.scrollWidget.setLayout(self.scrollLayout)

        # scroll area
        self.scrollArea = QtGui.QScrollArea()
        self.scrollArea.setWidgetResizable(True)
        self.scrollArea.setWidget(self.scrollWidget)

        # main layout
        self.mainLayout = QtGui.QVBoxLayout()

        # add all main to the main vLayout
        self.mainLayout.addWidget(self.addButton)
        self.mainLayout.addWidget(self.scrollArea)

        # central widget
        self.centralWidget = QtGui.QWidget()
        self.centralWidget.setLayout(self.mainLayout)

        # set central widget
        self.setCentralWidget(self.centralWidget)

    def addButton(self):
        self.scrollLayout.addRow(Test())


class Test(QtGui.QWidget):
  def __init__( self, parent=None):
      super(Test, self).__init__(parent)

      self.pushButton = QtGui.QPushButton('I am in Test widget')
      self.pushButton.clicked.connect(self.testPush)

      layout = QtGui.QHBoxLayout()
      layout.addWidget(self.pushButton)
      self.setLayout(layout)

  def testPush(self):
      print "The test button was pushed!"



app = QtGui.QApplication(sys.argv)
myWidget = Main()
for i in xrange(5):
    myWidget.addButton()
myWidget.show()
app.exec_()

您是否尝试过将其解析为.ui格式?这就是XML,它非常简单


无论您喜欢哪种输出格式,尝试PLY进行词法分析和解析。否则,只需使用正则表达式搜索围绕它的子字符串,并使用.split',获取参数。这是我的看法。

为什么不在PyQt4的基础上创建自己的高级库并使用它呢?它将比生成Python源代码更易于维护。我并不是什么都不懂。我有了pyQT,我只需要知道最好的解释方式:sliderattributesBlender,在做了更多的研究之后,我现在可以看到你的方法的价值了。通过下面的例子,我可以看到如何通过在库中编写类来动态地将小部件添加到表单中。好的,这似乎是我想要的答案,这两种方法都可以让我逐行阅读并解释简单的代码,生成我需要的更复杂的代码。谢谢