当前位置: 首页>>代码示例>>Python>>正文


Python Form.parse方法代码示例

本文整理汇总了Python中form.Form.parse方法的典型用法代码示例。如果您正苦于以下问题:Python Form.parse方法的具体用法?Python Form.parse怎么用?Python Form.parse使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在form.Form的用法示例。


在下文中一共展示了Form.parse方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: TestParser

# 需要导入模块: from form import Form [as 别名]
# 或者: from form.Form import parse [as 别名]
class TestParser(unittest.TestCase):
    '''
    Testeando que los arboles AST sean generados correctamente a partir de varias expresiones.
    '''

    @classmethod
    def setUpClass(self):
        self.form = Form()

    def test_concat(self):
        exp = "ABCD"
        ast = self.form.parse(exp)
        ns = ast.root.preorder()
        res_actual = [n.type for n in ns]
        res_expected = ["concat", "A", "concat", "B", "concat", "C", "D"]
        self.assertEqual(res_actual, res_expected)

    def test_divide(self):
        exp = "A/B"
        ast = self.form.parse(exp)
        ns = ast.root.preorder()
        res_actual = [n.type for n in ns]
        res_expected = ["divide", "A", "B", "barra"]
        self.assertEqual(res_actual, res_expected)

    def test_superindice(self):
        exp = "A^B"
        ast = self.form.parse(exp)
        ns = ast.root.preorder()
        res_actual = [n.type for n in ns]
        res_expected = ["p", "A", "B"]
        self.assertEqual(res_actual, res_expected)

    def test_subindice(self):
        exp = "A_B"
        ast = self.form.parse(exp)
        ns = ast.root.preorder()
        res_actual = [n.type for n in ns]
        res_expected = ["u", "A", "B"]
        self.assertEqual(res_actual, res_expected)

    def test_parentesis1(self):
        exp = "(A)"
        ast = self.form.parse(exp)
        ns = ast.root.preorder()
        res_actual = [n.type for n in ns]
        res_expected = ["parens", "(", "A", ")"]
        self.assertEqual(res_actual, res_expected)

    def test_llaves(self):
        exp = "{A/B}C"
        ast = self.form.parse(exp)
        ns = ast.root.preorder()
        res_actual = [n.type for n in ns]
        res_expected = ['concat', 'brackets', '{', 'divide', 'A', 'B', 'barra', '}', 'C']
        self.assertEqual(res_actual, res_expected)

    def test_parentesis2(self):
        exp = "(A/B)C"
        ast = self.form.parse(exp)
        ns = ast.root.preorder()
        res_actual = [n.type for n in ns]
        res_expected = ['concat', 'parens', '(', 'divide', 'A', 'B', 'barra', ')', 'C']
        self.assertEqual(res_actual, res_expected)

    def test_super_sub_indice(self):
        exp = "A^B_C"
        ast = self.form.parse(exp)
        ns = ast.root.preorder()
        res_actual = [n.type for n in ns]
        res_expected = ["pu", "A", "B", "C"]
        self.assertEqual(res_actual, res_expected)

    def test_combinado1(self):
        exp = "(A^BC^D/E^F_G+H)-I"
        ast = self.form.parse(exp)
        ns = ast.root.preorder()
        res_actual = [n.type for n in ns]
        res_expected = ["concat", "parens", "(", "divide", "concat", "p", "A", "B", "p", "C", "D", "concat", "pu", "E", "F", "G", "concat", "+", "H", "barra", ")", "concat", "-", "I"]
        self.assertEqual(res_actual, res_expected)

    def test_combinado2(self):
        exp = "(A^{BC})"
        ast = self.form.parse(exp)
        ns = ast.root.preorder()
        res_actual = [n.type for n in ns]
        res_expected = ['parens', '(', 'p', 'A', 'brackets', '{', 'concat', 'B', 'C', '}', ')']
        self.assertEqual(res_actual, res_expected)

    def test_combinado3(self):
        exp = "({A/B/C}{D/E})^{J+K}"
        ast = self.form.parse(exp)
        ns = ast.root.preorder()
        res_actual = [n.type for n in ns]
        res_expected = ['p', 'parens','(', 'concat', 'brackets', '{', 'divide', 'A', 'divide', 'B', 'C', 'barra', 'barra', '}', 'brackets', '{', 'divide', 'D', 'E', 'barra', '}', ')', 'brackets', '{', 'concat', 'J', 'concat', '+', 'K', '}']
        self.assertEqual(res_actual, res_expected)

    def test_expresion_invalida1(self):
        exp = "^B"
        self.assertRaises(Exception, lambda: self.form.parse(exp)) 
#.........这里部分代码省略.........
开发者ID:aszyrej,项目名称:tleng-tp1,代码行数:103,代码来源:testParser.py


注:本文中的form.Form.parse方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。