python类NodeVisitor()的实例源码

utils.py 文件源码 项目:bothub-sdk-python 作者: bothub-studio 项目源码 文件源码 阅读 19 收藏 0 点赞 0 评论 0
def get_decorators(cls):
    decorators = {}

    def visit_FunctionDef(node):
        decorators[node.name] = []
        for n in node.decorator_list:
            name = ''
            if isinstance(n, ast.Call):
                name = n.func.attr if isinstance(n.func, ast.Attribute) else n.func.id
            else:
                name = n.attr if isinstance(n, ast.Attribute) else n.id

            args = [a.s for a in n.args] if hasattr(n, 'args') else []
            decorators[node.name].append((name, args))

    node_iter = ast.NodeVisitor()
    node_iter.visit_FunctionDef = visit_FunctionDef
    _cls = cls if inspect.isclass(cls) else cls.__class__
    node_iter.visit(ast.parse(inspect.getsource(_cls)))
    return decorators
tf_upgrade.py 文件源码 项目:cancer 作者: yancz1989 项目源码 文件源码 阅读 21 收藏 0 点赞 0 评论 0
def visit_Attribute(self, node):  # pylint: disable=invalid-name
    """Handle bare Attributes i.e. [tf.foo, tf.bar].

    Args:
      node: Node that is of type ast.Attribute
    """
    full_name = self._get_attribute_full_path(node)
    if full_name and full_name.startswith("tf."):
      self._rename_functions(node, full_name)
    if full_name in self._api_change_spec.change_to_function:
      if not hasattr(node, "is_function_for_call"):
        new_text = full_name + "()"
        self._file_edit.add("Changed %r to %r"%(full_name, new_text),
                            node.lineno, node.col_offset, full_name, new_text)

    ast.NodeVisitor.generic_visit(self, node)
tf_upgrade.py 文件源码 项目:dlbench 作者: hclhkbu 项目源码 文件源码 阅读 28 收藏 0 点赞 0 评论 0
def visit_Attribute(self, node):  # pylint: disable=invalid-name
    """Handle bare Attributes i.e. [tf.foo, tf.bar].

    Args:
      node: Node that is of type ast.Attribute
    """
    full_name = self._get_attribute_full_path(node)
    if full_name and full_name.startswith("tf."):
      self._rename_functions(node, full_name)
    if full_name in self._api_change_spec.change_to_function:
      if not hasattr(node, "is_function_for_call"):
        new_text = full_name + "()"
        self._file_edit.add("Changed %r to %r"%(full_name, new_text),
                            node.lineno, node.col_offset, full_name, new_text)

    ast.NodeVisitor.generic_visit(self, node)
tf_upgrade.py 文件源码 项目:Stacked_LSTMS_Highway_Residual_On_TimeSeries_Datasets 作者: praveendareddy21 项目源码 文件源码 阅读 23 收藏 0 点赞 0 评论 0
def visit_Attribute(self, node):  # pylint: disable=invalid-name
    """Handle bare Attributes i.e. [tf.foo, tf.bar].

    Args:
      node: Node that is of type ast.Attribute
    """
    full_name = self._get_attribute_full_path(node)
    if full_name and full_name.startswith("tf."):
      self._rename_functions(node, full_name)
    if full_name in self._api_change_spec.change_to_function:
      if not hasattr(node, "is_function_for_call"):
        new_text = full_name + "()"
        self._file_edit.add("Changed %r to %r"%(full_name, new_text),
                            node.lineno, node.col_offset, full_name, new_text)

    ast.NodeVisitor.generic_visit(self, node)
python_parser.py 文件源码 项目:crc-diagram 作者: IuryAlves 项目源码 文件源码 阅读 24 收藏 0 点赞 0 评论 0
def parse(self):
        """
        The main functionality. It parses a tree from :py:data:`self.path_or_stream`
        and uses :py:data:`ast.NodeVisitor` to iterate over the tree.

        :returns: self
        """
        self.stream.seek(0)
        try:
            with closing(self.stream) as stream:
                tree = ast.parse(stream.read())
        except (SyntaxError,):
            raise ParserException('File {file_} is not a python file'.format(
                file_=self.stream.name
            ))
        else:
            self.visit(tree)
        return self
tf_upgrade.py 文件源码 项目:dnn-quant 作者: euclidjda 项目源码 文件源码 阅读 23 收藏 0 点赞 0 评论 0
def visit_Attribute(self, node):  # pylint: disable=invalid-name
    """Handle bare Attributes i.e. [tf.foo, tf.bar].

    Args:
      node: Node that is of type ast.Attribute
    """
    full_name = self._get_attribute_full_path(node)
    if full_name and full_name.startswith("tf."):
      self._rename_functions(node, full_name)
    if full_name in self._api_change_spec.change_to_function:
      if not hasattr(node, "is_function_for_call"):
        new_text = full_name + "()"
        self._file_edit.add("Changed %r to %r"%(full_name, new_text),
                            node.lineno, node.col_offset, full_name, new_text)

    ast.NodeVisitor.generic_visit(self, node)
ast_edits.py 文件源码 项目:visual_mpc 作者: febert 项目源码 文件源码 阅读 27 收藏 0 点赞 0 评论 0
def visit_Attribute(self, node):  # pylint: disable=invalid-name
    """Handle bare Attributes i.e. [tf.foo, tf.bar].

    Args:
      node: Node that is of type ast.Attribute
    """
    full_name = self._get_attribute_full_path(node)
    if full_name:
      self._rename_functions(node, full_name)
    if full_name in self._api_change_spec.change_to_function:
      if not hasattr(node, "is_function_for_call"):
        new_text = full_name + "()"
        self._file_edit.add("Changed %r to %r"%(full_name, new_text),
                            node.lineno, node.col_offset, full_name, new_text)

    ast.NodeVisitor.generic_visit(self, node)
tf_upgrade.py 文件源码 项目:ram_modified 作者: jtkim-kaist 项目源码 文件源码 阅读 26 收藏 0 点赞 0 评论 0
def visit_Attribute(self, node):  # pylint: disable=invalid-name
    """Handle bare Attributes i.e. [tf.foo, tf.bar].

    Args:
      node: Node that is of type ast.Attribute
    """
    full_name = self._get_attribute_full_path(node)
    if full_name and full_name.startswith("tf."):
      self._rename_functions(node, full_name)
    if full_name in self._api_change_spec.change_to_function:
      if not hasattr(node, "is_function_for_call"):
        new_text = full_name + "()"
        self._file_edit.add("Changed %r to %r"%(full_name, new_text),
                            node.lineno, node.col_offset, full_name, new_text)

    ast.NodeVisitor.generic_visit(self, node)
tptv1.py 文件源码 项目:TerpreT 作者: 51alg 项目源码 文件源码 阅读 22 收藏 0 点赞 0 评论 0
def get_set_vars(root):
    class GetSetVarsVisitor(ast.NodeVisitor):
        def __init__(self):
            self.set_vars = set()

        def visit_Call(self, node):
            if u.is_set_to_call(node):
                self.set_vars.add(get_var_name(node.func.value))

    vis = GetSetVarsVisitor()
    if isinstance(root, list):
        for stmt in root:
            vis.visit(stmt)
    else:
        vis.visit(root)
    return vis.set_vars
frontend_io.py 文件源码 项目:pymake 作者: dtrckd 项目源码 文件源码 阅读 21 收藏 0 点赞 0 评论 0
def get_decorators(cls):
    target = cls
    decorators = {}

    def visit_FunctionDef(node):
        decorators[node.name] = []
        for n in node.decorator_list:
            name = ''
            if isinstance(n, ast.Call):
                name = n.func.attr if isinstance(n.func, ast.Attribute) else n.func.id
            else:
                name = n.attr if isinstance(n, ast.Attribute) else n.id

            decorators[node.name].append(name)

    node_iter = ast.NodeVisitor()
    node_iter.visit_FunctionDef = visit_FunctionDef
    node_iter.visit(ast.parse(inspect.getsource(target)))
    return decorators
tf_upgrade.py 文件源码 项目:Tensorflow-Turitors 作者: Xls1994 项目源码 文件源码 阅读 23 收藏 0 点赞 0 评论 0
def visit_Attribute(self, node):  # pylint: disable=invalid-name
    """Handle bare Attributes i.e. [tf.foo, tf.bar].

    Args:
      node: Node that is of type ast.Attribute
    """
    full_name = self._get_attribute_full_path(node)
    if full_name and full_name.startswith("tf."):
      self._rename_functions(node, full_name)
    if full_name in self._api_change_spec.change_to_function:
      if not hasattr(node, "is_function_for_call"):
        new_text = full_name + "()"
        self._file_edit.add("Changed %r to %r"%(full_name, new_text),
                            node.lineno, node.col_offset, full_name, new_text)

    ast.NodeVisitor.generic_visit(self, node)
parser.py 文件源码 项目:viper 作者: ethereum 项目源码 文件源码 阅读 26 收藏 0 点赞 0 评论 0
def decorate_ast_with_source(_ast, code):

    class MyVisitor(ast.NodeVisitor):
        def visit(self, node):
            self.generic_visit(node)
            node.source_code = code

    MyVisitor().visit(_ast)
tf_upgrade.py 文件源码 项目:cancer 作者: yancz1989 项目源码 文件源码 阅读 20 收藏 0 点赞 0 评论 0
def generic_visit(self, node):
    ast.NodeVisitor.generic_visit(self, node)
tf_upgrade.py 文件源码 项目:dlbench 作者: hclhkbu 项目源码 文件源码 阅读 24 收藏 0 点赞 0 评论 0
def generic_visit(self, node):
    ast.NodeVisitor.generic_visit(self, node)
ast_edits.py 文件源码 项目:ML-Project 作者: Shiam-Chowdhury 项目源码 文件源码 阅读 32 收藏 0 点赞 0 评论 0
def generic_visit(self, node):
    ast.NodeVisitor.generic_visit(self, node)
ast_edits.py 文件源码 项目:ML-Project 作者: Shiam-Chowdhury 项目源码 文件源码 阅读 22 收藏 0 点赞 0 评论 0
def visit_Attribute(self, node):  # pylint: disable=invalid-name
    """Handle bare Attributes i.e. [tf.foo, tf.bar].
    Args:
      node: Node that is of type ast.Attribute
    """
    full_name = self._get_attribute_full_path(node)
    if full_name:
      self._rename_functions(node, full_name)
    if full_name in self._api_change_spec.change_to_function:
      if not hasattr(node, "is_function_for_call"):
        new_text = full_name + "()"
        self._file_edit.add("Changed %r to %r"%(full_name, new_text),
                            node.lineno, node.col_offset, full_name, new_text)

    ast.NodeVisitor.generic_visit(self, node)
tf_upgrade.py 文件源码 项目:Stacked_LSTMS_Highway_Residual_On_TimeSeries_Datasets 作者: praveendareddy21 项目源码 文件源码 阅读 30 收藏 0 点赞 0 评论 0
def generic_visit(self, node):
    ast.NodeVisitor.generic_visit(self, node)
analyzer.py 文件源码 项目:chalice 作者: aws 项目源码 文件源码 阅读 25 收藏 0 点赞 0 评论 0
def visit(self, node):
        # type: (ast.AST) -> None
        inferred_type = self._binder.get_type_for_node(node)
        if isinstance(inferred_type, Boto3ClientMethodCallType):
            self.api_calls.setdefault(inferred_type.service_name, set()).add(
                inferred_type.method_name)
        ast.NodeVisitor.visit(self, node)
analyzer.py 文件源码 项目:chalice 作者: aws 项目源码 文件源码 阅读 25 收藏 0 点赞 0 评论 0
def visit(self, node):
        # type: (Any) -> None
        return ast.NodeVisitor.visit(self, node)
parser.py 文件源码 项目:tdoc 作者: Ryanb58 项目源码 文件源码 阅读 22 收藏 0 点赞 0 评论 0
def parse_file(file_base, file_path, file_name):
    """Parse a python's files contents to extract API documentation."""
    src_path = os.path.join(file_base, file_path, file_name)
    print(src_path)

    with open(src_path) as fp:
        source = fp.read()
        tree = ast.parse(source)

        output_folder = "./docs/"
        dst_path = os.path.join(output_folder, file_path, file_name + ".md")

        os.makedirs(os.path.join(output_folder, file_path), exist_ok=True)

        with open(dst_path, "w") as output_file:

            class FuncLister(ast.NodeVisitor):
                def visit_FunctionDef(self, node):
                    """Function Visitor"""
                    output_file.write(get_function_markdown(node))
                    self.generic_visit(node)

                def visit_ClassDef(self, node):
                    """Class Visitor"""
                    output_file.write(get_class_markdown(node))
                    self.generic_visit(node)

            FuncLister().visit(tree)
ipython_autoimport.py 文件源码 项目:ipython-autoimport 作者: anntzer 项目源码 文件源码 阅读 20 收藏 0 点赞 0 评论 0
def _get_import_cache(ipython):
    """Load a mapping of names to import statements from the IPython history.
    """

    import_cache = {}

    def _format_alias(alias):
        return ("import {0.name} as {0.asname}" if alias.asname
                else "import {0.name}").format(alias)

    class Visitor(ast.NodeVisitor):
        def visit_Import(self, node):
            for alias in node.names:
                (import_cache.setdefault(alias.asname or alias.name, set())
                 .add(_format_alias(alias)))

        def visit_ImportFrom(self, node):
            if node.level:  # Skip relative imports.
                return
            for alias in node.names:
                (import_cache.setdefault(alias.asname or alias.name, set())
                 .add("from {} {}".format(node.module, _format_alias(alias))))

    for _, _, entry in (
            ipython.history_manager.get_tail(
                ipython.history_load_length, raw=False)):
        try:
            parsed = ast.parse(entry)
        except SyntaxError:
            continue
        Visitor().visit(parsed)

    return import_cache
tf_upgrade.py 文件源码 项目:dnn-quant 作者: euclidjda 项目源码 文件源码 阅读 23 收藏 0 点赞 0 评论 0
def generic_visit(self, node):
    ast.NodeVisitor.generic_visit(self, node)
ast_edits.py 文件源码 项目:visual_mpc 作者: febert 项目源码 文件源码 阅读 28 收藏 0 点赞 0 评论 0
def generic_visit(self, node):
    ast.NodeVisitor.generic_visit(self, node)
parse.py 文件源码 项目:atom-tracer 作者: OmarShehata 项目源码 文件源码 阅读 24 收藏 0 点赞 0 评论 0
def visit_Assign(self, node):
        for target in node.targets:
            if(isinstance(target,ast.Tuple)):#this is to handle variables declared on the same line
                for el in target.elts:
                    self.grabVar(el)
            else:
                self.grabVar(target)

        ast.NodeVisitor.generic_visit(self, node)#Make sure to run the original method so the AST module can do its thing
parse.py 文件源码 项目:atom-tracer 作者: OmarShehata 项目源码 文件源码 阅读 38 收藏 0 点赞 0 评论 0
def visit_For(self,node):
        #Iterator variables don't get picked up in the Assign nodes, so we have to find them by looking at for loops
        self.grabVar(node.target)
        #We also keep track of where this for loop starts and ends
        self.forLoops[node.lineno] = {'start':node.lineno,'end':node.body[len(node.body)-1].lineno,'is_arg':False}

        ast.NodeVisitor.generic_visit(self, node)
parse.py 文件源码 项目:atom-tracer 作者: OmarShehata 项目源码 文件源码 阅读 84 收藏 0 点赞 0 评论 0
def visit_FunctionDef(self,node):
        funcStart = node.lineno
        funcEnd = node.body[len(node.body)-1].lineno
        self.functions[node.name] = {'start':funcStart,'end':funcEnd}
        #Grab the arguments
        argList = node.args.args
        for argNode in argList:
            argName = argNode.arg
            varObj = {'name':argName,'line':node.lineno,'start':node.lineno,'col':-1,'end':funcEnd,'is_arg':True}
            self.varList.append(varObj)

        ast.NodeVisitor.generic_visit(self, node)
parse.py 文件源码 项目:atom-tracer 作者: OmarShehata 项目源码 文件源码 阅读 25 收藏 0 点赞 0 评论 0
def visit_ClassDef(self, node):
        classStart = node.lineno
        classEnd = node.body[len(node.body)-1].lineno
        self.classes[node.name] = {'start':classStart,'end':classEnd}

        ast.NodeVisitor.generic_visit(self, node)
parse.py 文件源码 项目:atom-tracer 作者: OmarShehata 项目源码 文件源码 阅读 32 收藏 0 点赞 0 评论 0
def visit_Assign(self, node):
        for target in node.targets:
            if(isinstance(target,ast.Tuple)):#this is to handle variables declared on the same line
                for el in target.elts:
                    self.grabVar(el)
            else:
                self.grabVar(target)
        ast.NodeVisitor.generic_visit(self, node)#Make sure to run the original method so the AST module can do its thing
parse.py 文件源码 项目:atom-tracer 作者: OmarShehata 项目源码 文件源码 阅读 28 收藏 0 点赞 0 评论 0
def visit_FunctionDef(self,node):
        funcStart = node.lineno
        funcEnd = node.body[len(node.body)-1].lineno
        self.functions[node.name] = {'start':funcStart,'end':funcEnd}
        #Grab the arguments
        argList = node.args.args
        for argNode in argList:
            argName = argNode.arg
            varObj = {'name':argName,'line':node.lineno,'start':node.lineno,'col':-1,'end':funcEnd,'is_arg':True}
            self.varList.append(varObj)
        ast.NodeVisitor.generic_visit(self, node)
references.py 文件源码 项目:python-langserver 作者: sourcegraph 项目源码 文件源码 阅读 24 收藏 0 点赞 0 评论 0
def visit_ImportFrom(self, node):
        if node.module and node.module.split(".")[0] == self.name:
            self.result = True

    # Based on ast.NodeVisitor.visit


问题


面经


文章

微信
公众号

扫码关注公众号