本文整理汇总了Python中lib2to3.fixer_util.Name方法的典型用法代码示例。如果您正苦于以下问题:Python fixer_util.Name方法的具体用法?Python fixer_util.Name怎么用?Python fixer_util.Name使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类lib2to3.fixer_util
的用法示例。
在下文中一共展示了fixer_util.Name方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: transform
# 需要导入模块: from lib2to3 import fixer_util [as 别名]
# 或者: from lib2to3.fixer_util import Name [as 别名]
def transform(self, node, results):
import_mod = results.get("module_name")
if import_mod:
mod_name = import_mod.value
new_name = unicode(self.mapping[mod_name])
import_mod.replace(Name(new_name, prefix=import_mod.prefix))
if "name_import" in results:
# If it's not a "from x import x, y" or "import x as y" import,
# marked its usage to be replaced.
self.replace[mod_name] = new_name
if "multiple_imports" in results:
# This is a nasty hack to fix multiple imports on a line (e.g.,
# "import StringIO, urlparse"). The problem is that I can't
# figure out an easy way to make a pattern recognize the keys of
# MAPPING randomly sprinkled in an import statement.
results = self.match(node)
if results:
self.transform(node, results)
else:
# Replace usage of the module.
bare_name = results["bare_with_attr"][0]
new_name = self.replace.get(bare_name.value)
if new_name:
bare_name.replace(Name(new_name, prefix=bare_name.prefix))
示例2: _isCallable
# 需要导入模块: from lib2to3 import fixer_util [as 别名]
# 或者: from lib2to3.fixer_util import Name [as 别名]
def _isCallable(self, node, results):
obj = results["obj"]
return Call(Name("callable"), [obj.clone()], prefix=node.prefix)
示例3: _handle_type2abc
# 需要导入模块: from lib2to3 import fixer_util [as 别名]
# 或者: from lib2to3.fixer_util import Name [as 别名]
def _handle_type2abc(self, node, results, module, abc):
touch_import(None, module, node)
obj = results["obj"]
args = [obj.clone(), String(", " + ".".join([module, abc]))]
return Call(Name("isinstance"), args, prefix=node.prefix)
示例4: transform
# 需要导入模块: from lib2to3 import fixer_util [as 别名]
# 或者: from lib2to3.fixer_util import Name [as 别名]
def transform(self, node, results):
# First, find the sys import. We'll just hope it's global scope.
if "sys_import" in results:
if self.sys_import is None:
self.sys_import = results["sys_import"]
return
func = results["func"].clone()
func.prefix = ""
register = pytree.Node(syms.power,
Attr(Name("atexit"), Name("register"))
)
call = Call(register, [func], node.prefix)
node.replace(call)
if self.sys_import is None:
# That's interesting.
self.warning(node, "Can't find sys import; Please add an atexit "
"import at the top of your file.")
return
# Now add an atexit import after the sys import.
names = self.sys_import.children[1]
if names.type == syms.dotted_as_names:
names.append_child(Comma())
names.append_child(Name("atexit", " "))
else:
containing_stmt = self.sys_import.parent
position = containing_stmt.children.index(self.sys_import)
stmt_container = containing_stmt.parent
new_import = pytree.Node(syms.import_name,
[Name("import"), Name("atexit", " ")]
)
new = pytree.Node(syms.simple_stmt, [new_import])
containing_stmt.insert_child(position + 1, Newline())
containing_stmt.insert_child(position + 2, new)
示例5: test
# 需要导入模块: from lib2to3 import fixer_util [as 别名]
# 或者: from lib2to3.fixer_util import Name [as 别名]
def test(self):
call = parse("foo()", strip_levels=2)
self.assertStr(Attr(Name("a"), Name("b")), "a.b")
self.assertStr(Attr(call, Name("b")), "foo().b")
示例6: test_returns
# 需要导入模块: from lib2to3 import fixer_util [as 别名]
# 或者: from lib2to3.fixer_util import Name [as 别名]
def test_returns(self):
attr = Attr(Name("a"), Name("b"))
self.assertEqual(type(attr), list)
示例7: _Call
# 需要导入模块: from lib2to3 import fixer_util [as 别名]
# 或者: from lib2to3.fixer_util import Name [as 别名]
def _Call(self, name, args=None, prefix=None):
"""Help the next test"""
children = []
if isinstance(args, list):
for arg in args:
children.append(arg)
children.append(Comma())
children.pop()
return Call(Name(name), children, prefix)
示例8: transform
# 需要导入模块: from lib2to3 import fixer_util [as 别名]
# 或者: from lib2to3.fixer_util import Name [as 别名]
def transform(self, node, results):
name = results["name"]
name.replace(Name("cheese", name.prefix))
示例9: transform
# 需要导入模块: from lib2to3 import fixer_util [as 别名]
# 或者: from lib2to3.fixer_util import Name [as 别名]
def transform(self, node, results):
self.warning(node, warn_msg)
return Name(u"getargspec", prefix=node.prefix)
示例10: insert_object
# 需要导入模块: from lib2to3 import fixer_util [as 别名]
# 或者: from lib2to3.fixer_util import Name [as 别名]
def insert_object(node, idx):
node.insert_child(idx, RParen())
node.insert_child(idx, Name(u"object"))
node.insert_child(idx, LParen())
示例11: transform
# 需要导入模块: from lib2to3 import fixer_util [as 别名]
# 或者: from lib2to3.fixer_util import Name [as 别名]
def transform(self, node, results):
name = results[u"name"]
name.replace(Name(u"buffer", prefix=name.prefix))
示例12: transform
# 需要导入模块: from lib2to3 import fixer_util [as 别名]
# 或者: from lib2to3.fixer_util import Name [as 别名]
def transform(self, node, results):
name = results["name"]
touch_import_top(u'builtins', name.value, node)
# name.replace(Name(u"input", prefix=name.prefix))
示例13: transform
# 需要导入模块: from lib2to3 import fixer_util [as 别名]
# 或者: from lib2to3.fixer_util import Name [as 别名]
def transform(self, node, results):
if u"name" in results:
name = results[u"name"]
name.replace(Name(u"getcwdu", prefix=name.prefix))
elif u"bad" in results:
# Can't convert to getcwdu and then expect to catch every use.
self.cannot_convert(node, u"import os, use os.getcwd() instead.")
return
else:
raise ValueError(u"For some reason, the pattern matcher failed.")
示例14: transform
# 需要导入模块: from lib2to3 import fixer_util [as 别名]
# 或者: from lib2to3.fixer_util import Name [as 别名]
def transform(self, node, results):
assert results
base = results.get("base")
attr = results.get("attr")
name = results.get("name")
if base:
if self.shadowed_next:
# Omit this:
# attr.replace(Name("__next__", prefix=attr.prefix))
pass
else:
base = [n.clone() for n in base]
base[0].prefix = ""
node.replace(Call(Name("next", prefix=node.prefix), base))
elif name:
# Omit this:
# n = Name("__next__", prefix=name.prefix)
# name.replace(n)
pass
elif attr:
# We don't do this transformation if we're assigning to "x.next".
# Unfortunately, it doesn't seem possible to do this in PATTERN,
# so it's being done here.
if is_assign_target(node):
head = results["head"]
if "".join([str(n) for n in head]).strip() == '__builtin__':
self.warning(node, bind_warning)
return
# Omit this:
# attr.replace(Name("__next__"))
elif "global" in results:
self.warning(node, bind_warning)
self.shadowed_next = True
### The following functions help test if node is part of an assignment
### target.
示例15: transform
# 需要导入模块: from lib2to3 import fixer_util [as 别名]
# 或者: from lib2to3.fixer_util import Name [as 别名]
def transform(self, node, results):
syms = self.syms
exc = results["exc"].clone()
if exc.type == token.STRING:
msg = "Python 3 does not support string exceptions"
self.cannot_convert(node, msg)
return
# Python 2 supports
# raise ((((E1, E2), E3), E4), E5), V
# as a synonym for
# raise E1, V
# Since Python 3 will not support this, we recurse down any tuple
# literals, always taking the first element.
if is_tuple(exc):
while is_tuple(exc):
# exc.children[1:-1] is the unparenthesized tuple
# exc.children[1].children[0] is the first element of the tuple
exc = exc.children[1].children[0].clone()
exc.prefix = u" "
if "val" not in results:
# One-argument raise
new = pytree.Node(syms.raise_stmt, [Name(u"raise"), exc])
new.prefix = node.prefix
return new
val = results["val"].clone()
if is_tuple(val):
args = [c.clone() for c in val.children[1:-1]]
else:
val.prefix = u""
args = [val]
return pytree.Node(syms.raise_stmt,
[Name(u"raise"), Call(exc, args)],
prefix=node.prefix)