Exemplo n.º 1
0
    def match(self, func_ir, block, typemap, calltypes):
        # TODO: check that vars are used only in read_csv

        self.block = block
        self.args = args = []

        # Find all assignments with a right-hand read_csv() call
        for inst in find_operations(block=block, op_name='call'):
            expr = inst.value
            try:
                callee = func_ir.infer_constant(expr.func)
            except errors.ConstantInferenceError:
                continue
            if callee is not pd.read_csv:
                continue
            # collect arguments with list, set and dict
            # in order to replace with tuple
            for key, var in expr.kws:
                if key in self._read_csv_const_args:
                    arg_def = guard(get_definition, func_ir, var)
                    ops = ['build_list', 'build_set', 'build_map']
                    if arg_def.op in ops:
                        args.append(arg_def)

        return len(args) > 0
Exemplo n.º 2
0
    def match(self, func_ir, block, typemap, calltypes):

        self._block = block
        self._func_ir = func_ir
        self._calls_to_rewrite = set()

        # Find all assignments with a RHS expr being a call to dict, and where arg
        # is a call to zip and store these ir.Expr for further modification
        for inst in find_operations(block=block, op_name='call'):
            expr = inst.value
            try:
                callee = func_ir.infer_constant(expr.func)
            except errors.ConstantInferenceError:
                continue

            if (callee is dict and len(expr.args) == 1):
                dict_arg_expr = guard(get_definition, func_ir, expr.args[0])
                if (getattr(dict_arg_expr, 'op', None) == 'call'):
                    called_func = guard(get_definition, func_ir,
                                        dict_arg_expr.func)
                    if (called_func.value is zip
                            and len(dict_arg_expr.args) == 2):
                        self._calls_to_rewrite.add(dict_arg_expr)

        return len(self._calls_to_rewrite) > 0
Exemplo n.º 3
0
    def match(self, func_ir, block, typemap, calltypes):
        # TODO: 1. save instructions of build_map, build_list for read_csv params
        # 2. check that vars are used only in read_csv
        # 3. replace vars with build_tuple inplace

        self.func_ir = func_ir
        self.block = block
        self.consts = consts = {}

        # Find all assignments with a right-hand read_csv() call
        for inst in find_operations(block=block, op_name='call'):
            expr = inst.value
            call = guard(find_callname, func_ir, expr)
            if call not in self._pandas_read_csv_calls:
                continue
            # collect constant parameters with type list and dict
            # in order to replace with tuple
            for key, var in expr.kws:
                if key not in self._read_csv_const_args:
                    continue
                try:
                    const = func_ir.infer_constant(var)
                except errors.ConstantInferenceError:
                    try:
                        const = ConstantInference(func_ir).infer_constant(
                            var.name)
                    except errors.ConstantInferenceError:
                        continue
                if isinstance(const, (list, dict)):
                    consts.setdefault(inst, {})[key] = const

        return len(consts) > 0
Exemplo n.º 4
0
    def match(self, func_ir, block, typemap, calltypes):
        self._reset()

        self._block = block
        self._func_ir = func_ir
        self._calls_to_rewrite = set()

        for stmt in find_operations(block=block, op_name='call'):
            expr = stmt.value
            fdef = guard(find_callname, func_ir, expr)
            if fdef == self._pandas_dataframe:
                args = get_call_parameters(call=expr, arg_names=self._df_arg_list)

                if self._match_dict_case(args, func_ir):
                    self._calls_to_rewrite.add(stmt)
                else:
                    pass  # Forward this case to pd_dataframe_overload which will handle it

        return len(self._calls_to_rewrite) > 0