當前位置: 首頁>>代碼示例>>Python>>正文


Python _ast.Module方法代碼示例

本文整理匯總了Python中_ast.Module方法的典型用法代碼示例。如果您正苦於以下問題:Python _ast.Module方法的具體用法?Python _ast.Module怎麽用?Python _ast.Module使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在_ast的用法示例。


在下文中一共展示了_ast.Module方法的6個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。

示例1: test_compile_ast

# 需要導入模塊: import _ast [as 別名]
# 或者: from _ast import Module [as 別名]
def test_compile_ast(self):
        fname = __file__
        if fname.lower().endswith(('pyc', 'pyo')):
            fname = fname[:-1]
        with open(fname, 'r') as f:
            fcontents = f.read()
        sample_code = [
            ['<assign>', 'x = 5'],
            ['<print1>', 'print 1'],
            ['<printv>', 'print v'],
            ['<printTrue>', 'print True'],
            ['<printList>', 'print []'],
            ['<ifblock>', """if True:\n    pass\n"""],
            ['<forblock>', """for n in [1, 2, 3]:\n    print n\n"""],
            ['<deffunc>', """def foo():\n    pass\nfoo()\n"""],
            [fname, fcontents],
        ]

        for fname, code in sample_code:
            co1 = compile(code, '%s1' % fname, 'exec')
            ast = compile(code, '%s2' % fname, 'exec', _ast.PyCF_ONLY_AST)
            self.assertTrue(type(ast) == _ast.Module)
            co2 = compile(ast, '%s3' % fname, 'exec')
            self.assertEqual(co1, co2)
            # the code object's filename comes from the second compilation step
            self.assertEqual(co2.co_filename, '%s3' % fname)

        # raise exception when node type doesn't match with compile mode
        co1 = compile('print 1', '<string>', 'exec', _ast.PyCF_ONLY_AST)
        self.assertRaises(TypeError, compile, co1, '<ast>', 'eval')

        # raise exception when node type is no start node
        self.assertRaises(TypeError, compile, _ast.If(), '<ast>', 'exec')

        # raise exception when node has invalid children
        ast = _ast.Module()
        ast.body = [_ast.BoolOp()]
        self.assertRaises(TypeError, compile, ast, '<ast>', 'exec') 
開發者ID:IronLanguages,項目名稱:ironpython2,代碼行數:40,代碼來源:test_compile.py

示例2: visit_module

# 需要導入模塊: import _ast [as 別名]
# 或者: from _ast import Module [as 別名]
def visit_module(self, node: _ast.Module):  # body,
        last = None
        for body_node in node.body:
            last = self._run(body_node)
        return last 
開發者ID:item4,項目名稱:yui,代碼行數:7,代碼來源:calc.py

示例3: test_compile_ast

# 需要導入模塊: import _ast [as 別名]
# 或者: from _ast import Module [as 別名]
def test_compile_ast(self):
        fname = __file__
        if fname.lower().endswith('pyc'):
            fname = fname[:-1]
        with open(fname, 'r') as f:
            fcontents = f.read()
        sample_code = [
            ['<assign>', 'x = 5'],
            ['<ifblock>', """if True:\n    pass\n"""],
            ['<forblock>', """for n in [1, 2, 3]:\n    print(n)\n"""],
            ['<deffunc>', """def foo():\n    pass\nfoo()\n"""],
            [fname, fcontents],
        ]

        for fname, code in sample_code:
            co1 = compile(code, '%s1' % fname, 'exec')
            ast = compile(code, '%s2' % fname, 'exec', _ast.PyCF_ONLY_AST)
            self.assertTrue(type(ast) == _ast.Module)
            co2 = compile(ast, '%s3' % fname, 'exec')
            self.assertEqual(co1, co2)
            # the code object's filename comes from the second compilation step
            self.assertEqual(co2.co_filename, '%s3' % fname)

        # raise exception when node type doesn't match with compile mode
        co1 = compile('print(1)', '<string>', 'exec', _ast.PyCF_ONLY_AST)
        self.assertRaises(TypeError, compile, co1, '<ast>', 'eval')

        # raise exception when node type is no start node
        self.assertRaises(TypeError, compile, _ast.If(), '<ast>', 'exec')

        # raise exception when node has invalid children
        ast = _ast.Module()
        ast.body = [_ast.BoolOp()]
        self.assertRaises(TypeError, compile, ast, '<ast>', 'exec') 
開發者ID:Microvellum,項目名稱:Fluid-Designer,代碼行數:36,代碼來源:test_compile.py

示例4: test_compile_ast

# 需要導入模塊: import _ast [as 別名]
# 或者: from _ast import Module [as 別名]
def test_compile_ast(self):
        fname = __file__
        if fname.lower().endswith(('pyc', 'pyo')):
            fname = fname[:-1]
        with open(fname, 'r') as f:
            fcontents = f.read()
        sample_code = [
            ['<assign>', 'x = 5'],
            ['<ifblock>', """if True:\n    pass\n"""],
            ['<forblock>', """for n in [1, 2, 3]:\n    print(n)\n"""],
            ['<deffunc>', """def foo():\n    pass\nfoo()\n"""],
            [fname, fcontents],
        ]

        for fname, code in sample_code:
            co1 = compile(code, '%s1' % fname, 'exec')
            ast = compile(code, '%s2' % fname, 'exec', _ast.PyCF_ONLY_AST)
            self.assertTrue(type(ast) == _ast.Module)
            co2 = compile(ast, '%s3' % fname, 'exec')
            self.assertEqual(co1, co2)
            # the code object's filename comes from the second compilation step
            self.assertEqual(co2.co_filename, '%s3' % fname)

        # raise exception when node type doesn't match with compile mode
        co1 = compile('print(1)', '<string>', 'exec', _ast.PyCF_ONLY_AST)
        self.assertRaises(TypeError, compile, co1, '<ast>', 'eval')

        # raise exception when node type is no start node
        self.assertRaises(TypeError, compile, _ast.If(), '<ast>', 'exec')

        # raise exception when node has invalid children
        ast = _ast.Module()
        ast.body = [_ast.BoolOp()]
        self.assertRaises(TypeError, compile, ast, '<ast>', 'exec') 
開發者ID:IronLanguages,項目名稱:ironpython3,代碼行數:36,代碼來源:test_compile.py

示例5: get_child_nodes

# 需要導入模塊: import _ast [as 別名]
# 或者: from _ast import Module [as 別名]
def get_child_nodes(node):
    if isinstance(node, _ast.Module):
        return node.body
    result = []
    if node._fields is not None:
        for name in node._fields:
            child = getattr(node, name)
            if isinstance(child, list):
                for entry in child:
                    if isinstance(entry, _ast.AST):
                        result.append(entry)
            if isinstance(child, _ast.AST):
                result.append(child)
    return result 
開發者ID:zrzka,項目名稱:blackmamba,代碼行數:16,代碼來源:ast.py

示例6: test_compile_ast

# 需要導入模塊: import _ast [as 別名]
# 或者: from _ast import Module [as 別名]
def test_compile_ast(self):
        fname = __file__
        if fname.lower().endswith(('pyc', 'pyo')):
            fname = fname[:-1]
        with open(fname, 'r') as f:
            fcontents = f.read()
        sample_code = [
            ['<assign>', 'x = 5'],
            ['<print1>', 'print 1'],
            ['<printv>', 'print v'],
            ['<printTrue>', 'print True'],
            ['<printList>', 'print []'],
            ['<ifblock>', """if True:\n    pass\n"""],
            ['<forblock>', """for n in [1, 2, 3]:\n    print n\n"""],
            ['<deffunc>', """def foo():\n    pass\nfoo()\n"""],
            #FIXME: Next one not working in Jython:
            #[fname, fcontents],
        ]

        for fname, code in sample_code:
            co1 = compile(code, '%s1' % fname, 'exec')
            ast = compile(code, '%s2' % fname, 'exec', _ast.PyCF_ONLY_AST)
            self.assert_(type(ast) == _ast.Module)
            co2 = compile(ast, '%s3' % fname, 'exec')
            if not test_support.is_jython:
                self.assertEqual(co1, co2)
            # the code object's filename comes from the second compilation step
            self.assertEqual(co2.co_filename, '%s3' % fname)

        # raise exception when node type doesn't match with compile mode
        co1 = compile('print 1', '<string>', 'exec', _ast.PyCF_ONLY_AST)
        #FIXME: raises wrong error in Jython.
        #self.assertRaises(TypeError, compile, co1, '<ast>', 'eval')

        # raise exception when node type is no start node
        self.assertRaises(TypeError, compile, _ast.If(), '<ast>', 'exec')

        # raise exception when node has invalid children
        ast = _ast.Module()
        ast.body = [_ast.BoolOp()]
        #FIXME: raises NPE in Jython.
        #self.assertRaises(TypeError, compile, ast, '<ast>', 'exec') 
開發者ID:Acmesec,項目名稱:CTFCrackTools-V2,代碼行數:44,代碼來源:test_compile.py


注:本文中的_ast.Module方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。