本文整理匯總了Python中ast.walk方法的典型用法代碼示例。如果您正苦於以下問題:Python ast.walk方法的具體用法?Python ast.walk怎麽用?Python ast.walk使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類ast
的用法示例。
在下文中一共展示了ast.walk方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: get_top_imported_names
# 需要導入模塊: import ast [as 別名]
# 或者: from ast import walk [as 別名]
def get_top_imported_names(file: str) -> Set[str]:
"""Collect names imported in given file.
We only collect top-level names, i.e. `from foo.bar import baz`
will only add `foo` to the list.
"""
if not file.endswith(".pyi"):
return set()
with open(os.path.join(file), "rb") as f:
content = f.read()
parsed = ast.parse(content)
top_imported = set()
for node in ast.walk(parsed):
if isinstance(node, ast.Import):
for name in node.names:
top_imported.add(name.name.split('.')[0])
elif isinstance(node, ast.ImportFrom):
if node.level > 0:
# Relative imports always refer to the current package.
continue
assert node.module
top_imported.add(node.module.split('.')[0])
return top_imported
示例2: compile_expression
# 需要導入模塊: import ast [as 別名]
# 或者: from ast import walk [as 別名]
def compile_expression(exp):
cached = cache.get(exp)
if cached is not None:
return cached
_exp = ast.parse(exp)
nodes = [node for node in ast.walk(_exp)]
if len(nodes) < 2 or not isinstance(nodes[1], ast.Expr):
raise ExpressionError("%s is not Expression" % exp)
for node in nodes:
if isinstance(node, ast.Call):
raise ExpressionError("Call method is forbidden")
if isinstance(node, ast.Lambda):
raise ExpressionError("Lambda is strongly forbidden")
result = compile(exp, '<string>', mode='eval')
cache[exp] = result
return result
示例3: get_statement_startend2
# 需要導入模塊: import ast [as 別名]
# 或者: from ast import walk [as 別名]
def get_statement_startend2(lineno, node):
import ast
# flatten all statements and except handlers into one lineno-list
# AST's line numbers start indexing at 1
l = []
for x in ast.walk(node):
if isinstance(x, _ast.stmt) or isinstance(x, _ast.ExceptHandler):
l.append(x.lineno - 1)
for name in "finalbody", "orelse":
val = getattr(x, name, None)
if val:
# treat the finally/orelse part as its own statement
l.append(val[0].lineno - 1 - 1)
l.sort()
insert_index = bisect_right(l, lineno)
start = l[insert_index - 1]
if insert_index >= len(l):
end = None
else:
end = l[insert_index]
return start, end
示例4: get_source_file_contents
# 需要導入模塊: import ast [as 別名]
# 或者: from ast import walk [as 別名]
def get_source_file_contents(self, extension_list, directories_to_skip=None):
file_paths = []
file_contents = []
directories_to_skip = directories_to_skip or []
for dirname, directories_list, filenames in os.walk(self.path, topdown=True):
directories_list[:] = [
d for d in directories_list
if d not in directories_to_skip
]
for filename in filenames:
extension = os.path.splitext(filename)[1]
if extension in extension_list:
file_paths.append(os.path.join(dirname, filename))
for file_path in file_paths:
with open(file_path, 'r', encoding='utf-8') as file_handler:
file_contents.append(file_handler.read())
source_file_contents = list(zip(file_paths, file_contents))
return source_file_contents
示例5: indent_not_multiple_of_tab_size
# 需要導入模塊: import ast [as 別名]
# 或者: from ast import walk [as 別名]
def indent_not_multiple_of_tab_size(project_folder, tab_size, *args, **kwargs):
"""
Since there are cases for which col_offset is computed incorrectly,
this validator must be nothing more than a simple warning.
It compliments the pep8 validator which tends to fail in cases when
the indent is incorrect.
"""
node_types_to_validate = (ast.For, ast.If, ast.FunctionDef, ast.With)
for parsed_file in project_folder.get_parsed_py_files():
lines_offsets = file_helpers.get_line_offsets(parsed_file.content)
for node in ast.walk(parsed_file.ast_tree):
if not ast_helpers.is_node_offset_fine(
node,
lines_offsets,
node_types_to_validate,
tab_size,
):
return parsed_file.get_name_with_line(node.lineno)
示例6: _get_modules
# 需要導入模塊: import ast [as 別名]
# 或者: from ast import walk [as 別名]
def _get_modules(self, content) -> Set[str]:
imports = set()
tree = ast.parse(content)
for node in ast.walk(tree):
if isinstance(node, ast.Import):
for subnode in node.names:
imports.add(subnode.name)
elif isinstance(node, ast.ImportFrom) and node.level == 0:
imports.add(node.module)
modules = set()
for module in imports:
if not module:
continue
module = module.split('.', maxsplit=1)[0]
if module in self.stdlib:
continue
module = self.aliases.get(module, module)
modules.add(module)
return modules
示例7: get_statement_startend2
# 需要導入模塊: import ast [as 別名]
# 或者: from ast import walk [as 別名]
def get_statement_startend2(lineno, node):
import ast
# flatten all statements and except handlers into one lineno-list
# AST's line numbers start indexing at 1
values = []
for x in ast.walk(node):
if isinstance(x, (ast.stmt, ast.ExceptHandler)):
values.append(x.lineno - 1)
for name in ("finalbody", "orelse"):
val = getattr(x, name, None)
if val:
# treat the finally/orelse part as its own statement
values.append(val[0].lineno - 1 - 1)
values.sort()
insert_index = bisect_right(values, lineno)
start = values[insert_index - 1]
if insert_index >= len(values):
end = None
else:
end = values[insert_index]
return start, end
示例8: snoop
# 需要導入模塊: import ast [as 別名]
# 或者: from ast import walk [as 別名]
def snoop(self, _line, cell):
filename = self.shell.compile.cache(cell)
code = self.shell.compile(cell, filename, 'exec')
tracer = snoop()
tracer.variable_whitelist = set()
for node in ast.walk(ast.parse(cell)):
if isinstance(node, ast.Name):
name = node.id
if isinstance(
self.shell.user_global_ns.get(name),
type(ast),
):
# hide modules
continue
tracer.variable_whitelist.add(name)
tracer.target_codes.add(code)
with tracer:
self.shell.ex(code)
示例9: __init__
# 需要導入模塊: import ast [as 別名]
# 或者: from ast import walk [as 別名]
def __init__(self, *args, **kwargs):
super(Source, self).__init__(*args, **kwargs)
if self.tree and self.text:
self.highlighted = ArgDefaultDict(
lambda style: raw_highlight(self.text, style).splitlines()
)
else:
self.lines = defaultdict(lambda: u'SOURCE IS UNAVAILABLE')
self.highlighted = defaultdict(lambda: self.lines)
self.statements = StatementsDict(self)
self.nodes = []
if self.tree:
self.tree._depth = 0
for node in ast.walk(self.tree):
node._tree_index = len(self.nodes)
self.nodes.append(node)
for child in ast.iter_child_nodes(node):
child._depth = node._depth + 1
示例10: ast_names
# 需要導入模塊: import ast [as 別名]
# 或者: from ast import walk [as 別名]
def ast_names(code):
"""Iterator that yields all the (ast) names in a Python expression.
:arg code: A string containing a Python expression.
"""
# Syntax that allows new name bindings to be introduced is tricky to
# handle here, so we just refuse to do so.
disallowed_ast_nodes = (ast.Lambda, ast.ListComp, ast.GeneratorExp)
if sys.version_info >= (2, 7):
disallowed_ast_nodes += (ast.DictComp, ast.SetComp)
for node in ast.walk(ast.parse(code)):
if isinstance(node, disallowed_ast_nodes):
raise PatsyError("Lambda, list/dict/set comprehension, generator "
"expression in patsy formula not currently supported.")
if isinstance(node, ast.Name):
yield node.id
示例11: fields_same
# 需要導入模塊: import ast [as 別名]
# 或者: from ast import walk [as 別名]
def fields_same(n1: ast.AST, n2: ast.AST) -> bool:
for (a1, v1), (a2, v2) in zip(ast.iter_fields(n1), ast.iter_fields(n2)):
# ignore ast attributes, they'll be covered by walk
if a1 != a2:
return False
elif _all_isinstance((v1, v2), ast.AST):
continue
elif _all_isinstance((v1, v2), (list, tuple)):
if len(v1) != len(v2):
return False
# ignore sequences which are all-ast, they'll be covered by walk
elif _all_isinstance(v1, ast.AST) and _all_isinstance(v2, ast.AST):
continue
elif v1 != v2:
return False
elif v1 != v2:
return False
return True
示例12: walk_python_files
# 需要導入模塊: import ast [as 別名]
# 或者: from ast import walk [as 別名]
def walk_python_files():
u'''
Generator that yields all CKAN Python source files.
Yields 2-tuples containing the filename in absolute and relative (to
the project root) form.
'''
def _is_dir_ignored(root, d):
if d.startswith(u'.'):
return True
return os.path.join(rel_root, d) in IGNORED_DIRS
for abs_root, dirnames, filenames in os.walk(PROJECT_ROOT):
rel_root = os.path.relpath(abs_root, PROJECT_ROOT)
if rel_root == u'.':
rel_root = u''
dirnames[:] = [d for d in dirnames if not _is_dir_ignored(rel_root, d)]
for filename in filenames:
if not filename.endswith(u'.py'):
continue
abs_name = os.path.join(abs_root, filename)
rel_name = os.path.join(rel_root, filename)
yield abs_name, rel_name
示例13: get_version
# 需要導入模塊: import ast [as 別名]
# 或者: from ast import walk [as 別名]
def get_version(fname):
"""Get the version info from the mpld3 package without importing it"""
import ast
with open(fname) as init_file:
module = ast.parse(init_file.read())
version = (ast.literal_eval(node.value) for node in ast.walk(module)
if isinstance(node, ast.Assign)
and node.targets[0].id == "__version__")
try:
return next(version)
except StopIteration:
raise ValueError("version could not be located")
#cffi.verifier.cleanup_tmpdir()
示例14: ingest
# 需要導入模塊: import ast [as 別名]
# 或者: from ast import walk [as 別名]
def ingest(self, rootdir):
"""
Collect all the .py files to perform analysis upon
"""
if not os.path.isdir(rootdir):
raise Exception("directory %s passed in is not a dir" % rootdir)
self.__target_dir = rootdir
# walk the dirs/files
for root, subdir, files in os.walk(self.__target_dir):
for f in files:
if f.endswith(".py"):
fullpath = root + os.sep + f
contents = file(fullpath).read()
tree = ast.parse(contents)
self.__fn_to_ast[fullpath] = tree
# potentially analyze .html files for jinja templates
if self.perform_jinja_analysis:
self.__template_dir = self.get_template_dir()
示例15: find_template_dir
# 需要導入模塊: import ast [as 別名]
# 或者: from ast import walk [as 別名]
def find_template_dir(self):
# web-p2 is web-p2/partners/templates
# login is login/templates
# TODO: look for invocations of `jinja2.Environment` and see if
# we can pull the template directory / package from there? Should work
# for most.
template_dirs = set()
for root, subdir, files in os.walk(self.__target_dir):
for fname in files:
fpath = os.path.join(root, fname)
if fname.endswith(".html"):
with open(fpath, "rb") as f:
# Hmm, smells like a jinja template!
if b"{%" in f.read():
template_dirs.add(root)
# If there are multiple template directories in a repo we might need
# repo-specific overrides.
return None if not template_dirs else os.path.commonprefix(template_dirs)