简体   繁体   English

将简单代码解释为python代码

[英]Interpreting simple code to python code

I build a lot of very basic QT repetitive programs at work with QT python then compile them with py2exe. 我使用QT python构建了许多非常基本的QT重复程序,然后使用py2exe进行编译。 These are used by my coworkers at controllers for another program. 我的同事在控制器上将这些用作另一个程序。 I am wonder how I could build a python interpreter that would convert simple commands into actual python code similar to the way Processing converts a simplified code into java. 我不知道如何构建一个将简单命令转换为实际python代码的python解释器,类似于Processing将简化代码转换为java的方式。

For example the "simple code" file may read: 例如,“简单代码”文件可能显示为:

slider('Distance', 'dist', 50, 100, 'int')
button('Apply Changes', 'apply')

which I would then interpret into a pyQT program form using types below: 然后我将使用以下类型将其解释为pyQT程序形式:

slider(label, name, min, max, type)
button(label, name)

These would all be written to new python file, which when run would generate the appropriate form. 这些都将被写入新的python文件,该文件在运行时将生成适当的形式。 The part I am stuck on is how to interpret the "simple code" into python code. 我坚持的部分是如何将“简单代码”解释为python代码。

Thanks in advance 提前致谢


Solution #1 解决方案1

The code below is uses the regex and split idea by SPEN-zar to identify the widget type, then parse the inputs to generate the necessary outputs. 下面的代码使用SPEN-zar的正则表达式和拆分思想来识别小部件类型,然后解析输入以生成必要的输出。 Obviously, this will be expanded upon to generate real pyQT file, but this is the basic demonstrates the basic logic. 显然,这将被扩展以生成实际的pyQT文件,但这是基本演示的基本逻辑。

Thank you for the help. 感谢您的帮助。

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'

Solution #2 解决方案#2

After doing further research into blender's suggestion, I have modified the code below that uses a class to define a button. 在进一步研究Blender的建议之后,我修改了下面的代码,该代码使用一个类来定义按钮。 This class can then be easily added to the form as many times as needed. 然后可以根据需要将此类轻松添加到表单中。 By building classes for all the types needed it would be easy to easy generate forms as well maintain and add to the library. 通过为所有需要的类型构建类,可以轻松地生成表单以及维护和添加到库中。

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_()

Have you tried just parsing it into a .ui format? 您是否尝试过将其解析为.ui格式? That is XML, which is pretty straightforward. 这就是XML,这非常简单。

Whichever output format you prefer, try PLY for the lexing and parsing. 无论您喜欢哪种输出格式,都可以尝试使用PLY进行词法分析。 Otherwise, just use a regular expression to search for the substring with "(" ")" surrounding it and use .split(',') to get the arguments. 否则,只需使用正则表达式来搜索带有“(”“)”的子字符串,然后使用.split(',')来获取参数。 That's my take. 那是我的看法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM