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


Python ast.walk函数代码示例

本文整理汇总了Python中ast.walk函数的典型用法代码示例。如果您正苦于以下问题:Python walk函数的具体用法?Python walk怎么用?Python walk使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。


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

示例1: _log_function

def _log_function(item):
    """
    Handler function for log message types.

    :param item: ast object being inspected for certain properties
    :type item: :class:`~ast.AST`
    :return: Returns the descriptor and arg offset of the item.
    :rtype: tuple (str, int)
    """
    level_arg = item.args[0]
    if isinstance(level_arg, ast.Str):
        level = level_arg.s.lower()
    else:
        any_call_or_name_elements = any(
            isinstance(element, (ast.Call, ast.Name))
            for element in ast.walk(level_arg)
        )
        if any_call_or_name_elements:
            level = "(dynamic)"
        else:
            level = ', '.join(
                element.s.lower()
                for element in ast.walk(level_arg)
                if isinstance(element, ast.Str)
            )
    integer_arg_offset = 1
    return level, integer_arg_offset
开发者ID:Arelle,项目名称:ArelleBuilder,代码行数:27,代码来源:generate_messages_catalog.py

示例2: ast_equal

def ast_equal(code1, code2, check_line_col=False, ignore_var_names=False):
    """
    Checks whether ast nodes are equivalent recursively.

    By default does not check line number or col offset
    """
    gen1 = ast.walk(code1)
    gen2 = ast.walk(code2)

    for node1, node2 in zip_longest(gen1, gen2):
        # unequal length
        if node1 is None or node2 is None:
            return False

        if type(node1) != type(node2):
            return False

        # ignore the names of load name variables.
        if ignore_var_names and is_load_name(node1) and is_load_name(node2):
            continue

        if not ast_field_equal(node1, node2):
            return False

        if check_line_col and hasattr(node1, 'lineno'):
            if node1.lineno != node2.lineno:
                return False
            if node1.col_offset != node2.col_offset:
                return False

    return True
开发者ID:dalejung,项目名称:asttools,代码行数:31,代码来源:__init__.py

示例3: nestable_lines

    def nestable_lines(self):
        """ Return the range of lines that are nestable.

        Parse the tree for all start and end lines of a nestable group (e.g. a
        function defintion or if statement). For each, return a tuple of the
        start and end line number.

        """

        nests = []
        nodes = [ast.walk(node) for node in self._tree.body]
        for node in nodes:
            end = 0
            for subnode in node:
                if isinstance(subnode, (ast.FunctionDef, ast.ClassDef, ast.If,
                                        ast.For, ast.TryExcept,
                                        ast.TryFinally)):
                    end = 0
                    for subsubnode in ast.walk(subnode):
                        try:
                            lineno = subsubnode.lineno
                        except AttributeError:
                            pass
                        else:
                            if lineno > end:
                                end = lineno
                    nests.append((subnode.lineno, end))
        return nests
开发者ID:liasis,项目名称:introspector,代码行数:28,代码来源:parse.py

示例4: hy_eval

def hy_eval(hytree, namespace, module_name):
    foo = HyObject()
    foo.start_line = 0
    foo.end_line = 0
    foo.start_column = 0
    foo.end_column = 0
    replace_hy_obj(hytree, foo)

    if not isinstance(module_name, string_types):
        raise HyTypeError(foo, "Module name must be a string")

    _ast, expr = hy_compile(hytree, module_name, get_expr=True)

    # Spoof the positions in the generated ast...
    for node in ast.walk(_ast):
        node.lineno = 1
        node.col_offset = 1

    for node in ast.walk(expr):
        node.lineno = 1
        node.col_offset = 1

    if not isinstance(namespace, dict):
        raise HyTypeError(foo, "Globals must be a dictionary")

    # Two-step eval: eval() the body of the exec call
    eval(ast_compile(_ast, "<eval_body>", "exec"), namespace)

    # Then eval the expression context and return that
    return eval(ast_compile(expr, "<eval>", "eval"), namespace)
开发者ID:ALSchwalm,项目名称:hy,代码行数:30,代码来源:importer.py

示例5: ast_transform

def ast_transform(root):
    parent_node = None
    dt_class_names = set(c.__name__ for c in (DateTime, Date, Duration))

    str_nodes = []
    for node in ast.walk(root):
        try:
            if isinstance(node, ast.Str):
                if isinstance(parent_node, ast.Name) and parent_node.id in dt_class_names:
                    pass
                else:
                    str_nodes.append(node)
        finally:
            parent_node = node
    nt = DTNodeTransformer(str_nodes)
    nt.visit(root)
    ast.fix_missing_locations(root)
    prev_lineno, prev_col_offset = 1, 0
    for n in ast.walk(root):
        if not hasattr(n, 'lineno'):
            n.lineno = prev_lineno
        else:
            prev_lineno = n.lineno
        if not hasattr(n, 'col_offset'):
            n.col_offset = prev_col_offset
        else:
            prev_col_offset = n.col_offset
开发者ID:simone-campagna,项目名称:dtcalc,代码行数:27,代码来源:dt_ast.py

示例6: replace_variables

 def replace_variables(self):
     """Replace script variables in string values."""
     variables = {
         'SCRIPT_SOURCE': self.source_script,
         'SCRIPT_NAME': self.script_name,
         'SCRIPT_PATH': self.script_path,
         'SCRIPT_AUTHOR': 'Sebastien Helleu',
         'SCRIPT_VERSION': '1.0',
         'SCRIPT_LICENSE': 'GPL3',
         'SCRIPT_DESCRIPTION': ('%s scripting API test' %
                                self.language.capitalize()),
         'SCRIPT_LANGUAGE': self.language,
     }
     # count the total number of tests
     tests_count = 0
     for node in ast.walk(self.tree):
         if isinstance(node, ast.Call) and \
                 isinstance(node.func, ast.Name) and \
                 node.func.id == 'check':
             tests_count += 1
     variables['SCRIPT_TESTS'] = str(tests_count)
     # replace variables
     for node in ast.walk(self.tree):
         if isinstance(node, ast.Str) and \
                 node.s in variables:
             node.s = variables[node.s]
开发者ID:weechat,项目名称:weechat,代码行数:26,代码来源:testapigen.py

示例7: check_for_wrong_tuple

def check_for_wrong_tuple(tree, code, noqa):
    errors = []
    candidates = []
    for assign in ast.walk(tree):
        if not isinstance(assign, ast.Assign) or assign.lineno in noqa:
            continue
        elif isinstance(assign.value, ast.Call):
            continue
        for tuple_el in ast.walk(assign):
            if isinstance(tuple_el, ast.Tuple) and len(tuple_el.elts) == 1:
                candidates.append((assign.lineno, assign.col_offset))
                break
    if not candidates:
        return []
    for candidate in candidates:
        tokens = tokenize.generate_tokens(lambda L=iter(code): next(L))
        for t in tokens:
            x = TokenInfo(*t)
            if x.start[0] != candidate[0]:
                continue
            if x.type == token.OP and x.string == "=":
                x = TokenInfo(*next(tokens))
                if x.type != token.OP and x.string != "(":
                    x_next = TokenInfo(*next(tokens))
                    if x_next.type == token.OP and x_next.string == ",":
                        errors.append(x.start)
    return errors
开发者ID:ar4s,项目名称:flake8_tuple,代码行数:27,代码来源:flake8_tuple.py

示例8: parse

    def parse(self, source, filename="source"):
        self.clear()
        self.filename = filename
        data = ast.parse(source, filename)
        for a in ast.walk(data):
            if isinstance(a, _ast.Assign) and isinstance(a.targets[0], _ast.Name):
                name = a.targets[0].id
                if name == self.stored_dict_name:
                    if isinstance(a.value, _ast.Dict):
                        for x in a.value.values:
                            if isinstance(x, _ast.Str):
                                self.stored_raw.append((x.s, x.lineno, x.col_offset))
                            else:
                                print "Error in %s:%d:%d: nonstring dict value" % (
                                    self.filename,
                                    x.lineno,
                                    x.col_offset,
                                )

        if not self.using_function:
            for a in ast.walk(data):
                if isinstance(a, _ast.Subscript) and isinstance(a.value, _ast.Name):
                    name = a.value.id
                    if name == self.retrieved_dict_name:
                        if hasattr(a.slice, "value") and isinstance(a.slice.value, _ast.Str):
                            x = a.slice.value
                            self.retrieved_raw.append((x.s, x.lineno, x.col_offset))
        else:
            for a in ast.walk(data):
                if isinstance(a, _ast.Call) and isinstance(a.func, _ast.Name):
                    name = a.func.id
                    if name == self.retrieved_dict_name:
                        if len(a.args) == 1 and isinstance(a.args[0], _ast.Str):
                            x = a.args[0]
                            self.retrieved_raw.append((x.s, x.lineno, x.col_offset))
                        else:
                            print "Suspicious use of '%s' in %s:%d:%d" % (
                                self.retrieved_dict_name,
                                self.filename,
                                a.lineno,
                                a.col_offset,
                            )

        for item in self.stored_raw:
            if item[0] in self.stored:
                self.stored[item[0]].append(item)
            else:
                self.stored[item[0]] = [item]

        for item in self.retrieved_raw:
            if item[0] in self.retrieved:
                self.retrieved[item[0]].append(item)
            else:
                self.retrieved[item[0]] = [item]

        s_set = set(self.stored.keys())
        r_set = set(self.retrieved.keys())
        self.missing = r_set - s_set
        self.redundant = s_set - r_set
开发者ID:Verlihub,项目名称:python,代码行数:59,代码来源:translation_check.py

示例9: translateFunctions

    def translateFunctions(self, tree):
        for node in ast.walk(tree):
            if isinstance(node, ast.FunctionDef):
                self.forwardDeclaration(node)

        for node in ast.walk(tree):
            if isinstance(node, ast.FunctionDef):
                self.visit(node)
开发者ID:TheGreatTook,项目名称:PLProjectCode,代码行数:8,代码来源:Translator.py

示例10: _find_last_line_for_class

def _find_last_line_for_class(source, classname):
    all_nodes = list(ast.walk(ast.parse(source)))
    classes = [n for n in all_nodes if isinstance(n, ast.ClassDef)]
    our_class = next(c for c in classes if c.name == classname)
    last_line_in_our_class = max(
        getattr(thing, 'lineno', 0) for thing in ast.walk(our_class)
    )
    return last_line_in_our_class
开发者ID:hjwp,项目名称:Book-TDD-Web-Dev-Python,代码行数:8,代码来源:write_to_file.py

示例11: read_setup_py

def read_setup_py(setup_py):
	requirements = set()
	tree = ast.parse(setup_py)
	for node in ast.walk(tree):
		arg = getattr(node, "arg", None)
		if arg in ("requires", "install_requires"):
			for node in ast.walk(node.value):
				if isinstance(node, ast.Str):
					requirements.add(read_package_name(node.s))
	return requirements
开发者ID:akx,项目名称:nocheese,代码行数:10,代码来源:nocheese.py

示例12: has_main

def has_main(filepath):
    with open(filepath, "r") as f:
        source = f.read()

    tree = ast.parse(source)

    first_level_ifs = (n for n in ast.walk(tree) if isinstance(n, _ast.If))

    strs_in_ifs = [n.s for _if in first_level_ifs for n in ast.walk(_if) if isinstance(n, _ast.Str)]

    return "__main__" in strs_in_ifs
开发者ID:josericardo,项目名称:runnablesearch,代码行数:11,代码来源:searcher.py

示例13: hy_eval

def hy_eval(hytree, namespace=None, module_name=None, ast_callback=None):
    """``eval`` evaluates a quoted expression and returns the value. The optional
    second and third arguments specify the dictionary of globals to use and the
    module name. The globals dictionary defaults to ``(local)`` and the module
    name defaults to the name of the current module.

       => (eval '(print "Hello World"))
       "Hello World"

    If you want to evaluate a string, use ``read-str`` to convert it to a
    form first:

       => (eval (read-str "(+ 1 1)"))
       2"""
    if namespace is None:
        frame = inspect.stack()[1][0]
        namespace = inspect.getargvalues(frame).locals
    if module_name is None:
        m = inspect.getmodule(inspect.stack()[1][0])
        module_name = '__eval__' if m is None else m.__name__

    foo = HyObject()
    foo.start_line = 0
    foo.end_line = 0
    foo.start_column = 0
    foo.end_column = 0
    replace_hy_obj(hytree, foo)

    if not isinstance(module_name, string_types):
        raise HyTypeError(foo, "Module name must be a string")

    _ast, expr = hy_compile(hytree, module_name, get_expr=True)

    # Spoof the positions in the generated ast...
    for node in ast.walk(_ast):
        node.lineno = 1
        node.col_offset = 1

    for node in ast.walk(expr):
        node.lineno = 1
        node.col_offset = 1

    if ast_callback:
        ast_callback(_ast, expr)

    if not isinstance(namespace, dict):
        raise HyTypeError(foo, "Globals must be a dictionary")

    # Two-step eval: eval() the body of the exec call
    eval(ast_compile(_ast, "<eval_body>", "exec"), namespace)

    # Then eval the expression context and return that
    return eval(ast_compile(expr, "<eval>", "eval"), namespace)
开发者ID:tuturto,项目名称:hy,代码行数:53,代码来源:importer.py

示例14: compare

    def compare(self):
        if not self.solution.strip():
            return False

        try:
            root = ast.parse(self.solution)
        except:
            return False

        soln_nodes = [ type(n).__name__ for n in ast.walk(root) ]
        key_nodes = [ type(n).__name__ for n in ast.walk(ast.parse(self.answer_key)) ]
        
        return soln_nodes == key_nodes
开发者ID:chriszf,项目名称:drillsergeant,代码行数:13,代码来源:model.py

示例15: fix_linenumbers_ast

 def fix_linenumbers_ast(self, tree):
     # Hack because of Python bug(?).  When creating the tree, some nodes do 
     # not have the _attributes field.
     for node in ast.walk(tree):
         if not hasattr(node, '_attributes'):
             node._attributes = ()
     
     tree = ast.fix_missing_locations(tree)
     
     for node in ast.walk(tree):
         if isinstance(node, (ast.stmt, ast.expr)):
             node.lineno -= 1
     
     return tree
开发者ID:bmbouter,项目名称:CeleryManagement,代码行数:14,代码来源:parser.py


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