Python lib2to3.pgen2.token.NAME Examples
The following are 30
code examples of lib2to3.pgen2.token.NAME().
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example.
You may also want to check out all available functions/classes of the module
lib2to3.pgen2.token
, or try the search function
.
Example #1
Source File: test_refactor.py From CTFCrackTools with GNU General Public License v3.0 | 6 votes |
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])
Example #2
Source File: fix_annotate_json.py From pyannotate with Apache License 2.0 | 6 votes |
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))
Example #3
Source File: test_refactor.py From misp42splunk with GNU Lesser General Public License v3.0 | 6 votes |
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])
Example #4
Source File: fix_annotate_json.py From pyannotate with Apache License 2.0 | 6 votes |
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
Example #5
Source File: __init__.py From retype with MIT License | 6 votes |
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
Example #6
Source File: fix_os_environ_unicode.py From scalyr-agent-2 with Apache License 2.0 | 6 votes |
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()
Example #7
Source File: test_refactor.py From ironpython3 with Apache License 2.0 | 6 votes |
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])
Example #8
Source File: fix_annotate.py From pyannotate with Apache License 2.0 | 6 votes |
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
Example #9
Source File: test_refactor.py From datafari with Apache License 2.0 | 6 votes |
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])
Example #10
Source File: test_refactor.py From Project-New-Reign---Nemesis-Main with GNU General Public License v3.0 | 6 votes |
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])
Example #11
Source File: test_refactor.py From odoo13-x64 with GNU General Public License v3.0 | 6 votes |
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])
Example #12
Source File: test_refactor.py From CTFCrackTools-V2 with GNU General Public License v3.0 | 6 votes |
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])
Example #13
Source File: test_refactor.py From CTFCrackTools-V2 with GNU General Public License v3.0 | 6 votes |
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])
Example #14
Source File: pytree_utils_test.py From yapf with Apache License 2.0 | 6 votes |
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])
Example #15
Source File: test_refactor.py From Imogen with MIT License | 6 votes |
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])
Example #16
Source File: test_refactor.py From Fluid-Designer with GNU General Public License v3.0 | 6 votes |
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])
Example #17
Source File: test_refactor.py From CTFCrackTools with GNU General Public License v3.0 | 6 votes |
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])
Example #18
Source File: test_refactor.py From misp42splunk with GNU Lesser General Public License v3.0 | 6 votes |
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])
Example #19
Source File: test_refactor.py From android_universal with MIT License | 6 votes |
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])
Example #20
Source File: test_refactor.py From oss-ftp with MIT License | 6 votes |
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])
Example #21
Source File: test_refactor.py From Computable with MIT License | 6 votes |
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])
Example #22
Source File: test_refactor.py From ironpython2 with Apache License 2.0 | 6 votes |
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])
Example #23
Source File: __init__.py From retype with MIT License | 5 votes |
def get_offset_and_prefix(body, skip_assignments=False): """Returns the offset after which a statement can be inserted to the `body`. This offset is calculated to come after all imports, and maybe existing (possibly annotated) assignments if `skip_assignments` is True. Also returns the indentation prefix that should be applied to the inserted node. """ assert body.type in (syms.file_input, syms.suite) _offset = 0 prefix = "" for _offset, child in enumerate(body.children): if child.type == syms.simple_stmt: stmt = child.children[0] if stmt.type == syms.expr_stmt: expr = stmt.children if not skip_assignments: break if ( len(expr) != 2 or expr[0].type != token.NAME or expr[1].type != syms.annassign or _eq in expr[1].children ): break elif stmt.type not in (syms.import_name, syms.import_from, token.STRING): break elif child.type == token.INDENT: assert isinstance(child, Leaf) prefix = child.value elif child.type != token.NEWLINE: break prefix, child.prefix = child.prefix, prefix return _offset, prefix
Example #24
Source File: fix_throw.py From blackmamba with MIT License | 5 votes |
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
Example #25
Source File: fix_unicode_keep_u.py From blackmamba with MIT License | 5 votes |
def transform(self, node, results): if node.type == token.NAME: new = node.clone() new.value = _mapping[node.value] return new
Example #26
Source File: __init__.py From retype with MIT License | 5 votes |
def _c_nameconstant(const): return Leaf(token.NAME, repr(const.value))
Example #27
Source File: fix_unicode_keep_u.py From V1EngineeringInc-Docs with Creative Commons Attribution Share Alike 4.0 International | 5 votes |
def transform(self, node, results): if node.type == token.NAME: new = node.clone() new.value = _mapping[node.value] return new
Example #28
Source File: fix_throw.py From V1EngineeringInc-Docs with Creative Commons Attribution Share Alike 4.0 International | 5 votes |
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
Example #29
Source File: fix_unicode_keep_u.py From Tautulli with GNU General Public License v3.0 | 5 votes |
def transform(self, node, results): if node.type == token.NAME: new = node.clone() new.value = _mapping[node.value] return new
Example #30
Source File: fix_throw.py From Tautulli with GNU General Public License v3.0 | 5 votes |
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