Exemple #1
0
    def apply(self):
        new_block = self.block.copy()
        new_block.clear()
        vars_to_remove = []

        for inst in self.block.body:
            if inst in self.consts:
                consts = self.consts[inst]

                for key, value in consts.items():
                    if key not in dict(inst.value.kws):
                        continue

                    # collecting data from current variable
                    current_var = [
                        var for name, var in inst.value.kws if name == key
                    ][0]
                    loc = current_var.loc

                    seq, _ = guard(find_build_sequence, self.func_ir,
                                   current_var)
                    if not seq:
                        continue
                    if isinstance(value, list):
                        items = seq
                    elif isinstance(value, dict):
                        items = sum(map(list, seq), [])
                    else:
                        continue

                    # create tuple variable
                    stmt = make_assign(ir.Expr.build_tuple(items=items,
                                                           loc=loc),
                                       new_block.scope,
                                       self.func_ir,
                                       loc,
                                       name=f"{key}_tuple")
                    new_block.append(stmt)

                    # replace variable in call
                    inst.value.kws = [(kw[0],
                                       stmt.target) if kw[0] == key else kw
                                      for kw in inst.value.kws]

                    # save old variable for removing
                    vars_to_remove.append(current_var)

            new_block.append(inst)

        # remove old variables
        for var in vars_to_remove:
            # unsused variables are removed after new block is created b/c
            # remove_unused_recursively should see all del statements of variables
            remove_unused_recursively(var, new_block, self.func_ir)

        return new_block
Exemple #2
0
        def apply(self):
            init_df_stmt = import_function(init_dataframe, self._block, self._func_ir)

            for stmt in self._calls_to_rewrite:
                args = get_call_parameters(call=stmt.value, arg_names=self._df_arg_list)

                old_data = args['data']

                args['data'], args['columns'] = self._extract_dict_args(args, self._func_ir)

                self._replace_call(stmt, init_df_stmt.target, args, self._block, self._func_ir)

                remove_unused_recursively(old_data, self._block, self._func_ir)

            return self._block
Exemple #3
0
    def apply(self):
        for stmt in self._calls_to_rewrite:
            args = get_call_parameters(call=stmt.value,
                                       arg_names=self._df_arg_list)
            old_data = args['data']
            args['data'], args['columns'] = self._extract_dict_args(
                args, self._func_ir)

            args_len = len(args['data'])
            func_name = f'init_dataframe_{args_len}'

            # injected_module = modules[pd_dataframe_ext_module.__name__]
            init_df = getattr(pd_dataframe_ext_module, func_name, None)
            if init_df is None:
                init_df_text = gen_init_dataframe_text(func_name, args_len)
                init_df = gen_init_dataframe_func(
                    func_name, init_df_text, {
                        'numba': numba,
                        'cgutils': cgutils,
                        'signature': signature,
                        'types': types,
                        'get_structure_maps': get_structure_maps,
                        'intrinsic': intrinsic,
                        'DataFrameType': DataFrameType,
                        'ColumnLoc': ColumnLoc,
                        'string_type': string_type,
                        'intrinsic': intrinsic,
                        'fix_df_array': fix_df_array,
                        'fix_df_index': fix_df_index,
                        'init_empty_index': init_empty_index,
                        'EmptyIndexType': EmptyIndexType
                    })

                setattr(pd_dataframe_ext_module, func_name, init_df)
                init_df.__module__ = pd_dataframe_ext_module.__name__
                init_df._defn.__module__ = pd_dataframe_ext_module.__name__

            init_df_stmt = import_function(init_df, self._block, self._func_ir)
            self._replace_call(stmt, init_df_stmt.target, args, self._block,
                               self._func_ir)

            remove_unused_recursively(old_data, self._block, self._func_ir)
            self._pipeline.typingctx.refresh()

        return self._block