Python ast.Dict() Examples
The following are 30
code examples of ast.Dict().
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: pyupgrade.py From pyupgrade with MIT License | 6 votes |
def _replace_typed_class( tokens: List[Token], i: int, call: ast.Call, types: Dict[str, ast.expr], ) -> None: if i > 0 and tokens[i - 1].name in {'INDENT', UNIMPORTANT_WS}: indent = f'{tokens[i - 1].src}{" " * 4}' else: indent = ' ' * 4 # NT = NamedTuple("nt", [("a", int)]) # ^i ^end end = i + 1 while end < len(tokens) and tokens[end].name != 'NEWLINE': end += 1 attrs = '\n'.join(f'{indent}{k}: {_unparse(v)}' for k, v in types.items()) src = f'class {tokens[i].src}({_unparse(call.func)}):\n{attrs}' tokens[i:end] = [Token('CODE', src)]
Example #2
Source File: parser.py From resolwe with Apache License 2.0 | 6 votes |
def get_value(self, node): """Convert value from an AST node.""" if not isinstance(node, ast.Dict): raise TypeError("must be a dictionary") evaluator = SafeEvaluator() try: value = evaluator.run(node) except Exception as ex: # TODO: Handle errors. raise ex try: # Ensure value is a serializable dictionary. value = json.loads(json.dumps(value)) if not isinstance(value, dict): raise TypeError except (TypeError, ValueError): raise TypeError("must be serializable") return value # Possible process metadata.
Example #3
Source File: percent_transformer.py From flynt with MIT License | 6 votes |
def transform_generic(node): """Convert a `BinOp` `%` formatted str with a unknown name on the `node.right` to an f-string. When `node.right` is a Name since we don't know if it's a single var or a dict so we sniff the string. Sniffs the left string for Dict style usage e.g. `"val: %(key_name1)s val2: %(key_name2)s" % some_dict` else (e.g. `"val: %s" % some_var`): Borrow the core logic by injecting the name into a ast.Tuple Returns ast.JoinedStr (f-string), bool: str-in-str """ has_dict_str_format = DICT_PATTERN.findall(node.left.s) if has_dict_str_format: return transform_dict(node), True # if it's just a name then pretend it's tuple to use that code node.right = ast.Tuple(elts=[node.right]) return transform_tuple(node), False
Example #4
Source File: injection_shell.py From bandit with Apache License 2.0 | 6 votes |
def has_shell(context): keywords = context.node.keywords result = False if 'shell' in context.call_keywords: for key in keywords: if key.arg == 'shell': val = key.value if isinstance(val, ast.Num): result = bool(val.n) elif isinstance(val, ast.List): result = bool(val.elts) elif isinstance(val, ast.Dict): result = bool(val.keys) elif isinstance(val, ast.Name) and val.id in ['False', 'None']: result = False elif not six.PY2 and isinstance(val, ast.NameConstant): result = val.value else: result = True return result
Example #5
Source File: rewrite.py From python-netsurv with MIT License | 6 votes |
def pop_format_context(self, expl_expr): """Format the %-formatted string with current format context. The expl_expr should be an ast.Str instance constructed from the %-placeholders created by .explanation_param(). This will add the required code to format said string to .expl_stmts and return the ast.Name instance of the formatted string. """ current = self.stack.pop() if self.stack: self.explanation_specifiers = self.stack[-1] keys = [ast.Str(key) for key in current.keys()] format_dict = ast.Dict(keys, list(current.values())) form = ast.BinOp(expl_expr, ast.Mod(), format_dict) name = "@py_format" + str(next(self.variable_counter)) if self.enable_assertion_pass_hook: self.format_variables.append(name) self.expl_stmts.append(ast.Assign([ast.Name(name, ast.Store())], form)) return ast.Name(name, ast.Load())
Example #6
Source File: rewrite.py From python-netsurv with MIT License | 6 votes |
def pop_format_context(self, expl_expr): """Format the %-formatted string with current format context. The expl_expr should be an ast.Str instance constructed from the %-placeholders created by .explanation_param(). This will add the required code to format said string to .expl_stmts and return the ast.Name instance of the formatted string. """ current = self.stack.pop() if self.stack: self.explanation_specifiers = self.stack[-1] keys = [ast.Str(key) for key in current.keys()] format_dict = ast.Dict(keys, list(current.values())) form = ast.BinOp(expl_expr, ast.Mod(), format_dict) name = "@py_format" + str(next(self.variable_counter)) if self.enable_assertion_pass_hook: self.format_variables.append(name) self.expl_stmts.append(ast.Assign([ast.Name(name, ast.Store())], form)) return ast.Name(name, ast.Load())
Example #7
Source File: django_xss.py From bandit with Apache License 2.0 | 6 votes |
def transform2call(var): if isinstance(var, ast.BinOp): is_mod = isinstance(var.op, ast.Mod) is_left_str = isinstance(var.left, ast.Str) if is_mod and is_left_str: new_call = ast.Call() new_call.args = [] new_call.args = [] if six.PY2: new_call.starargs = None new_call.keywords = None if six.PY2: new_call.kwargs = None new_call.lineno = var.lineno new_call.func = ast.Attribute() new_call.func.value = var.left new_call.func.attr = 'format' if isinstance(var.right, ast.Tuple): new_call.args = var.right.elts elif six.PY2 and isinstance(var.right, ast.Dict): new_call.kwargs = var.right else: new_call.args = [var.right] return new_call
Example #8
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 #9
Source File: rewrite.py From pytest with MIT License | 6 votes |
def pop_format_context(self, expl_expr: ast.expr) -> ast.Name: """Format the %-formatted string with current format context. The expl_expr should be an str ast.expr instance constructed from the %-placeholders created by .explanation_param(). This will add the required code to format said string to .expl_stmts and return the ast.Name instance of the formatted string. """ current = self.stack.pop() if self.stack: self.explanation_specifiers = self.stack[-1] keys = [ast.Str(key) for key in current.keys()] format_dict = ast.Dict(keys, list(current.values())) form = ast.BinOp(expl_expr, ast.Mod(), format_dict) name = "@py_format" + str(next(self.variable_counter)) if self.enable_assertion_pass_hook: self.format_variables.append(name) self.expl_stmts.append(ast.Assign([ast.Name(name, ast.Store())], form)) return ast.Name(name, ast.Load())
Example #10
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 #11
Source File: cmd.py From recipes-py with Apache License 2.0 | 6 votes |
def _apply_imports_to_unparsed_expression(exp_ast, imports): """Attempts to evaluate the code equivalent of `exp_ast`, with `imports` as available symbols. If it's successful, it returns the evaluated object. Otherwise this returns the unparsed code for `exp_ast`. Args: * exp_ast (Union[ast.Name, ast.Attribute, ast.Call]) - The expression to evaluate. * imports (Dict[str, object]) - The symbols to include during the evaluation of `exp_ast`. Returns the evaluation of `exp_ast` if it can successfully evaluate with `imports`. Otherwise this returns the source-code representation of exp_ast as a string. """ assert isinstance(exp_ast, (ast.Name, ast.Attribute, ast.Call)), type(exp_ast) unparsed = _unparse(exp_ast).strip() try: return eval(unparsed, {'__builtins__': None}, imports) except (NameError, AttributeError): return unparsed
Example #12
Source File: setup_info.py From pipenv with MIT License | 6 votes |
def parse_setup_function(self): setup = {} # type: Dict[Any, Any] self.unmap_binops() function_names = self.parse_functions() if "setup" in function_names: setup = self.unparse(function_names["setup"]) keys = list(setup.keys()) if len(keys) == 1 and keys[0] is None: _, setup = setup.popitem() keys = list(setup.keys()) for k in keys: # XXX: Remove unresolved functions from the setup dictionary if isinstance(setup[k], dict): if not setup[k]: continue key = next(iter(setup[k].keys())) val = setup[k][key] if key in function_names and val is None or val == {}: setup.pop(k) return setup
Example #13
Source File: setup_info.py From pipenv with MIT License | 6 votes |
def ast_parse_setup_py(path): # type: (S) -> Dict[Any, Any] ast_analyzer = ast_parse_file(path) setup = {} # type: Dict[Any, Any] ast_analyzer.unmap_binops() function_names = ast_analyzer.parse_functions() if "setup" in function_names: setup = ast_unparse(function_names["setup"], analyzer=ast_analyzer) keys = list(setup.keys()) if len(keys) == 1 and keys[0] is None: _, setup = setup.popitem() keys = list(setup.keys()) for k in keys: # XXX: Remove unresolved functions from the setup dictionary if isinstance(setup[k], dict): if not setup[k]: continue key = next(iter(setup[k].keys())) val = setup[k][key] if key in function_names and val is None or val == {}: setup.pop(k) return setup
Example #14
Source File: setup_info.py From pipenv with MIT License | 6 votes |
def get_initial_info(self): # type: () -> Dict[S, Any] parse_setupcfg = False parse_setuppy = False if self.setup_cfg and self.setup_cfg.exists(): parse_setupcfg = True if self.setup_py and self.setup_py.exists(): parse_setuppy = True if parse_setuppy or parse_setupcfg: with cd(self.base_dir): if parse_setuppy: self.update_from_dict(self.parse_setup_py()) if parse_setupcfg: self.update_from_dict(self.parse_setup_cfg()) if self.name is not None and any( [ self.requires, self.setup_requires, self._extras_requirements, self.build_backend, ] ): return self.as_dict() return self.get_info()
Example #15
Source File: setup_info.py From pipenv with MIT License | 6 votes |
def get_info(self): # type: () -> Dict[S, Any] with cd(self.base_dir): self.run_pyproject() self.build() if self.setup_py and self.setup_py.exists() and self.metadata is None: if not self.requires or not self.name: try: with cd(self.base_dir): self.run_setup() except Exception: with cd(self.base_dir): metadata = self.get_egg_metadata() if metadata: self.populate_metadata(metadata) if self.metadata is None or not self.name: with cd(self.base_dir): metadata = self.get_egg_metadata() if metadata: self.populate_metadata(metadata) return self.as_dict()
Example #16
Source File: setup_info.py From pipenv with MIT License | 6 votes |
def as_dict(self): # type: () -> Dict[STRING_TYPE, Any] prop_dict = { "name": self.name, "version": self.version if self._version else None, "base_dir": self.base_dir, "ireq": self.ireq, "build_backend": self.build_backend, "build_requires": self.build_requires, "requires": self.requires if self._requirements else None, "setup_requires": self.setup_requires, "python_requires": self.python_requires, "extras": self.extras if self._extras_requirements else None, "extra_kwargs": self.extra_kwargs, "setup_cfg": self.setup_cfg, "setup_py": self.setup_py, "pyproject": self.pyproject, } return {k: v for k, v in prop_dict.items() if v}
Example #17
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 #18
Source File: input.py From GYP3 with BSD 3-Clause "New" or "Revised" License | 6 votes |
def CheckNode(node, keypath): if isinstance(node, ast.Dict): dict = {} for key, value in zip(node.keys, node.values): assert isinstance(key, ast.Str) key = key.s if key in dict: raise GypError("Key '" + key + "' repeated at level " + repr(len(keypath) + 1) + " with key path '" + '.'.join(keypath) + "'") kp = list(keypath) # Make a copy of the list for descending this node. kp.append(key) dict[key] = CheckNode(value, kp) return dict elif isinstance(node, ast.List): children = [] for index, child in enumerate(node.elts): kp = list(keypath) # Copy list. kp.append(repr(index)) children.append(CheckNode(child, kp)) return children elif isinstance(node, ast.Str): return node.s elif isinstance(node, ast.Num): return node.n else: raise TypeError("Unknown AST node at key path '" + '.'.join(keypath) + "': " + repr(node))
Example #19
Source File: setup_info.py From pipenv with MIT License | 5 votes |
def get_metadata(path, pkg_name=None, metadata_type=None): # type: (S, Optional[S], Optional[S]) -> Dict[S, Union[S, List[RequirementType], Dict[S, RequirementType]]] wheel_allowed = metadata_type == "wheel" or metadata_type is None egg_allowed = metadata_type == "egg" or metadata_type is None dist = None # type: Optional[Union[DistInfoDistribution, EggInfoDistribution]] if wheel_allowed: dist = get_distinfo_dist(path, pkg_name=pkg_name) if egg_allowed and dist is None: dist = get_egginfo_dist(path, pkg_name=pkg_name) if dist is not None: return get_metadata_from_dist(dist) return {}
Example #20
Source File: setup_info.py From pipenv with MIT License | 5 votes |
def get_metadata_from_wheel(wheel_path): # type: (S) -> Dict[Any, Any] if not isinstance(wheel_path, six.string_types): raise TypeError("Expected string instance, received {0!r}".format(wheel_path)) try: dist = Wheel(wheel_path) except Exception: pass metadata = dist.metadata name = metadata.name version = metadata.version requires = [] extras_keys = getattr(metadata, "extras", []) # type: List[STRING_TYPE] extras = { k: [] for k in extras_keys } # type: Dict[STRING_TYPE, List[RequirementType]] for req in getattr(metadata, "run_requires", []): parsed_req = init_requirement(req) parsed_marker = parsed_req.marker if parsed_marker: extra = get_extra_name_from_marker(parsed_marker) if extra is None: requires.append(parsed_req) continue if extra not in extras: extras[extra] = [] parsed_req = strip_extras_markers_from_requirement(parsed_req) extras[extra].append(parsed_req) else: requires.append(parsed_req) return {"name": name, "version": version, "requires": requires, "extras": extras}
Example #21
Source File: _recompute.py From icontract with MIT License | 5 votes |
def visit_Dict(self, node: ast.Dict) -> Dict[Any, Any]: """Visit keys and values and assemble a dictionary with the results.""" recomputed_dict = dict() # type: Dict[Any, Any] for key, val in zip(node.keys, node.values): assert isinstance(key, ast.AST) assert isinstance(val, ast.AST) recomputed_dict[self.visit(node=key)] = self.visit(node=val) self.recomputed_values[node] = recomputed_dict return recomputed_dict
Example #22
Source File: setup_info.py From pipenv with MIT License | 5 votes |
def _prepare_wheel_building_kwargs( ireq=None, # type: Optional[InstallRequirement] src_root=None, # type: Optional[STRING_TYPE] src_dir=None, # type: Optional[STRING_TYPE] editable=False, # type: bool ): # type: (...) -> Dict[STRING_TYPE, STRING_TYPE] download_dir = os.path.join(CACHE_DIR, "pkgs") # type: STRING_TYPE mkdir_p(download_dir) wheel_download_dir = os.path.join(CACHE_DIR, "wheels") # type: STRING_TYPE mkdir_p(wheel_download_dir) if src_dir is None: if editable and src_root is not None: src_dir = src_root elif src_root is not None: src_dir = _get_src_dir(root=src_root) # type: STRING_TYPE else: src_dir = create_tracked_tempdir(prefix="reqlib-src") # Let's always resolve in isolation if src_dir is None: src_dir = create_tracked_tempdir(prefix="reqlib-src") build_dir = create_tracked_tempdir(prefix="reqlib-build") return { "build_dir": build_dir, "src_dir": src_dir, "download_dir": download_dir, "wheel_download_dir": wheel_download_dir, }
Example #23
Source File: setup_info.py From pipenv with MIT License | 5 votes |
def __init__(self): self.name_types = [] self.function_map = {} # type: Dict[Any, Any] self.function_names = {} self.resolved_function_names = {} self.functions = [] self.strings = [] self.assignments = {} self.binOps = [] self.binOps_map = {} self.recurse = True super(Analyzer, self).__init__()
Example #24
Source File: expression.py From hadrian with Apache License 2.0 | 5 votes |
def _literal(expr, state): if isinstance(expr, ast.Dict): out = OrderedDict() for key, value in zip(expr.keys, expr.values): if isinstance(key, ast.Str) or (isinstance(key, ast.Name) and isinstance(state["subs"].get(key.id, None), basestring)): kkey = _literal(key, state) vvalue = _literal(value, state) out[kkey] = vvalue else: raise PythonToPfaException("literal JSON keys must be strings or subs identifiers, not {0} (source line {1})".format(ast.dump(expr), expr.lineno)) return out elif isinstance(expr, ast.Num): return expr.n elif isinstance(expr, ast.Str): return expr.s elif isinstance(expr, ast.Name): if expr.id in state["subs"]: return state["subs"][expr.id] elif expr.id == "None": return None elif expr.id == "True": return True elif expr.id == "False": return False else: raise PythonToPfaException("identifiers ({0}) are not allowed in a literal expression, unless they are subs identifiers (source line {1})".format(ast.dump(expr), expr.lineno)) elif isinstance(expr, ast.List): out = [] for value in expr.elts: out.append(_literal(value, state)) return out raise PythonToPfaException("Python AST node {0} (source line {1}) is not literal JSON".format(ast.dump(expr), expr.lineno))
Example #25
Source File: _recompute.py From icontract with MIT License | 5 votes |
def __init__(self, variable_lookup: List[Mapping[str, Any]]) -> None: """ Initialize. :param variable_lookup: list of lookup tables to look-up the values of the variables, sorted by precedence """ # Resolve precedence of variable lookup self._name_to_value = dict() # type: Dict[str, Any] for lookup in variable_lookup: for name, value in lookup.items(): if name not in self._name_to_value: self._name_to_value[name] = value # value assigned to each visited node self.recomputed_values = dict() # type: Dict[ast.AST, Any]
Example #26
Source File: setup_info.py From pipenv with MIT License | 5 votes |
def as_dict(self): # type: () -> Dict[STRING_TYPE, Optional[PkgResourcesRequirement]] return {self.name: self.requirement}
Example #27
Source File: setup_info.py From pipenv with MIT License | 5 votes |
def as_dict(self): # type: () -> Dict[STRING_TYPE, Tuple[RequirementType, ...]] return {self.name: tuple([r.requirement for r in self.requirements])}
Example #28
Source File: setup_info.py From pipenv with MIT License | 5 votes |
def extras(self): # type: () -> Dict[S, Optional[Any]] if self._extras_requirements is None: self._extras_requirements = () self.get_info() extras_dict = {} extras = set(self._extras_requirements) for section, deps in extras: if isinstance(deps, BaseRequirement): extras_dict[section] = deps.requirement elif isinstance(deps, (list, tuple)): extras_dict[section] = [d.requirement for d in deps] return extras_dict
Example #29
Source File: setup_info.py From pipenv with MIT License | 5 votes |
def parse_setup_cfg(self): # type: () -> Dict[STRING_TYPE, Any] if self.setup_cfg is not None and self.setup_cfg.exists(): contents = self.setup_cfg.read_text() base_dir = self.setup_cfg.absolute().parent.as_posix() try: parsed = setuptools_parse_setup_cfg(self.setup_cfg.as_posix()) except Exception: if six.PY2: contents = self.setup_cfg.read_bytes() parsed = parse_setup_cfg(contents, base_dir) if not parsed: return {} return parsed return {}
Example #30
Source File: rewrite.py From pytest with MIT License | 5 votes |
def _assert_expr_to_lineno(self) -> Dict[int, str]: return _get_assertion_exprs(self.source)