Python ast.List() Examples
The following are 30
code examples of ast.List().
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
ast
, or try the search function
.
Example #1
Source File: inflect.py From pybids with MIT License | 7 votes |
def _get_value_from_ast(self, obj): """ Return the value of the ast object. """ if isinstance(obj, ast.Num): return obj.n elif isinstance(obj, ast.Str): return obj.s elif isinstance(obj, ast.List): return [self._get_value_from_ast(e) for e in obj.elts] elif isinstance(obj, ast.Tuple): return tuple([self._get_value_from_ast(e) for e in obj.elts]) # None, True and False are NameConstants in Py3.4 and above. elif sys.version_info.major >= 3 and isinstance(obj, ast.NameConstant): return obj.value # For python versions below 3.4 elif isinstance(obj, ast.Name) and (obj.id in ["True", "False", "None"]): return string_to_constant[obj.id] # Probably passed a variable name. # Or passed a single word without wrapping it in quotes as an argument # ex: p.inflect("I plural(see)") instead of p.inflect("I plural('see')") raise NameError("name '%s' is not defined" % obj.id)
Example #2
Source File: readers.py From pdm with MIT License | 6 votes |
def _find_sub_setup_call( self, elements ): # type: (List[Any]) -> Tuple[Optional[ast.Call], Optional[List[Any]]] for element in elements: if not isinstance(element, (ast.FunctionDef, ast.If)): continue setup_call = self._find_setup_call(element.body) if setup_call != (None, None): setup_call, body = setup_call body = elements + body return setup_call, body return None, None
Example #3
Source File: pyupgrade.py From pyupgrade with MIT License | 6 votes |
def _fix_percent_format_tuple( tokens: List[Token], start: int, node: ast.BinOp, ) -> None: # TODO: this is overly timid paren = start + 4 if tokens_to_src(tokens[start + 1:paren + 1]) != ' % (': return victims = _victims(tokens, paren, node.right, gen=False) victims.ends.pop() for index in reversed(victims.starts + victims.ends): _remove_brace(tokens, index) newsrc = _percent_to_format(tokens[start].src) tokens[start] = tokens[start]._replace(src=newsrc) tokens[start + 1:paren] = [Token('Format', '.format'), Token('OP', '(')]
Example #4
Source File: _ast_to_ir2.py From tmppy with Apache License 2.0 | 6 votes |
def __init__(self, symbol_table: SymbolTable, custom_types_symbol_table: SymbolTable, external_ir2_symbols_by_name_by_module: Dict[str, Dict[str, Union[ir2.FunctionDefn, ir2.CustomType]]], filename: str, source_lines: List[str], identifier_generator: Iterator[str], function_name: Optional[str] = None, function_definition_line: Optional[int] = None, first_enclosing_except_stmt_line: Optional[int] = None, partially_typechecked_function_definitions_by_name: Dict[str, ast.FunctionDef] = None): assert (function_name is None) == (function_definition_line is None) self.symbol_table = symbol_table self.custom_types_symbol_table = custom_types_symbol_table self.external_ir2_symbols_by_name_by_module = external_ir2_symbols_by_name_by_module self.partially_typechecked_function_definitions_by_name = partially_typechecked_function_definitions_by_name or dict() self.filename = filename self.source_lines = source_lines self.current_function_name = function_name self.current_function_definition_line = function_definition_line self.first_enclosing_except_stmt_line = first_enclosing_except_stmt_line self.identifier_generator = identifier_generator
Example #5
Source File: pyupgrade.py From pyupgrade with MIT License | 6 votes |
def _trim_end(self, tokens: List[Token]) -> 'Block': """the tokenizer reports the end of the block at the beginning of the next block """ i = last_token = self.end - 1 while tokens[i].name in NON_CODING_TOKENS | {'DEDENT', 'NEWLINE'}: # if we find an indented comment inside our block, keep it if ( tokens[i].name in {'NL', 'NEWLINE'} and tokens[i + 1].name == UNIMPORTANT_WS and len(tokens[i + 1].src) > self._initial_indent(tokens) ): break # otherwise we've found another line to remove elif tokens[i].name in {'NL', 'NEWLINE'}: last_token = i i -= 1 return self._replace(end=last_token + 1)
Example #6
Source File: pyupgrade.py From pyupgrade with MIT License | 6 votes |
def _compare_to_3( test: ast.Compare, op: Union[Type[ast.cmpop], Tuple[Type[ast.cmpop], ...]], ) -> bool: if not ( isinstance(test.ops[0], op) and isinstance(test.comparators[0], ast.Tuple) and len(test.comparators[0].elts) >= 1 and all(isinstance(n, ast.Num) for n in test.comparators[0].elts) ): return False # checked above but mypy needs help elts = cast('List[ast.Num]', test.comparators[0].elts) return elts[0].n == 3 and all(n.n == 0 for n in elts[1:])
Example #7
Source File: setup_reader.py From poetry with MIT License | 6 votes |
def _find_sub_setup_call( self, elements ): # type: (List[Any]) -> Tuple[Optional[ast.Call], Optional[List[Any]]] for element in elements: if not isinstance(element, (ast.FunctionDef, ast.If)): continue setup_call = self._find_setup_call(element.body) if setup_call != (None, None): setup_call, body = setup_call body = elements + body return setup_call, body return None, None
Example #8
Source File: readers.py From pdm with MIT License | 6 votes |
def read_from_directory( cls, directory ): # type: (Union[str, Path]) -> Dict[str, Union[List, Dict]] if isinstance(directory, str): directory = Path(directory) result = cls.DEFAULT.copy() for filename in cls.FILES: filepath = directory / filename if not filepath.exists(): continue new_result = getattr(cls(), "read_{}".format(filename.replace(".", "_")))( filepath ) for key in result.keys(): if new_result[key]: result[key] = new_result[key] return result
Example #9
Source File: pyupgrade.py From pyupgrade with MIT License | 6 votes |
def visit_Call(self, node: ast.Call) -> None: if ( isinstance(node.func, ast.Name) and node.func.id == 'set' and len(node.args) == 1 and not node.keywords and isinstance(node.args[0], SET_TRANSFORM) ): arg, = node.args key = _ast_to_offset(node.func) if isinstance(arg, (ast.List, ast.Tuple)) and not arg.elts: self.set_empty_literals[key] = arg else: self.sets[key] = arg elif ( isinstance(node.func, ast.Name) and node.func.id == 'dict' and len(node.args) == 1 and not node.keywords and isinstance(node.args[0], (ast.ListComp, ast.GeneratorExp)) and isinstance(node.args[0].elt, (ast.Tuple, ast.List)) and len(node.args[0].elt.elts) == 2 ): self.dicts[_ast_to_offset(node.func)] = node.args[0] self.generic_visit(node)
Example #10
Source File: setup_reader.py From poetry with MIT License | 6 votes |
def read_from_directory( cls, directory ): # type: (Union[basestring, Path]) -> Dict[str, Union[List, Dict]] if isinstance(directory, basestring): directory = Path(directory) result = cls.DEFAULT.copy() for filename in cls.FILES: filepath = directory / filename if not filepath.exists(): continue new_result = getattr(cls(), "read_{}".format(filename.replace(".", "_")))( filepath ) for key in result.keys(): if new_result[key]: result[key] = new_result[key] return result
Example #11
Source File: pyupgrade.py From pyupgrade with MIT License | 6 votes |
def _process_set_literal( tokens: List[Token], start: int, arg: ast.expr, ) -> None: if _is_wtf('set', tokens, start): return gen = isinstance(arg, ast.GeneratorExp) set_victims = _victims(tokens, start + 1, arg, gen=gen) del set_victims.starts[0] end_index = set_victims.ends.pop() tokens[end_index] = Token('OP', '}') for index in reversed(set_victims.starts + set_victims.ends): _remove_brace(tokens, index) tokens[start:start + 2] = [Token('OP', '{')]
Example #12
Source File: pyupgrade.py From pyupgrade with MIT License | 6 votes |
def _replace_call( tokens: List[Token], start: int, end: int, args: List[Tuple[int, int]], tmpl: str, ) -> None: arg_strs = [_arg_str(tokens, *arg) for arg in args] start_rest = args[0][1] + 1 while ( start_rest < end and tokens[start_rest].name in {'COMMENT', UNIMPORTANT_WS} ): start_rest += 1 rest = tokens_to_src(tokens[start_rest:end - 1]) src = tmpl.format(args=arg_strs, rest=rest) tokens[start:end] = [Token('CODE', src)]
Example #13
Source File: core.py From ibis with Apache License 2.0 | 6 votes |
def indent(lines, spaces=4): """Indent `lines` by `spaces` spaces. Parameters ---------- lines : Union[str, List[str]] A string or list of strings to indent spaces : int The number of spaces to indent `lines` Returns ------- indented_lines : str """ if isinstance(lines, str): text = [lines] text = '\n'.join(lines) return textwrap.indent(text, ' ' * spaces)
Example #14
Source File: helper.py From YAPyPy with MIT License | 6 votes |
def comp_op_rewrite(op: t.Union[Tokenizer, t.List[Tokenizer]]): """ ('<'|'>'|'=='|'>='|'<='|'<>'|'!='|'in'|'not' 'in'|'is'|'is' 'not') """ if isinstance(op, list): op = tuple(map(lambda it: it.value, op)) else: op = op.value return { '<': ast.Lt, '>': ast.Gt, '==': ast.Eq, '>=': ast.GtE, '<=': ast.LtE, '<>': lambda: raise_exp(NotImplemented), '!=': ast.NotEq, 'in': ast.In, ('is', ): ast.Is, ('is', 'not'): ast.IsNot, ('not', 'in'): ast.NotIn, }[op]()
Example #15
Source File: setup_info.py From pipenv with MIT License | 6 votes |
def pep517_subprocess_runner(cmd, cwd=None, extra_environ=None): # type: (List[AnyStr], Optional[AnyStr], Optional[Mapping[S, S]]) -> None """The default method of calling the wrapper subprocess.""" env = os.environ.copy() if extra_environ: env.update(extra_environ) run( cmd, cwd=cwd, env=env, block=True, combine_stderr=True, return_object=False, write_to_stdout=False, nospin=True, )
Example #16
Source File: checker.py From linter-pylama with MIT License | 6 votes |
def TRY(self, node): handler_names = [] # List the exception handlers for i, handler in enumerate(node.handlers): if isinstance(handler.type, ast.Tuple): for exc_type in handler.type.elts: handler_names.append(getNodeName(exc_type)) elif handler.type: handler_names.append(getNodeName(handler.type)) if handler.type is None and i < len(node.handlers) - 1: self.report(messages.DefaultExceptNotLast, handler) # Memorize the except handlers and process the body self.exceptHandlers.append(handler_names) for child in node.body: self.handleNode(child, node) self.exceptHandlers.pop() # Process the other nodes: "except:", "else:", "finally:" self.handleChildren(node, omit='body')
Example #17
Source File: pyupgrade.py From pyupgrade with MIT License | 6 votes |
def _process_set_empty_literal(tokens: List[Token], start: int) -> None: if _is_wtf('set', tokens, start): return i = start + 2 brace_stack = ['('] while brace_stack: token = tokens[i].src if token == BRACES[brace_stack[-1]]: brace_stack.pop() elif token in BRACES: brace_stack.append(token) elif '\n' in token: # Contains a newline, could cause a SyntaxError, bail return i += 1 # Remove the inner tokens del tokens[start + 2:i - 1]
Example #18
Source File: pyupgrade.py From pyupgrade with MIT License | 5 votes |
def _process_dict_comp( tokens: List[Token], start: int, arg: ListCompOrGeneratorExp, ) -> None: if _is_wtf('dict', tokens, start): return dict_victims = _victims(tokens, start + 1, arg, gen=True) elt_victims = _victims(tokens, dict_victims.arg_index, arg.elt, gen=True) del dict_victims.starts[0] end_index = dict_victims.ends.pop() tokens[end_index] = Token('OP', '}') for index in reversed(dict_victims.ends): _remove_brace(tokens, index) # See #6, Fix SyntaxError from rewriting dict((a, b)for a, b in y) if tokens[elt_victims.ends[-1] + 1].src == 'for': tokens.insert(elt_victims.ends[-1] + 1, Token(UNIMPORTANT_WS, ' ')) for index in reversed(elt_victims.ends): _remove_brace(tokens, index) assert elt_victims.first_comma_index is not None tokens[elt_victims.first_comma_index] = Token('OP', ':') for index in reversed(dict_victims.starts + elt_victims.starts): _remove_brace(tokens, index) tokens[start:start + 2] = [Token('OP', '{')]
Example #19
Source File: pyupgrade.py From pyupgrade with MIT License | 5 votes |
def _replace_yield(tokens: List[Token], i: int) -> None: in_token = _find_token(tokens, i, 'in') colon = _find_block_start(tokens, i) block = Block.find(tokens, i, trim_end=True) container = tokens_to_src(tokens[in_token + 1:colon]).strip() tokens[i:block.end] = [Token('CODE', f'yield from {container}\n')]
Example #20
Source File: pyupgrade.py From pyupgrade with MIT License | 5 votes |
def _fixup_dedent_tokens(tokens: List[Token]) -> None: """For whatever reason the DEDENT / UNIMPORTANT_WS tokens are misordered | if True: | if True: | pass | else: |^ ^- DEDENT |+----UNIMPORTANT_WS """ for i, token in enumerate(tokens): if token.name == UNIMPORTANT_WS and tokens[i + 1].name == 'DEDENT': tokens[i], tokens[i + 1] = tokens[i + 1], tokens[i]
Example #21
Source File: pyupgrade.py From pyupgrade with MIT License | 5 votes |
def _unparse(node: ast.expr) -> str: if isinstance(node, ast.Name): return node.id elif isinstance(node, ast.Attribute): return ''.join((_unparse(node.value), '.', node.attr)) elif isinstance(node, ast.Call): return '{}()'.format(_unparse(node.func)) elif isinstance(node, ast.Subscript): if sys.version_info >= (3, 9): # pragma: no cover (py39+) # https://github.com/python/typeshed/pull/3950 node_slice: ast.expr = node.slice # type: ignore elif isinstance(node.slice, ast.Index): # pragma: no cover (<py39) node_slice = node.slice.value else: raise AssertionError(f'expected Slice: {ast.dump(node)}') if isinstance(node_slice, ast.Tuple): if len(node_slice.elts) == 1: slice_s = f'{_unparse(node_slice.elts[0])},' else: slice_s = ', '.join(_unparse(elt) for elt in node_slice.elts) else: slice_s = _unparse(node_slice) return '{}[{}]'.format(_unparse(node.value), slice_s) elif isinstance(node, ast.Str): return repr(node.s) elif isinstance(node, ast.Ellipsis): return '...' elif isinstance(node, ast.List): return '[{}]'.format(', '.join(_unparse(elt) for elt in node.elts)) elif isinstance(node, ast.NameConstant): return repr(node.value) else: raise NotImplementedError(ast.dump(node))
Example #22
Source File: setup_info.py From pipenv with MIT License | 5 votes |
def parse_setup_cfg( setup_cfg_contents, # type: S base_dir, # type: S ): # type: (...) -> Dict[S, Union[S, None, Set[BaseRequirement], List[S], Dict[STRING_TYPE, Tuple[BaseRequirement]]]] default_opts = { "metadata": {"name": "", "version": ""}, "options": { "install_requires": "", "python_requires": "", "build_requires": "", "setup_requires": "", "extras": "", "packages.find": {"where": "."}, }, } parser = configparser.ConfigParser(default_opts) if six.PY2: buff = io.BytesIO(setup_cfg_contents) parser.readfp(buff) else: parser.read_string(setup_cfg_contents) results = {} package_dir = get_package_dir_from_setupcfg(parser, base_dir=base_dir) name, version = get_name_and_version_from_setupcfg(parser, package_dir) results["name"] = name results["version"] = version install_requires = set() # type: Set[BaseRequirement] if parser.has_option("options", "install_requires"): install_requires = make_base_requirements( parser.get("options", "install_requires").split("\n") ) results["install_requires"] = install_requires if parser.has_option("options", "python_requires"): results["python_requires"] = parse_special_directives( parser.get("options", "python_requires"), package_dir ) if parser.has_option("options", "build_requires"): results["build_requires"] = parser.get("options", "build_requires") results["extras_require"] = get_extras_from_setupcfg(parser) return results
Example #23
Source File: pyupgrade.py From pyupgrade with MIT License | 5 votes |
def _arg_str(tokens: List[Token], start: int, end: int) -> str: return tokens_to_src(tokens[start:end]).strip()
Example #24
Source File: pyupgrade.py From pyupgrade with MIT License | 5 votes |
def _fix_format_literal(tokens: List[Token], end: int) -> None: parts = rfind_string_parts(tokens, end) parsed_parts = [] last_int = -1 for i in parts: try: parsed = parse_format(tokens[i].src) except ValueError: # the format literal was malformed, skip it return # The last segment will always be the end of the string and not a # format, slice avoids the `None` format key for _, fmtkey, spec, _ in parsed[:-1]: if ( fmtkey is not None and inty(fmtkey) and int(fmtkey) == last_int + 1 and spec is not None and '{' not in spec ): last_int += 1 else: return parsed_parts.append(tuple(_remove_fmt(tup) for tup in parsed)) for i, parsed in zip(parts, parsed_parts): tokens[i] = tokens[i]._replace(src=unparse_parsed_string(parsed))
Example #25
Source File: pyupgrade.py From pyupgrade with MIT License | 5 votes |
def _fix_extraneous_parens(tokens: List[Token], i: int) -> None: # search forward for another non-coding token i += 1 while tokens[i].name in NON_CODING_TOKENS: i += 1 # if we did not find another brace, return immediately if tokens[i].src != '(': return start = i depth = 1 while depth: i += 1 # found comma or yield at depth 1: this is a tuple / coroutine if depth == 1 and tokens[i].src in {',', 'yield'}: return elif tokens[i].src in OPENING: depth += 1 elif tokens[i].src in CLOSING: depth -= 1 end = i # empty tuple if all(t.name in NON_CODING_TOKENS for t in tokens[start + 1:i]): return # search forward for the next non-coding token i += 1 while tokens[i].name in NON_CODING_TOKENS: i += 1 if tokens[i].src == ')': _remove_brace(tokens, end) _remove_brace(tokens, start)
Example #26
Source File: pyupgrade.py From pyupgrade with MIT License | 5 votes |
def _arg_token_index(tokens: List[Token], i: int, arg: ast.expr) -> int: # lists containing non-tuples report the first element correctly if isinstance(arg, ast.List): # If the first element is a tuple, the ast lies to us about its col # offset. We must find the first `(` token after the start of the # list element. if isinstance(arg.elts[0], ast.Tuple): i = _search_until(tokens, i, arg) return _find_open_paren(tokens, i) else: return _search_until(tokens, i, arg.elts[0]) # others' start position points at their first child node already else: return _search_until(tokens, i, arg)
Example #27
Source File: pyupgrade.py From pyupgrade with MIT License | 5 votes |
def _remove_brace(tokens: List[Token], i: int) -> None: if _is_on_a_line_by_self(tokens, i): del tokens[i - 1:i + 2] else: del tokens[i]
Example #28
Source File: pyupgrade.py From pyupgrade with MIT License | 5 votes |
def _is_on_a_line_by_self(tokens: List[Token], i: int) -> bool: return ( tokens[i - 2].name == 'NL' and tokens[i - 1].name == UNIMPORTANT_WS and tokens[i + 1].name == 'NL' )
Example #29
Source File: pyupgrade.py From pyupgrade with MIT License | 5 votes |
def _find_open_paren(tokens: List[Token], i: int) -> int: return _find_token(tokens, i, '(')
Example #30
Source File: pyupgrade.py From pyupgrade with MIT License | 5 votes |
def _search_until(tokens: List[Token], idx: int, arg: ast.expr) -> int: while ( idx < len(tokens) and not ( tokens[idx].line == arg.lineno and tokens[idx].utf8_byte_offset == arg.col_offset ) ): idx += 1 return idx