本文整理汇总了Python中ast.iter_child_nodes函数的典型用法代码示例。如果您正苦于以下问题:Python iter_child_nodes函数的具体用法?Python iter_child_nodes怎么用?Python iter_child_nodes使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了iter_child_nodes函数的20个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于我们的系统推荐出更棒的Python代码示例。
示例1: _iter_member_names
def _iter_member_names(self):
'''
iterate over assign and def member names, including class bases
preserves order (bases members goes first)
'''
all_bases_names = set(itertools.chain(*[dir(base) for base in self.__class__.__mro__]))
for base in reversed(self.__class__.__mro__):
if base.__name__ == 'object':
continue
class_src = '\n'.join(l for l in inspect.getsource(base).
split('\n') if not l.lstrip().startswith('#'))
classnodes = ast.iter_child_nodes(ast.parse(dedent(class_src)))
names = []
for classnode in classnodes:
for node in ast.iter_child_nodes(classnode):
if isinstance(node, ast.FunctionDef):
if node.name in Behaviour._omit_member_names:
continue
names += node.name,
if node.name in all_bases_names:
all_bases_names.remove(node.name)
elif isinstance(node, ast.Assign):
for target in node.targets:
if target.id in Behaviour._omit_member_names:
continue
names += target.id,
if target.id in all_bases_names:
all_bases_names.remove(target.id)
for name in names:
if not name in all_bases_names:
yield name
开发者ID:denz,项目名称:bdd-experiment,代码行数:35,代码来源:behaviour.py
示例2: get_testcases
def get_testcases(paths):
"""Walk each path in ``paths`` and return the test cases found.
:param path: List o directories to find test modules and test cases.
:return: A dict mapping a test module path and its test cases.
"""
testmodules = []
for path in paths:
for dirpath, _, filenames in os.walk(path):
for filename in filenames:
if filename.startswith('test_') and filename.endswith('.py'):
testmodules.append(os.path.join(dirpath, filename))
testcases = collections.OrderedDict()
for testmodule in testmodules:
testcases[testmodule] = []
with open(testmodule) as handler:
for node in ast.iter_child_nodes(ast.parse(handler.read())):
if isinstance(node, ast.ClassDef):
# Class test methods
class_name = node.name
testcases[testmodule].extend([
TestFunction(subnode, class_name, testmodule)
for subnode in ast.iter_child_nodes(node)
if isinstance(subnode, ast.FunctionDef) and
subnode.name.startswith('test_')
])
elif (isinstance(node, ast.FunctionDef) and
node.name.startswith('test_')):
# Module's test functions
testcases[testmodule].append(TestFunction(
node, testmodule=testmodule))
return testcases
开发者ID:spoore1,项目名称:testimony,代码行数:32,代码来源:__init__.py
示例3: get_test_methods
def get_test_methods(fname):
seen_classes = {}
out = []
lines = open(fname).readlines()
lines = [x.rstrip('\r\n') for x in lines]
a = ast.parse(("\n".join(lines)).rstrip() + '\n', fname)
for cls in ast.iter_child_nodes(a):
if isinstance(cls, ast.ClassDef) and is_test_class(cls):
if cls.name in seen_classes:
raise ValueError("Duplicate class %s in %s" \
% (cls.name, fname))
seen_classes[cls.name] = {}
seen_methods = {}
for meth in ast.iter_child_nodes(cls):
if isinstance(meth, ast.FunctionDef) \
and meth.name.startswith('test'):
if meth.name in seen_methods:
raise ValueError("Duplicate method %s in %s" \
% (meth.name, fname))
seen_methods[meth.name] = {}
testname = get_test_name(meth, fname, cls.name,
meth.name,
ast.get_docstring(meth, False))
out.append("%s.%s %s" % (cls.name, meth.name, testname))
return out
开发者ID:drussel,项目名称:imp,代码行数:25,代码来源:get_python_tests.py
示例4: get_trait_definition
def get_trait_definition(self):
""" Retrieve the Trait attribute definition
"""
# Get the class source and tokenize it.
source = inspect.getsource(self.parent)
nodes = ast.parse(source)
for node in ast.iter_child_nodes(nodes):
if isinstance(node, ClassDef):
parent_node = node
break
else:
return ''
for node in ast.iter_child_nodes(parent_node):
if isinstance(node, Assign):
name = node.targets[0]
if name.id == self.object_name:
break
else:
return ''
endlineno = name.lineno
for item in ast.walk(node):
if hasattr(item, 'lineno'):
endlineno = max(endlineno, item.lineno)
definition_lines = [
line.strip()
for line in source.splitlines()[name.lineno-1:endlineno]]
definition = ''.join(definition_lines)
equal = definition.index('=')
return definition[equal + 1:].lstrip()
开发者ID:itziakos,项目名称:trait-documenter,代码行数:33,代码来源:trait_documenter.py
示例5: visit_Attribute
def visit_Attribute(self,node):
if self.messagenode is not None:
repnode = list(ast.walk(self.messagenode))
if node in repnode:
self.generic_visit(node)
return
self.messagenode = node
t = ast.iter_child_nodes(node)
res = [[t,0]]
maxcount = 1
while len(res) >= 1:
t = res[-1][0]
for childnode in t:
# print childnode
if isinstance(childnode, _ast.Attribute):
res[-1][1] = 1
else:
res[-1][1] = 0
res.append([ast.iter_child_nodes(childnode),0])
break
else:
maxcount = max(maxcount,sum([flag for (item,flag) in res]) + 2)
res.pop()
continue
# print maxcount
if maxcount >= config['messagechain']:
self.result.append((13,self.fileName,node.lineno,maxcount))
self.generic_visit(node)
开发者ID:njuap,项目名称:python-smells,代码行数:28,代码来源:astChecker.py
示例6: nodes_equal
def nodes_equal(x, y):
__tracebackhide__ = True
assert type(x) == type(y), "Ast nodes do not have the same type: '%s' != '%s' " % (
type(x),
type(y),
)
if isinstance(x, (ast.Expr, ast.FunctionDef, ast.ClassDef)):
assert (
x.lineno == y.lineno
), "Ast nodes do not have the same line number : %s != %s" % (
x.lineno,
y.lineno,
)
assert x.col_offset == y.col_offset, (
"Ast nodes do not have the same column offset number : %s != %s"
% (x.col_offset, y.col_offset)
)
for (xname, xval), (yname, yval) in zip(ast.iter_fields(x), ast.iter_fields(y)):
assert xname == yname, (
"Ast nodes fields differ : %s (of type %s) != %s (of type %s)"
% (xname, type(xval), yname, type(yval))
)
assert type(xval) == type(yval), (
"Ast nodes fields differ : %s (of type %s) != %s (of type %s)"
% (xname, type(xval), yname, type(yval))
)
for xchild, ychild in zip(ast.iter_child_nodes(x), ast.iter_child_nodes(y)):
assert nodes_equal(xchild, ychild), "Ast node children differs"
return True
开发者ID:mitnk,项目名称:xonsh,代码行数:29,代码来源:tools.py
示例7: visit_Subscript
def visit_Subscript(self,node):
if self.subscriptnode is not None:
repnode = list(ast.walk(self.subscriptnode))
if node in repnode:
self.generic_visit(node)
return
self.subscriptnode = node
t = ast.iter_child_nodes(node)
res = [[t,0]]
maxcount = 1
while len(res) >= 1:
t = res[-1][0]
for childnode in t:
# print childnode
if isinstance(childnode, _ast.Subscript) or isinstance(childnode, _ast.Tuple) or \
isinstance(childnode, _ast.Dict) or isinstance(childnode, _ast.List) or isinstance(childnode, _ast.Set):
res[-1][1] = 1
else:
res[-1][1] = 0
res.append([ast.iter_child_nodes(childnode),0])
break
else:
maxcount = max(maxcount,sum([flag for (item,flag) in res]) + 1)
res.pop()
continue
# print maxcount
if maxcount >= config['containerdepth']:
self.result.append((6,self.fileName,node.lineno,maxcount))
self.generic_visit(node)
开发者ID:njuap,项目名称:python-smells,代码行数:29,代码来源:astChecker.py
示例8: process_file
def process_file(self):
result = []
with open(self.path) as f:
current_node = ast.parse(f.read())
pathes = os.path.splitext(self.path)[0]
pathes = pathes.split("/")
base_path = ".".join(pathes)
nodes = [(i, base_path) for i in ast.iter_child_nodes(current_node)]
while len(nodes) > 0:
current_node, path = nodes.pop(0)
if isinstance(current_node, ast.Import):
module = ''
elif isinstance(current_node, ast.ImportFrom):
module = current_node.module
elif isinstance(current_node, ast.FunctionDef) or isinstance(current_node, ast.ClassDef):
path += "." + current_node.name
next_nodes = [(i, path) for i in ast.iter_child_nodes(current_node)]
nodes.extend(next_nodes)
continue
else:
continue
for n in current_node.names:
result.append(self.node_class(module=module, full_path=path, name=n.name, alias=n.asname))
return result
开发者ID:vwvolodya,项目名称:project,代码行数:26,代码来源:Analyzer.py
示例9: tag_class_functions
def tag_class_functions(self, cls_node):
"""Tag functions if they are methods, classmethods, staticmethods"""
# tries to find all 'old style decorators' like
# m = staticmethod(m)
late_decoration = {}
for node in iter_child_nodes(cls_node):
if not (isinstance(node, ast.Assign) and
isinstance(node.value, ast.Call) and
isinstance(node.value.func, ast.Name)):
continue
func_name = node.value.func.id
if func_name in ('classmethod', 'staticmethod'):
meth = (len(node.value.args) == 1 and node.value.args[0])
if isinstance(meth, ast.Name):
late_decoration[meth.id] = func_name
# iterate over all functions and tag them
for node in iter_child_nodes(cls_node):
if not isinstance(node, ast.FunctionDef):
continue
node.function_type = 'method'
if node.name == '__new__':
node.function_type = 'classmethod'
if node.name in late_decoration:
node.function_type = late_decoration[node.name]
elif node.decorator_list:
names = [d.id for d in node.decorator_list
if isinstance(d, ast.Name) and
d.id in ('classmethod', 'staticmethod')]
if names:
node.function_type = names[0]
开发者ID:jimr,项目名称:pep8-naming,代码行数:33,代码来源:pep8ext_naming.py
示例10: visit_ClassDef
def visit_ClassDef(self, node):
self.transforms = {}
self.in_class_define = True
functions_to_promote = []
setup_func = None
for class_func in ast.iter_child_nodes(node):
if isinstance(class_func, ast.FunctionDef):
if class_func.name == 'setup':
setup_func = class_func
for anon_func in ast.iter_child_nodes(class_func):
if isinstance(anon_func, ast.FunctionDef):
functions_to_promote.append(anon_func)
if setup_func:
for func in functions_to_promote:
setup_func.body.remove(func)
func.args.args.insert(0, ast.Name(id='self', ctx=ast.Load()))
node.body.append(func)
self.transforms[func.name] = 'self.' + func.name
ast.fix_missing_locations(node)
self.generic_visit(node)
return node
开发者ID:Axik,项目名称:pandas,代码行数:27,代码来源:vbench_to_asv.py
示例11: visit_Expr
def visit_Expr(self, node):
if self.document is None:
raise ValueError()
children = list(ast.iter_child_nodes(node))
if len(children) != 1:
raise ValueError()
feature_name = None
child = children[0]
if isinstance(child, ast.Attribute) \
and child.attr in self.document.features:
feature_name = child.attr
else:
raise ValueError()
grandchildren = list(ast.iter_child_nodes(child))
if len(grandchildren) != 2:
raise ValueError()
grandchild = grandchildren[0]
if isinstance(grandchild, ast.Name) \
and grandchild.id in self.locals \
and isinstance(self.locals[grandchild.id], self.document):
self.doc = self.locals[grandchild.id]
self.feature_name = feature_name
else:
raise ValueError()
开发者ID:JohnVinyard,项目名称:zounds,代码行数:29,代码来源:featureparser.py
示例12: test_iter_child_nodes
def test_iter_child_nodes(self):
node = ast.parse("spam(23, 42, eggs='leek')", mode='eval')
self.assertEqual(len(list(ast.iter_child_nodes(node.body))), 4)
iterator = ast.iter_child_nodes(node.body)
self.assertEqual(next(iterator).id, 'spam')
self.assertEqual(next(iterator).c, 23)
self.assertEqual(next(iterator).c, 42)
self.assertEqual(ast.dump(next(iterator)),
"keyword(arg='eggs', value=Const(c='leek', constant=pure_const()))"
)
开发者ID:lrq3000,项目名称:wpython2.wpython10,代码行数:10,代码来源:test_ast.py
示例13: walklocal
def walklocal(root):
"""Recursively yield all descendant nodes but not in a different scope"""
todo = collections.deque(ast.iter_child_nodes(root))
yield root, False
while todo:
node = todo.popleft()
newscope = isinstance(node, ast.FunctionDef)
if not newscope:
todo.extend(ast.iter_child_nodes(node))
yield node, newscope
开发者ID:Distrotech,项目名称:mercurial,代码行数:10,代码来源:import-checker.py
示例14: find_global_defs
def find_global_defs(self, func_def_node):
global_names = set()
nodes_to_check = deque(iter_child_nodes(func_def_node))
while nodes_to_check:
node = nodes_to_check.pop()
if isinstance(node, ast.Global):
global_names.update(node.names)
if not isinstance(node, (ast.FunctionDef, ast.ClassDef)):
nodes_to_check.extend(iter_child_nodes(node))
func_def_node.global_names = global_names
开发者ID:jimr,项目名称:pep8-naming,代码行数:11,代码来源:pep8ext_naming.py
示例15: visit_FunctionDef
def visit_FunctionDef(self,node):
# argsCount
def findCharacter(s,d):
try:
value = s.index(d)
except ValueError:
return -1
else:
return value
funcName = node.name.strip()
p = re.compile("^(__[a-zA-Z0-9]+__)$")
if p.match(funcName.strip()) and funcName != "__import__" and funcName != "__all__":
self.defmagic.add((funcName,self.fileName,node.lineno))
stmt = astunparse.unparse(node.args)
arguments = stmt.split(",")
argsCount = 0
for element in arguments:
if findCharacter(element,'=') == -1:
argsCount += 1
self.result.append((1,self.fileName,node.lineno,argsCount))
#function length
lines = set()
res = [node]
while len(res) >= 1:
t = res[0]
for n in ast.iter_child_nodes(t):
if not hasattr(n,'lineno') or ((isinstance(t,_ast.FunctionDef) or isinstance(t,_ast.ClassDef)) and n == t.body[0] and isinstance(n,_ast.Expr)):
continue
lines.add(n.lineno)
if isinstance(n,_ast.ClassDef) or isinstance(n,_ast.FunctionDef):
continue
else:
res.append(n)
del res[0]
self.result.append((2,self.fileName,node.lineno,len(lines)))
#nested scope depth
if node in self.scopenodes:
self.scopenodes.remove(node)
self.generic_visit(node)
return
dep = [[node,1]] #node,nestedlevel
maxlevel = 1
while len(dep) >= 1:
t = dep[0][0]
currentlevel = dep[0][1]
for n in ast.iter_child_nodes(t):
if isinstance(n,_ast.FunctionDef):
self.scopenodes.append(n)
dep.append([n,currentlevel+1])
maxlevel = max(maxlevel,currentlevel)
del dep[0]
if maxlevel>1:
self.result.append((3,self.fileName,node.lineno,maxlevel)) #DOC
self.generic_visit(node)
开发者ID:chenzhifei731,项目名称:Pysmell,代码行数:54,代码来源:astChecker.py
示例16: generic_visit
def generic_visit(self, node):
name=typename(node)
self.debug('debug; visiting: %s'%str(name))
if name == 'Module':
self.graph+='digraph G {'+self.delim
ast.NodeVisitor.generic_visit(self, node)
# TODO: print labels
for n in self.nodenumbers.keys():
self.graph+='%s [label="%s"];%s' % (self.getNodeNumber(n), self.getNodeType(n), self.delim)
self.graph+='}'
elif name == 'FunctionDef':
# todo: number of params
# always create downward links as needed
for n in ast.iter_child_nodes(node):
if typename(n) in ('For','If','Return'):
self.debug('%s -> %s' % (str(name),typename(n)))
self.makeLink(node, n)
ast.NodeVisitor.generic_visit(self, node)
elif name == 'For':
for n in ast.iter_child_nodes(node):
if typename(n) in ('For', 'If', 'Return'):
self.debug('%s -> %s' % (name,typename(n)))
self.makeLink(node, n)
ast.NodeVisitor.generic_visit(self, node)
elif name == 'If':
# check the then branch
found=False
for n in node.body:
if typename(n) in ('If','For','Return'):
found=True
self.makeLink(node,n)
if not found:
self.makeLink(node,node)
if (len(node.orelse)>0):
found=False
for n in node.orelse:
if typename(n) in ('If','For','Return'):
found=True
self.makeLink(node,n)
if not found:
self.graph += '%s -> %s;%s' % (self.getNodeNumber(node), self.getNodeNumber(node), self.delim)
#print('body', node.body, typename(node.body), typename(node.orelse))
for n in ast.iter_child_nodes(node):
self.debug('%s ===> %s' % (str(name), typename(n)))
ast.NodeVisitor.generic_visit(self, node)
elif name == 'Return':
# probably no need for recursive step here???
#print(name, node._fields)
ast.NodeVisitor.generic_visit(self, node)
else:
#print('CATCHALL', name)
ast.NodeVisitor.generic_visit(self, node)
开发者ID:daveho,项目名称:CFOG,代码行数:52,代码来源:simplifiedCFG.py
示例17: nodes_equal
def nodes_equal(x, y):
__tracebackhide__ = True
assert type(x) == type(y)
if isinstance(x, (ast.Expr, ast.FunctionDef, ast.ClassDef)):
assert x.lineno == y.lineno
assert x.col_offset == y.col_offset
for (xname, xval), (yname, yval) in zip(ast.iter_fields(x),
ast.iter_fields(y)):
assert xname == yname
assert type(xval) == type(yval)
for xchild, ychild in zip(ast.iter_child_nodes(x),
ast.iter_child_nodes(y)):
assert nodes_equal(xchild, ychild)
return True
开发者ID:AndreaCrotti,项目名称:xonsh,代码行数:14,代码来源:test_parser.py
示例18: _extract_docstrings
def _extract_docstrings(self, bzl_file):
"""Extracts the docstrings for all public rules in the .bzl file.
This function parses the .bzl file and extracts the docstrings for all
public rules in the file that were extracted in _process_skylark. It calls
_add_rule_doc for to parse the attribute documentation in each docstring
and associate them with the extracted rules and attributes.
Args:
bzl_file: The .bzl file to extract docstrings from.
"""
try:
tree = None
with open(bzl_file) as f:
tree = ast.parse(f.read(), bzl_file)
key = None
for node in ast.iter_child_nodes(tree):
if isinstance(node, ast.Assign):
name = node.targets[0].id
if not name.startswith("_"):
key = name
continue
elif isinstance(node, ast.Expr) and key:
# Python itself does not treat strings defined immediately after a
# global variable definition as a docstring. Only extract string and
# parse as docstring if it is defined.
if hasattr(node.value, 's'):
self._add_rule_doc(key, node.value.s.strip())
key = None
except IOError:
print("Failed to parse {0}: {1}".format(bzl_file, e.strerror))
pass
开发者ID:davidzchen,项目名称:skydoc,代码行数:32,代码来源:rule_extractor.py
示例19: _statement_assign
def _statement_assign(self, env, node):
itr = self._fold_expr(env, node.value)
while itr:
try: yield next(itr)
except StopIteration: break
val = env.pop()
# TODO what else is supposed to hold??
assert len(node.targets) == 1
target = node.targets[0]
# fold the lists
if isinstance(val, list):
l = []
for v in val:
itr = self._fold_expr(env, v)
while itr:
try: yield next(itr)
except StopIteration: break
l.append(env.pop())
val = l
# assign to a list of variables
if (isinstance(target, ast.List) or isinstance(target, ast.Tuple)) and isinstance(val, list):
i = 0
for var in ast.iter_child_nodes(target):
if isinstance(var, ast.Name):
env.setvar(var.id, val[i])
i += 1
else:
env.setvar(target.id, val)
yield node
开发者ID:emanuele-f,项目名称:python-pesci,代码行数:32,代码来源:interpreter.py
示例20: generic_visit
def generic_visit(self, node, inside_call=False, inside_yield=False):
if isinstance(node, ast.Call):
inside_call = True
elif isinstance(node, ast.Yield):
inside_yield = True
elif isinstance(node, ast.Str):
if disable_lookups:
if inside_call and node.s.startswith("__"):
# calling things with a dunder is generally bad at this point...
raise AnsibleError(
"Invalid access found in the conditional: '%s'" % conditional
)
elif inside_yield:
# we're inside a yield, so recursively parse and traverse the AST
# of the result to catch forbidden syntax from executing
parsed = ast.parse(node.s, mode='exec')
cnv = CleansingNodeVisitor()
cnv.visit(parsed)
# iterate over all child nodes
for child_node in ast.iter_child_nodes(node):
self.generic_visit(
child_node,
inside_call=inside_call,
inside_yield=inside_yield
)
开发者ID:ernstp,项目名称:ansible,代码行数:25,代码来源:conditional.py
注:本文中的ast.iter_child_nodes函数示例整理自Github/MSDocs等源码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。 |
请发表评论