本文整理汇总了Python中redbaron.RedBaron.findAll方法的典型用法代码示例。如果您正苦于以下问题:Python RedBaron.findAll方法的具体用法?Python RedBaron.findAll怎么用?Python RedBaron.findAll使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类redbaron.RedBaron
的用法示例。
在下文中一共展示了RedBaron.findAll方法的5个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: insert_output_start_stop_indicators
# 需要导入模块: from redbaron import RedBaron [as 别名]
# 或者: from redbaron.RedBaron import findAll [as 别名]
def insert_output_start_stop_indicators(src):
"""
Insert identifier strings so that output can be segregated from input.
Parameters
----------
src : str
String containing input and output lines.
Returns
-------
str
String with output demarked.
"""
rb = RedBaron(src)
# find lines with trailing comments so we can preserve them properly
lines_with_comments = {}
comments = rb.findAll('comment')
for c in comments:
if c.previous and c.previous.type != 'endl':
lines_with_comments[c.previous] = c
input_block_number = 0
# find all nodes that might produce output
nodes = rb.findAll(lambda identifier: identifier in ['print', 'atomtrailers'])
for r in nodes:
# assume that whatever is in the try block will fail and produce no output
# this way we can properly handle display of error messages in the except
if hasattr(r.parent, 'type') and r.parent.type == 'try':
continue
# Output within if/else statements is not a good idea for docs, because
# we don't know which branch execution will follow and thus where to put
# the output block. Regardless of which branch is taken, though, the
# output blocks must start with the same block number.
if hasattr(r.parent, 'type') and r.parent.type == 'if':
if_block_number = input_block_number
if hasattr(r.parent, 'type') and r.parent.type in ['elif', 'else']:
input_block_number = if_block_number
if is_output_node(r):
# if there was a trailing comment on this line, output goes after it
if r in lines_with_comments:
r = lines_with_comments[r] # r is now the comment
# find the correct node to 'insert_after'
while hasattr(r, 'parent') and not hasattr(r.parent, 'insert'):
r = r.parent
r.insert_after('print(">>>>>%d")\n' % input_block_number)
input_block_number += 1
# curse you, redbaron! stop inserting endl before trailing comments!
for l, c in lines_with_comments.items():
if c.previous and c.previous.type == 'endl':
c.previous.value = ''
return rb.dumps()
示例2: replace_asserts_with_prints
# 需要导入模块: from redbaron import RedBaron [as 别名]
# 或者: from redbaron.RedBaron import findAll [as 别名]
def replace_asserts_with_prints(source_code):
"""
Replace asserts with print statements.
Using RedBaron, replace some assert calls with print statements that print the actual
value given in the asserts.
Depending on the calls, the actual value can be the first or second
argument.
"""
rb = RedBaron(source_code) # convert to RedBaron internal structure
for assert_type in ['assertAlmostEqual', 'assertLess', 'assertGreater', 'assertEqual',
'assert_equal_arrays', 'assertTrue', 'assertFalse']:
assert_nodes = rb.findAll("NameNode", value=assert_type)
for assert_node in assert_nodes:
assert_node = assert_node.parent
remove_redbaron_node(assert_node, 0) # remove 'self' from the call
assert_node.value[0].replace('print')
if assert_type not in ['assertTrue', 'assertFalse']:
remove_redbaron_node(assert_node.value[1], 1) # remove the expected value argument
assert_nodes = rb.findAll("NameNode", value='assert_rel_error')
for assert_node in assert_nodes:
assert_node = assert_node.parent
# If relative error tolerance is specified, there are 4 arguments
if len(assert_node.value[1]) == 4:
remove_redbaron_node(assert_node.value[1], -1) # remove the relative error tolerance
remove_redbaron_node(assert_node.value[1], -1) # remove the expected value
remove_redbaron_node(assert_node.value[1], 0) # remove the first argument which is
# the TestCase
assert_node.value[0].replace("print")
assert_nodes = rb.findAll("NameNode", value='assert_almost_equal')
for assert_node in assert_nodes:
assert_node = assert_node.parent
# If relative error tolerance is specified, there are 3 arguments
if len(assert_node.value[1]) == 3:
remove_redbaron_node(assert_node.value[1], -1) # remove the relative error tolerance
remove_redbaron_node(assert_node.value[1], -1) # remove the expected value
assert_node.value[0].replace("print")
source_code_with_prints = rb.dumps() # get back the string representation of the code
return source_code_with_prints
示例3: remove_raise_skip_tests
# 需要导入模块: from redbaron import RedBaron [as 别名]
# 或者: from redbaron.RedBaron import findAll [as 别名]
def remove_raise_skip_tests(src):
"""
Remove from the code any raise unittest.SkipTest lines since we don't want those in
what the user sees.
"""
rb = RedBaron(src)
raise_nodes = rb.findAll("RaiseNode")
for rn in raise_nodes:
# only the raise for SkipTest
if rn.value[:2].dumps() == 'unittestSkipTest':
rn.parent.value.remove(rn)
return rb.dumps()
示例4: get_method_body
# 需要导入模块: from redbaron import RedBaron [as 别名]
# 或者: from redbaron.RedBaron import findAll [as 别名]
def get_method_body(method_code):
'''Using the RedBaron module, get the body of a method.
Do not want the definition signature line
'''
method_code = '\n' + method_code # For some reason RedBaron has problems with this if
# if it does not start with an empty line
rb = RedBaron(method_code)
def_node = rb.findAll("DefNode")[0] # Look for the 'def' node. Should only be one!
def_node.value.decrease_indentation(8) # so that the code is all the way to the left
return def_node.value.dumps()
示例5: replace_asserts_with_prints
# 需要导入模块: from redbaron import RedBaron [as 别名]
# 或者: from redbaron.RedBaron import findAll [as 别名]
def replace_asserts_with_prints(src):
"""
Replace asserts with print statements.
Using RedBaron, replace some assert calls with print statements that print the actual
value given in the asserts. Depending on the calls, the actual value can be the first or second
argument.
Parameters
----------
src : str
String containing source lines.
Returns
-------
str
String containing source with asserts replaced by prints.
"""
rb = RedBaron(src) # convert to RedBaron internal structure
# findAll is slow, so only check the ones that are present.
base_assert = ['assertAlmostEqual', 'assertLess', 'assertGreater', 'assertEqual',
'assert_equal_arrays', 'assertTrue', 'assertFalse']
used_assert = [item for item in base_assert if item in src]
for assert_type in used_assert:
assert_nodes = rb.findAll("NameNode", value=assert_type)
for assert_node in assert_nodes:
assert_node = assert_node.parent
remove_redbaron_node(assert_node, 0) # remove 'self' from the call
assert_node.value[0].replace('print')
if assert_type not in ['assertTrue', 'assertFalse']:
# remove the expected value argument
remove_redbaron_node(assert_node.value[1], 1)
if 'assert_rel_error' in src:
assert_nodes = rb.findAll("NameNode", value='assert_rel_error')
for assert_node in assert_nodes:
assert_node = assert_node.parent
# If relative error tolerance is specified, there are 4 arguments
if len(assert_node.value[1]) == 4:
# remove the relative error tolerance
remove_redbaron_node(assert_node.value[1], -1)
remove_redbaron_node(assert_node.value[1], -1) # remove the expected value
# remove the first argument which is the TestCase
remove_redbaron_node(assert_node.value[1], 0)
#
assert_node.value[0].replace("print")
if 'assert_almost_equal' in src:
assert_nodes = rb.findAll("NameNode", value='assert_almost_equal')
for assert_node in assert_nodes:
assert_node = assert_node.parent
# If relative error tolerance is specified, there are 3 arguments
if len(assert_node.value[1]) == 3:
# remove the relative error tolerance
remove_redbaron_node(assert_node.value[1], -1)
remove_redbaron_node(assert_node.value[1], -1) # remove the expected value
assert_node.value[0].replace("print")
return rb.dumps()