def separate_assing_nodes(self, node: _ast.Assign, variables_names, variables): if isinstance(node.targets[0], _ast.Tuple): tuple_key = meta.values_for_ast_type[type(node.targets[0])] for num, target_node in enumerate( getattr(node.targets[0], tuple_key)): if isinstance(node.value, _ast.Tuple): inner_value = getattr(node.value, tuple_key)[num] else: node_value = self.get_value(node.value, variables_names, variables) if isinstance(node_value, dict): inner_value = _ast.Subscript( value=node.value, slice=_ast.Index(value=_ast.Num(n=num))) else: inner_value = node_value var = _ast.Assign(targets=[target_node], value=inner_value) yield var elif isinstance(node.value, _ast.List): for target in node.targets: for elem in node.value.elts: # for items var = _ast.Assign(targets=[target], value=elem) yield var elif isinstance(node.value, _ast.Call): value = self.get_value(node.value, variables_names, variables) for target in node.targets: # for items in call result var = _ast.Assign(targets=[target], value=value) yield var
def format_slice(self, index, kw): if isinstance(index, _ast.Tuple): dims = [] have_slice = False for dim in index.elts: if not isinstance(dim, _ast.Slice): dim = _ast.Index(value=dim, **kw) else: have_slice = True dims.append(dim) if have_slice: index = _ast.ExtSlice(dims=dims, **kw) else: index = _ast.Index(value=index, **kw) elif not isinstance(index, _ast.Slice): index = _ast.Index(value=index, **kw) return index
def _assign_list_form_variables(self, where_function_ast_node): copy_of_body = copy.deepcopy(where_function_ast_node.body) where_function_ast_node.body = [] for assignment_expression in copy_of_body: variable_name = assignment_expression.targets[0].id self.spec_metadata.add_feature_variable(self.feature_name, variable_name) variable_values = assignment_expression.value where_function_ast_node.body.append( _ast.Assign(targets=[ _ast.Subscript( value=_ast.Name(id='injectable_values', ctx=_ast.Load()), slice=_ast.Index(value=_ast.Str(s=variable_name)), ctx=_ast.Store()) ], value=variable_values))
def _assign_matrix_form_variables(self, where_function_ast_node): copy_of_body = copy.deepcopy(where_function_ast_node.body) where_function_ast_node.body = [] variables_and_values = WhereBlockFunctions._get_variables_and_values(copy_of_body) # We might be screwing with line numbers here for variable_name, variable_values in variables_and_values.items(): self.spec_metadata.add_feature_variable(self.feature_name, variable_name) where_function_ast_node.body.append( _ast.Assign( targets=[ _ast.Subscript( value=_ast.Name(id='injectable_values', ctx=_ast.Load()), slice=_ast.Index(value=ast_proxy.ast_str(s=variable_name)), ctx=_ast.Store() ) ], value=_ast.List(elts=variable_values, ctx=_ast.Load()) ))
def index_slice(value: _ast.expr) -> _ast.Index: return _ast.Index(value=value)
def Index(value): return _ast.Index(value)