本文整理汇总了Python中lib2to3.fixer_util.find_indentation方法的典型用法代码示例。如果您正苦于以下问题:Python fixer_util.find_indentation方法的具体用法?Python fixer_util.find_indentation怎么用?Python fixer_util.find_indentation使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类lib2to3.fixer_util
的用法示例。
在下文中一共展示了fixer_util.find_indentation方法的6个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_nothing
# 需要导入模块: from lib2to3 import fixer_util [as 别名]
# 或者: from lib2to3.fixer_util import find_indentation [as 别名]
def test_nothing(self):
fi = fixer_util.find_indentation
node = parse("node()")
self.assertEqual(fi(node), "")
node = parse("")
self.assertEqual(fi(node), "")
示例2: test_simple
# 需要导入模块: from lib2to3 import fixer_util [as 别名]
# 或者: from lib2to3.fixer_util import find_indentation [as 别名]
def test_simple(self):
fi = fixer_util.find_indentation
node = parse("def f():\n x()")
self.assertEqual(fi(node), "")
self.assertEqual(fi(node.children[0].children[4].children[2]), " ")
node = parse("def f():\n x()\n y()")
self.assertEqual(fi(node.children[0].children[4].children[4]), " ")
示例3: test_nothing
# 需要导入模块: from lib2to3 import fixer_util [as 别名]
# 或者: from lib2to3.fixer_util import find_indentation [as 别名]
def test_nothing(self):
fi = fixer_util.find_indentation
node = parse("node()")
self.assertEqual(fi(node), u"")
node = parse("")
self.assertEqual(fi(node), u"")
示例4: test_simple
# 需要导入模块: from lib2to3 import fixer_util [as 别名]
# 或者: from lib2to3.fixer_util import find_indentation [as 别名]
def test_simple(self):
fi = fixer_util.find_indentation
node = parse("def f():\n x()")
self.assertEqual(fi(node), u"")
self.assertEqual(fi(node.children[0].children[4].children[2]), u" ")
node = parse("def f():\n x()\n y()")
self.assertEqual(fi(node.children[0].children[4].children[4]), u" ")
示例5: transform
# 需要导入模块: from lib2to3 import fixer_util [as 别名]
# 或者: from lib2to3.fixer_util import find_indentation [as 别名]
def transform(self, node, results):
unifunc = results["unifunc"]
strfunc = Name("__str__", prefix=unifunc.prefix)
unifunc.replace(strfunc)
klass = node.clone()
klass.prefix = '\n' + find_indentation(node)
decorator = Node(syms.decorator, [Leaf(token.AT, "@"), Name('python_2_unicode_compatible')])
decorated = Node(syms.decorated, [decorator, klass], prefix=node.prefix)
node.replace(decorated)
touch_import('django.utils.encoding', 'python_2_unicode_compatible', decorated)
示例6: add_py2_annot
# 需要导入模块: from lib2to3 import fixer_util [as 别名]
# 或者: from lib2to3.fixer_util import find_indentation [as 别名]
def add_py2_annot(self, argtypes, restype, node, results):
children = results['suite'][0].children
# Insert '# type: {annot}' comment.
# For reference, see lib2to3/fixes/fix_tuple_params.py in stdlib.
if len(children) >= 1 and children[0].type != token.NEWLINE:
# one liner function
if children[0].prefix.strip() == '':
children[0].prefix = ''
children.insert(0, Leaf(token.NEWLINE, '\n'))
children.insert(
1, Leaf(token.INDENT, find_indentation(node) + ' '))
children.append(Leaf(token.DEDENT, ''))
if len(children) >= 2 and children[1].type == token.INDENT:
degen_str = '(...) -> %s' % restype
short_str = '(%s) -> %s' % (', '.join(argtypes), restype)
if (len(short_str) > 64 or len(argtypes) > 5) and len(short_str) > len(degen_str):
self.insert_long_form(node, results, argtypes)
annot_str = degen_str
else:
annot_str = short_str
children[1].prefix = '%s# type: %s\n%s' % (children[1].value, annot_str,
children[1].prefix)
children[1].changed()
else:
self.log_message("%s:%d: cannot insert annotation for one-line function" %
(self.filename, node.get_lineno()))