本文整理汇总了Python中lib2to3.pgen2.token.NAME属性的典型用法代码示例。如果您正苦于以下问题:Python token.NAME属性的具体用法?Python token.NAME怎么用?Python token.NAME使用的例子?那么, 这里精选的属性代码示例或许可以为您提供帮助。您也可以进一步了解该属性所在类lib2to3.pgen2.token
的用法示例。
在下文中一共展示了token.NAME属性的12个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_get_headnode_dict
# 需要导入模块: from lib2to3.pgen2 import token [as 别名]
# 或者: from lib2to3.pgen2.token import NAME [as 别名]
def test_get_headnode_dict(self):
class NoneFix(fixer_base.BaseFix):
pass
class FileInputFix(fixer_base.BaseFix):
PATTERN = "file_input< any * >"
class SimpleFix(fixer_base.BaseFix):
PATTERN = "'name'"
no_head = NoneFix({}, [])
with_head = FileInputFix({}, [])
simple = SimpleFix({}, [])
d = refactor._get_headnode_dict([no_head, with_head, simple])
top_fixes = d.pop(pygram.python_symbols.file_input)
self.assertEqual(top_fixes, [with_head, no_head])
name_fixes = d.pop(token.NAME)
self.assertEqual(name_fixes, [simple, no_head])
for fixes in d.values():
self.assertEqual(fixes, [no_head])
示例2: test_get_headnode_dict
# 需要导入模块: from lib2to3.pgen2 import token [as 别名]
# 或者: from lib2to3.pgen2.token import NAME [as 别名]
def test_get_headnode_dict(self):
class NoneFix(fixer_base.BaseFix):
pass
class FileInputFix(fixer_base.BaseFix):
PATTERN = "file_input< any * >"
class SimpleFix(fixer_base.BaseFix):
PATTERN = "'name'"
no_head = NoneFix({}, [])
with_head = FileInputFix({}, [])
simple = SimpleFix({}, [])
d = refactor._get_headnode_dict([no_head, with_head, simple])
top_fixes = d.pop(pygram.python_symbols.file_input)
self.assertEqual(top_fixes, [with_head, no_head])
name_fixes = d.pop(token.NAME)
self.assertEqual(name_fixes, [simple, no_head])
for fixes in d.itervalues():
self.assertEqual(fixes, [no_head])
示例3: _BuildSimpleTree
# 需要导入模块: from lib2to3.pgen2 import token [as 别名]
# 或者: from lib2to3.pgen2.token import NAME [as 别名]
def _BuildSimpleTree(self):
# Builds a simple tree we can play with in the tests.
# The tree looks like this:
#
# suite:
# LPAR
# LPAR
# simple_stmt:
# NAME('foo')
#
lpar1 = pytree.Leaf(token.LPAR, '(')
lpar2 = pytree.Leaf(token.LPAR, '(')
simple_stmt = pytree.Node(_GRAMMAR_SYMBOL2NUMBER['simple_stmt'],
[pytree.Leaf(token.NAME, 'foo')])
return pytree.Node(_GRAMMAR_SYMBOL2NUMBER['suite'],
[lpar1, lpar2, simple_stmt])
示例4: transform
# 需要导入模块: from lib2to3.pgen2 import token [as 别名]
# 或者: from lib2to3.pgen2.token import NAME [as 别名]
def transform(self, node, results):
next_sibling = node.next_sibling
if (
next_sibling
and next_sibling.type == token.EQUAL
and next_sibling.value == "="
):
return
prev_sibling = node.prev_sibling
if prev_sibling and prev_sibling.type == token.NAME:
if prev_sibling.value in ("del", "in"):
return
method = results["method"]
method_node = method.parent
if str(method_node.next_sibling) in (".clear", ".update"):
return
libmodernize.touch_import("scalyr_agent", "compat", node)
head = results["head"]
head.value = head.value.replace("os", "compat")
method.value = "os_environ_unicode"
node.changed()
示例5: _r_classdef
# 需要导入模块: from lib2to3.pgen2 import token [as 别名]
# 或者: from lib2to3.pgen2.token import NAME [as 别名]
def _r_classdef(cls, node, flags):
assert node.type in (syms.file_input, syms.suite)
name = Leaf(token.NAME, cls.name)
for child in flatten_some(node.children):
if child.type == syms.decorated:
# skip decorators
child = child.children[1]
if child.type == syms.classdef and child.children[1] == name:
cls_node = child.children[-1]
break
else:
raise ValueError(f"Class {name.value!r} not found in source.")
result = []
for ast_elem in cls.body:
result.extend(reapply(ast_elem, cls_node, flags))
return result
示例6: get_funcname
# 需要导入模块: from lib2to3.pgen2 import token [as 别名]
# 或者: from lib2to3.pgen2.token import NAME [as 别名]
def get_funcname(node):
# type: (Optional[Node]) -> Text
"""Get function name by (approximately) the following rules:
- function -> function_name
- method -> ClassName.function_name
More specifically, we include every class and function name that
the node is a child of, so nested classes and functions get names like
OuterClass.InnerClass.outer_fn.inner_fn.
"""
components = [] # type: List[str]
while node:
if node.type in (syms.classdef, syms.funcdef):
name = node.children[1]
assert name.type == token.NAME, repr(name)
assert isinstance(name, Leaf) # Same as previous, for mypy
components.append(name.value)
node = node.parent
return '.'.join(reversed(components))
示例7: make_annotation
# 需要导入模块: from lib2to3.pgen2 import token [as 别名]
# 或者: from lib2to3.pgen2.token import NAME [as 别名]
def make_annotation(self, node, results):
name = results['name']
assert isinstance(name, Leaf), repr(name)
assert name.type == token.NAME, repr(name)
funcname = get_funcname(node)
res = self.get_annotation_from_stub(node, results, funcname)
# If we couldn't find an annotation and this is a classmethod or
# staticmethod, try again with just the funcname, since the
# type collector can't figure out class names for those.
# (We try with the full name above first so that tools that *can* figure
# that out, like dmypy suggest, can use it.)
if not res:
decs = self.get_decorators(node)
if 'staticmethod' in decs or 'classmethod' in decs:
res = self.get_annotation_from_stub(node, results, name.value)
return res
示例8: get_decorators
# 需要导入模块: from lib2to3.pgen2 import token [as 别名]
# 或者: from lib2to3.pgen2.token import NAME [as 别名]
def get_decorators(self, node):
"""Return a list of decorators found on a function definition.
This is a list of strings; only simple decorators
(e.g. @staticmethod) are returned.
If the function is undecorated or only non-simple decorators
are found, return [].
"""
if node.parent is None:
return []
results = {}
if not self.decorated.match(node.parent, results):
return []
decorators = results.get('dd') or [results['d']]
decs = []
for d in decorators:
for child in d.children:
if isinstance(child, Leaf) and child.type == token.NAME:
decs.append(child.value)
return decs
示例9: transform
# 需要导入模块: from lib2to3.pgen2 import token [as 别名]
# 或者: from lib2to3.pgen2.token import NAME [as 别名]
def transform(self, node, results):
syms = self.syms
exc, val, trc = (results[u"exc"], results[u"val"], results[u"trc"])
val = val[0] if val else Leaf(token.NAME, u"None")
val.prefix = trc.prefix = u" "
kids = [exc.clone(), Comma(), val.clone(), Comma(), trc.clone()]
args = results[u"args"]
args.children = kids
示例10: transform
# 需要导入模块: from lib2to3.pgen2 import token [as 别名]
# 或者: from lib2to3.pgen2.token import NAME [as 别名]
def transform(self, node, results):
if node.type == token.NAME:
new = node.clone()
new.value = _mapping[node.value]
return new
示例11: test_used_and_unused
# 需要导入模块: from lib2to3.pgen2 import token [as 别名]
# 或者: from lib2to3.pgen2.token import NAME [as 别名]
def test_used_and_unused(self):
source = (
"from lib2to3.fixer_util import *\n"
"from lib2to3.pytree import *\n"
"from lib2to3.pgen2 import token\n"
"BlankLine, FromImport, Leaf, Newline, Node\n"
"token.NAME, token.STAR\n"
)
expected_unused_imports = [
{
"lineno": 1,
"module": lib2to3.fixer_util,
"name": "lib2to3.fixer_util",
"star": True,
"modules": [
"BlankLine",
"FromImport",
"Leaf", #
"Newline",
"Node", #
"token", #
"token.NAME", #
"token.STAR", #
],
},
{
"lineno": 2,
"module": lib2to3.pytree,
"name": "lib2to3.pytree",
"star": True,
"modules": ["Leaf", "Node"],
},
]
self.assertUnimportEqual(source, expected_unused_imports)
示例12: test_used_and_unused_2
# 需要导入模块: from lib2to3.pgen2 import token [as 别名]
# 或者: from lib2to3.pgen2.token import NAME [as 别名]
def test_used_and_unused_2(self):
source = (
"from lib2to3.fixer_util import *\n"
"from lib2to3.pytree import *\n"
"from lib2to3.pgen2.token import *\n"
"BlankLine, FromImport, Leaf, Newline, Node\n"
"NAME, STAR\n"
)
expected_unused_imports = [
{
"lineno": 1,
"name": "lib2to3.fixer_util",
"module": lib2to3.fixer_util,
"star": True,
"modules": [
"BlankLine",
"FromImport",
"Leaf", #
"Newline",
"Node", #
],
},
{
"lineno": 2,
"name": "lib2to3.pytree",
"module": lib2to3.pytree,
"star": True,
"modules": ["Leaf", "Node"],
},
{
"lineno": 3,
"name": "lib2to3.pgen2.token",
"star": True,
"module": lib2to3.pgen2.token,
"modules": ["NAME", "STAR"],
},
]
self.assertUnimportEqual(source, expected_unused_imports)