def __init__(self, parent=None):
        """ setup the application """
        QtGui.QWidget.__init__(self, parent)

        # parser setup
        self.line_parser = ParserLine(LanguageMathematica())
        self.text_parser = ParserText(LanguageMathematica())
        self.formatter = Formatter(LanguagePython(int2float=True))

        # Quit Button
        quitButton = QtGui.QPushButton("Close", self)
        self.connect(quitButton, QtCore.SIGNAL("clicked()"), QtGui.qApp,
                     QtCore.SLOT("quit()"))
        convertButton = QtGui.QPushButton("Convert", self)
        self.connect(convertButton, QtCore.SIGNAL("clicked()"), self.onChanged)

        # Widgets
        self.matLabel = QtGui.QLabel("Mathematica Code:")
        self.matEdit = QtGui.QTextEdit()
        self.pyLabel = QtGui.QLabel("Python Code:")
        self.pyEdit = QtGui.QTextEdit()
        self.connect(self.matEdit, QtCore.SIGNAL("textChanged()"),
                     self.onChanged)
        self.multilineCb = QtGui.QCheckBox("Support Multiple Lines", self)
        self.connect(
            self.multilineCb,
            QtCore.SIGNAL("stateChanged(int)"),
            lambda i: self.onChanged(),
        )

        # Bottom Row
        bottomRow = QtGui.QHBoxLayout()
        bottomRow.addWidget(self.multilineCb)
        bottomRow.addStretch(1)
        bottomRow.addWidget(convertButton)
        bottomRow.addWidget(quitButton)

        # Complete Layout
        vbox = QtGui.QVBoxLayout()
        vbox.addWidget(self.matLabel)
        vbox.addWidget(self.matEdit)
        vbox.addWidget(self.pyLabel)
        vbox.addWidget(self.pyEdit)
        vbox.addLayout(bottomRow)

        # set layout
        self.setLayout(vbox)
        self.setWindowTitle("Mathematica to Python Converter")
        self.resize(500, 350)
예제 #2
0
    def __init__(self):
        """ setup the application """
        super().__init__()

        # parser setup
        self.line_parser = ParserLine(LanguageMathematica())
        self.text_parser = ParserText(LanguageMathematica())
        self.formatter = Formatter(LanguagePython(int2float=True))

        # Quit Button
        quitButton = QtWidgets.QPushButton("Close", self)
        quitButton.clicked.connect(QtWidgets.qApp.quit)
        convertButton = QtWidgets.QPushButton("Convert", self)
        convertButton.clicked.connect(self.onChanged)

        # Widgets
        self.matLabel = QtWidgets.QLabel("Mathematica Code:")
        self.matEdit = QtWidgets.QTextEdit()
        self.matEdit.textChanged.connect(self.onChanged)
        self.pyLabel = QtWidgets.QLabel("Python Code:")
        self.pyEdit = QtWidgets.QTextEdit()
        self.pyEdit.setReadOnly(True)

        self.multilineCb = QtWidgets.QCheckBox("Support Multiple Lines", self)
        self.multilineCb.stateChanged.connect(self.onChanged)

        # Bottom Row
        bottomRow = QtWidgets.QHBoxLayout()
        bottomRow.addWidget(self.multilineCb)
        bottomRow.addStretch(1)
        bottomRow.addWidget(convertButton)
        bottomRow.addWidget(quitButton)

        # Complete Layout
        vbox = QtWidgets.QVBoxLayout()
        vbox.addWidget(self.matLabel)
        vbox.addWidget(self.matEdit)
        vbox.addWidget(self.pyLabel)
        vbox.addWidget(self.pyEdit)
        vbox.addLayout(bottomRow)

        # set layout
        self.setLayout(vbox)
        self.setWindowTitle("Mathematica to Python Converter")
        self.resize(500, 350)
    def __init__(self, parent=None):
        """ setup the application """
        QtGui.QWidget.__init__(self, parent)

        # parser setup
        self.line_parser = ParserLine(LanguageMathematica())
        self.text_parser = ParserText(LanguageMathematica())
        self.formatter = Formatter(LanguagePython(int2float=True))

        # Quit Button
        quitButton = QtGui.QPushButton('Close', self)
        self.connect(
            quitButton, QtCore.SIGNAL('clicked()'),
            QtGui.qApp, QtCore.SLOT('quit()')
       )
        convertButton = QtGui.QPushButton('Convert', self)
        self.connect(
            convertButton, QtCore.SIGNAL('clicked()'), self.onChanged
       )

        # Widgets
        self.matLabel = QtGui.QLabel('Mathematica Code:')
        self.matEdit = QtGui.QTextEdit()
        self.pyLabel = QtGui.QLabel('Python Code:')
        self.pyEdit = QtGui.QTextEdit()
        self.connect(
            self.matEdit, QtCore.SIGNAL('textChanged()'),
            self.onChanged
       )
        self.multilineCb = QtGui.QCheckBox('Support Multiple Lines', self)
        self.connect(
            self.multilineCb, QtCore.SIGNAL('stateChanged(int)'),
            lambda i:self.onChanged()
       )

        # Bottom Row
        bottomRow = QtGui.QHBoxLayout()
        bottomRow.addWidget(self.multilineCb)
        bottomRow.addStretch(1)
        bottomRow.addWidget(convertButton)
        bottomRow.addWidget(quitButton)

        # Complete Layout
        vbox = QtGui.QVBoxLayout()
        vbox.addWidget(self.matLabel)
        vbox.addWidget(self.matEdit)
        vbox.addWidget(self.pyLabel)
        vbox.addWidget(self.pyEdit)
        vbox.addLayout(bottomRow)

        # set layout
        self.setLayout(vbox)
        self.setWindowTitle('Mathematica to Python Converter')
        self.resize(500, 350)
class GUI(QtGui.QWidget):
    """ Class containing the graphical user interface """

    def __init__(self, parent=None):
        """ setup the application """
        QtGui.QWidget.__init__(self, parent)

        # parser setup
        self.line_parser = ParserLine(LanguageMathematica())
        self.text_parser = ParserText(LanguageMathematica())
        self.formatter = Formatter(LanguagePython(int2float=True))

        # Quit Button
        quitButton = QtGui.QPushButton('Close', self)
        self.connect(
            quitButton, QtCore.SIGNAL('clicked()'),
            QtGui.qApp, QtCore.SLOT('quit()')
       )
        convertButton = QtGui.QPushButton('Convert', self)
        self.connect(
            convertButton, QtCore.SIGNAL('clicked()'), self.onChanged
       )

        # Widgets
        self.matLabel = QtGui.QLabel('Mathematica Code:')
        self.matEdit = QtGui.QTextEdit()
        self.pyLabel = QtGui.QLabel('Python Code:')
        self.pyEdit = QtGui.QTextEdit()
        self.connect(
            self.matEdit, QtCore.SIGNAL('textChanged()'),
            self.onChanged
       )
        self.multilineCb = QtGui.QCheckBox('Support Multiple Lines', self)
        self.connect(
            self.multilineCb, QtCore.SIGNAL('stateChanged(int)'),
            lambda i:self.onChanged()
       )

        # Bottom Row
        bottomRow = QtGui.QHBoxLayout()
        bottomRow.addWidget(self.multilineCb)
        bottomRow.addStretch(1)
        bottomRow.addWidget(convertButton)
        bottomRow.addWidget(quitButton)

        # Complete Layout
        vbox = QtGui.QVBoxLayout()
        vbox.addWidget(self.matLabel)
        vbox.addWidget(self.matEdit)
        vbox.addWidget(self.pyLabel)
        vbox.addWidget(self.pyEdit)
        vbox.addLayout(bottomRow)

        # set layout
        self.setLayout(vbox)
        self.setWindowTitle('Mathematica to Python Converter')
        self.resize(500, 350)


    def onChanged(self):
        """ function doing the actual conversion """
        try:
            matCode = str(self.matEdit.toPlainText())

            if self.multilineCb.isChecked():
                self.text_parser.parse_text(matCode)
                pyCode = self.formatter(self.text_parser)

            else:
                matCode = ' '.join(matCode.splitlines())
                output = self.line_parser.parse_string(matCode)
                pyCode = self.formatter(output)

            self.pyEdit.setText(pyCode)
            self.pyLabel.setText('Python Code:')

        except Exception:
            if hasattr(self, 'pyLabel'):
                self.pyLabel.setText(
                    'Python Code <font color="#FF0000">(invalid)</font>:'
               )
 def setUp(self):
     self.parser = ParserLine(LanguagePython(int2float=False))
     self.formatter = Formatter(LanguagePython())
class ParserPythonCheck(unittest.TestCase):
    
    ops = ('*', '/', '+', '-')
    op_prob = 0.5
    
    def setUp(self):
        self.parser = ParserLine(LanguagePython(int2float=False))
        self.formatter = Formatter(LanguagePython())

    
    def calc(self, s, glob=None):
        results = self.parser.parse_string(s)
        return eval(self.formatter(results), glob)

    
    def show_parse(self, s):
        print(self.parser.parse_string(s))
        print(self.parser.expr_stack)
        print(self.parser.get_nested_structure())
        print(self.formatter(self.parser))


    def _get_token(self, depth=0):
        if random.random() < self.op_prob and depth < 10:
            op = random.choice(self.ops)
            arg1 = self._get_token(depth+1)
            arg2 = self._get_token(depth+1)
            return "(%s %s %s)" % (arg1, op, arg2)
        else:
            return str(random.uniform(-2, 2))


    def test_parse_rnd(self):
        for _ in range(20):
            expr = self._get_token()
            self.assertAlmostEqual(self.calc(expr), eval(expr))


    def test_parse(self):
        self.assertEqual(self.calc("9"), 9)
        self.assertEqual(self.calc("-9"), -9)
        self.assertEqual(self.calc("--9"), 9)
        self.assertEqual(self.calc("9 + 3 + 6"), 9 + 3 + 6)
        self.assertEqual(self.calc("-6 + 3"), -3)
        self.assertEqual(self.calc("9 + 3. / 11"), 9 + 3.0 / 11)
        self.assertEqual(self.calc("(9 + 3)"), (9 + 3))
        self.assertEqual(self.calc("(9+3.) / 11"), (9+3.0) / 11)
        self.assertEqual(self.calc("9 - 12 - 6"), 9 - 12 - 6)
        self.assertEqual(self.calc("9 - (12 - 6)"), 9 - (12 - 6))
        self.assertEqual(self.calc("2*3.14159"), 2*3.14159)
        self.assertEqual(self.calc("3.1415926535*3.1415926535 / 10"), 3.1415926535*3.1415926535 / 10)
        self.assertEqual(self.calc("np.pi * np.pi / 10"), np.pi * np.pi / 10)
        self.assertEqual(self.calc("np.pi*np.pi/10"), np.pi*np.pi/10)
        self.assertEqual(self.calc("np.pi**2"), np.pi**2)
        self.assertEqual(self.calc("round(np.pi**2)"), round(np.pi**2))
        self.assertEqual(self.calc("6.02e23 * 8.048"), 6.02e23 * 8.048)
        self.assertEqual(self.calc("np.e / 3"), np.e / 3)
        self.assertEqual(self.calc("sin(np.pi/2)"), np.sin(np.pi/2))
        self.assertEqual(self.calc("trunc(np.e)"), int(np.e))
        self.assertEqual(self.calc("trunc(-np.e)"), int(-np.e))
        self.assertEqual(self.calc("round(np.e)"), round(np.e))
        self.assertEqual(self.calc("round(-np.e)"), round(-np.e))
        self.assertAlmostEqual(self.calc("np.e**np.pi"), np.exp(np.pi))
        self.assertEqual(self.calc("2**3**2"), 2**3**2)
        self.assertEqual(self.calc("2**3+2"), 2**3+2)
        self.assertEqual(self.calc("2**9"), 2**9)
        self.assertEqual(self.calc("sign(-2)"), -1)
        self.assertEqual(self.calc("sign(0)"), 0)
        self.assertEqual(self.calc("sign(0.1)"), 1)
        self.assertEqual(self.calc("sin(np.pi - np.pi)"), 0.)
        self.assertEqual(self.calc("sin(np.pi/(sign(0.1)+sign(5)))"), np.sin(np.pi/2.))
        self.assertEqual(self.calc("sin(0.)"), 0.)
        self.assertEqual(self.calc("np.e**1."), np.e)
        self.assertEqual(self.calc("np.e**(3-2)"), np.e)
        self.assertEqual(self.calc("(1+2)**2"), 9)        


    def test_parse_eqation(self):
        self.assertEqual(self.calc("3 == 5"), False)
        self.assertEqual(self.calc("3 == 4-1"), True)
        self.assertEqual(self.calc("(a+b)**2 == 9", {"a":1, "b":2}), True)
        self.assertEqual(self.calc(
            "sin(1.)**(2.**3.) - sin(1.)**(2.**3.) + sin(1.) == sin(1.)"), True)
예제 #7
0
class GUI(QtWidgets.QWidget):
    """ Class containing the graphical user interface """

    def __init__(self):
        """ setup the application """
        super().__init__()

        # parser setup
        self.line_parser = ParserLine(LanguageMathematica())
        self.text_parser = ParserText(LanguageMathematica())
        self.formatter = Formatter(LanguagePython(int2float=True))

        # Quit Button
        quitButton = QtWidgets.QPushButton("Close", self)
        quitButton.clicked.connect(QtWidgets.qApp.quit)
        convertButton = QtWidgets.QPushButton("Convert", self)
        convertButton.clicked.connect(self.onChanged)

        # Widgets
        self.matLabel = QtWidgets.QLabel("Mathematica Code:")
        self.matEdit = QtWidgets.QTextEdit()
        self.matEdit.textChanged.connect(self.onChanged)
        self.pyLabel = QtWidgets.QLabel("Python Code:")
        self.pyEdit = QtWidgets.QTextEdit()
        self.pyEdit.setReadOnly(True)

        self.multilineCb = QtWidgets.QCheckBox("Support Multiple Lines", self)
        self.multilineCb.stateChanged.connect(self.onChanged)

        # Bottom Row
        bottomRow = QtWidgets.QHBoxLayout()
        bottomRow.addWidget(self.multilineCb)
        bottomRow.addStretch(1)
        bottomRow.addWidget(convertButton)
        bottomRow.addWidget(quitButton)

        # Complete Layout
        vbox = QtWidgets.QVBoxLayout()
        vbox.addWidget(self.matLabel)
        vbox.addWidget(self.matEdit)
        vbox.addWidget(self.pyLabel)
        vbox.addWidget(self.pyEdit)
        vbox.addLayout(bottomRow)

        # set layout
        self.setLayout(vbox)
        self.setWindowTitle("Mathematica to Python Converter")
        self.resize(500, 350)

    def onChanged(self):
        """ function doing the actual conversion """
        try:
            matCode = str(self.matEdit.toPlainText())

            if self.multilineCb.isChecked():
                self.text_parser.parse_text(matCode)
                pyCode = self.formatter(self.text_parser)

            else:
                matCode = " ".join(matCode.splitlines())
                output = self.line_parser.parse_string(matCode)
                pyCode = self.formatter(output)

            self.pyEdit.setText(pyCode)
            self.pyLabel.setText("Python Code:")

        except Exception:
            if hasattr(self, "pyLabel"):
                self.pyLabel.setText(
                    'Python Code <font color="#FF0000">(invalid)</font>:'
                )
 def setUp(self):
     self.parser = ParserLine(LanguageMathematica())
     self.formatter = Formatter(LanguagePython())
class ParserMathematicaCheck(unittest.TestCase):

    ops = ('*', '/', '+', '-')
    op_prob = 0.5

    def setUp(self):
        self.parser = ParserLine(LanguageMathematica())
        self.formatter = Formatter(LanguagePython())


    def calc(self, s):
        results = self.parser.parse_string(s)
        return eval(self.formatter(results), None, {'pi':np.pi, 'e':np.e})


    def show_parse(self, s):
        print(self.parser.parse_string(s))
        print(self.parser.expr_stack)
        print(self.parser.get_nested_structure())
        print(self.formatter(self.parser))


    def _get_token(self, depth=0):
        if random.random() < self.op_prob and depth < 10:
            op = random.choice(self.ops)
            arg1 = self._get_token(depth+1)
            arg2 = self._get_token(depth+1)
            return "(%s %s %s)" % (arg1, op, arg2)
        else:
            return str(random.uniform(-2, 2))


    def test_parse_rnd(self):
        for i in range(20):
            expr = self._get_token()
            self.assertAlmostEqual(self.calc(expr), eval(expr) )


    def test_parse(self):
        self.assertEqual(self.calc("9"), 9)
        self.assertEqual(self.calc("-9"), -9)
        self.assertEqual(self.calc("--9"), 9)
        self.assertEqual(self.calc("9 + 3 + 6"), 9 + 3 + 6)
        self.assertEqual(self.calc("-6 + 3"), -3)
        self.assertEqual(self.calc("9 + 3. / 11"), 9 + 3.0 / 11)
        self.assertEqual(self.calc("(9 + 3)"), (9 + 3))
        self.assertEqual(self.calc("(9+3.) / 11"), (9+3.0) / 11)
        self.assertEqual(self.calc("9 - 12 - 6"), 9 - 12 - 6)
        self.assertEqual(self.calc("9 - (12 - 6)"), 9 - (12 - 6))
        self.assertEqual(self.calc("2*3.14159"), 2*3.14159)
        self.assertEqual(self.calc("3.1415926535*3.1415926535 / 10"), 3.1415926535*3.1415926535 / 10)
        self.assertEqual(self.calc("Pi * Pi / 10"), np.pi * np.pi / 10)
        self.assertEqual(self.calc("Pi*Pi/10"), np.pi*np.pi/10)
        self.assertEqual(self.calc("Pi^2"), np.pi**2)
        self.assertEqual(self.calc("Round[Pi^2]"), round(np.pi**2))
        self.assertEqual(self.calc("6.02E23 * 8.048"), 6.02E23 * 8.048)
        self.assertEqual(self.calc("e / 3"), np.e / 3)
        self.assertEqual(self.calc("Sin[Pi/2]"), np.sin(np.pi/2))
        self.assertEqual(self.calc("Trunc[E]"), int(np.e))
        self.assertEqual(self.calc("Trunc[-E]"), int(-np.e))
        self.assertEqual(self.calc("Round[E]"), round(np.e))
        self.assertEqual(self.calc("Round[-E]"), round(-np.e))
        self.assertAlmostEqual(self.calc("E^Pi"), np.exp(np.pi))
        self.assertEqual(self.calc("2^3^2"), 2**3**2)
        self.assertEqual(self.calc("2^3+2"), 2**3+2)
        self.assertEqual(self.calc("2^9"), 2**9)
        self.assertEqual(self.calc("Sign[-2]"), -1)
        self.assertEqual(self.calc("Sign[0]"), 0)
        self.assertEqual(self.calc("Sign[0.1]"), 1)
        self.assertEqual(self.calc("Sin[Pi - Pi]"), 0.)
        self.assertEqual(self.calc("Sin[Pi/(Sign[0.1]+Sign[5])]"), np.sin(np.pi/2.))
        self.assertEqual(self.calc("Sin[0.]"), 0.)
        self.assertEqual(self.calc("E^1."), np.e)
        self.assertEqual(self.calc("E^(3-2)"), np.e)
        self.assertEqual(self.calc("(1+2)^2"), 9)
예제 #10
0
#!/usr/bin/env python

import sys
sys.path.append('..')

from src.parser_line import ParserLine
from src.language import LanguagePython, LanguageMathematica
from src.parser_text import ParserText
from src.formatter import Formatter

parser_python = ParserLine(LanguagePython())
parser_mathematica = ParserLine(LanguageMathematica())
parser_text = ParserText(LanguageMathematica())
formatter = Formatter(LanguagePython())


def parse_line(s, style='python'):
    if style == 'python':
        parser = parser_python
    else:
        parser = parser_mathematica
    parser.parse_string(s)
    print('parse: %r' % parser.result_parse)
    print('stack: %r' % parser.result_stack)
    print('nested: %r' % parser.result_nested)
    print(formatter(parser))
    print(s)


def parse_text(s, optimize=True):
    parser_text.parse_text(s)
 def setUp(self):
     self.parser = ParserLine(LanguageMathematica())
     self.formatter = Formatter(LanguagePython())
class ParserMathematicaCheck(unittest.TestCase):

    ops = ('*', '/', '+', '-')
    op_prob = 0.5

    def setUp(self):
        self.parser = ParserLine(LanguageMathematica())
        self.formatter = Formatter(LanguagePython())

    def calc(self, s):
        results = self.parser.parse_string(s)
        return eval(self.formatter(results), None, {'pi': np.pi, 'e': np.e})

    def show_parse(self, s):
        print(self.parser.parse_string(s))
        print(self.parser.expr_stack)
        print(self.parser.get_nested_structure())
        print(self.formatter(self.parser))

    def _get_token(self, depth=0):
        if random.random() < self.op_prob and depth < 10:
            op = random.choice(self.ops)
            arg1 = self._get_token(depth + 1)
            arg2 = self._get_token(depth + 1)
            return "(%s %s %s)" % (arg1, op, arg2)
        else:
            return str(random.uniform(-2, 2))

    def test_parse_rnd(self):
        for i in range(20):
            expr = self._get_token()
            self.assertAlmostEqual(self.calc(expr), eval(expr))

    def test_parse(self):
        self.assertEqual(self.calc("9"), 9)
        self.assertEqual(self.calc("-9"), -9)
        self.assertEqual(self.calc("--9"), 9)
        self.assertEqual(self.calc("9 + 3 + 6"), 9 + 3 + 6)
        self.assertEqual(self.calc("-6 + 3"), -3)
        self.assertEqual(self.calc("9 + 3. / 11"), 9 + 3.0 / 11)
        self.assertEqual(self.calc("(9 + 3)"), (9 + 3))
        self.assertEqual(self.calc("(9+3.) / 11"), (9 + 3.0) / 11)
        self.assertEqual(self.calc("9 - 12 - 6"), 9 - 12 - 6)
        self.assertEqual(self.calc("9 - (12 - 6)"), 9 - (12 - 6))
        self.assertEqual(self.calc("2*3.14159"), 2 * 3.14159)
        self.assertEqual(self.calc("3.1415926535*3.1415926535 / 10"),
                         3.1415926535 * 3.1415926535 / 10)
        self.assertEqual(self.calc("Pi * Pi / 10"), np.pi * np.pi / 10)
        self.assertEqual(self.calc("Pi*Pi/10"), np.pi * np.pi / 10)
        self.assertEqual(self.calc("Pi^2"), np.pi**2)
        self.assertEqual(self.calc("Round[Pi^2]"), round(np.pi**2))
        self.assertEqual(self.calc("6.02E23 * 8.048"), 6.02E23 * 8.048)
        self.assertEqual(self.calc("e / 3"), np.e / 3)
        self.assertEqual(self.calc("Sin[Pi/2]"), np.sin(np.pi / 2))
        self.assertEqual(self.calc("Trunc[E]"), int(np.e))
        self.assertEqual(self.calc("Trunc[-E]"), int(-np.e))
        self.assertEqual(self.calc("Round[E]"), round(np.e))
        self.assertEqual(self.calc("Round[-E]"), round(-np.e))
        self.assertAlmostEqual(self.calc("E^Pi"), np.exp(np.pi))
        self.assertEqual(self.calc("2^3^2"), 2**3**2)
        self.assertEqual(self.calc("2^3+2"), 2**3 + 2)
        self.assertEqual(self.calc("2^9"), 2**9)
        self.assertEqual(self.calc("Sign[-2]"), -1)
        self.assertEqual(self.calc("Sign[0]"), 0)
        self.assertEqual(self.calc("Sign[0.1]"), 1)
        self.assertEqual(self.calc("Sin[Pi - Pi]"), 0.)
        self.assertEqual(self.calc("Sin[Pi/(Sign[0.1]+Sign[5])]"),
                         np.sin(np.pi / 2.))
        self.assertEqual(self.calc("Sin[0.]"), 0.)
        self.assertEqual(self.calc("E^1."), np.e)
        self.assertEqual(self.calc("E^(3-2)"), np.e)
        self.assertEqual(self.calc("(1+2)^2"), 9)