def comparator_helper(op_str, left_var, right_var, negated=False): if (isinstance(left_var, VarTypeTemplate)): check_legal_cmp(left_var, op_str, right_var) op_str = types.unliteral(op_str) # print("AAAA", op_str) if (not isinstance(right_var, VarTypeTemplate)): # print("POOP") right_var_type = types.unliteral(right_var) # ctor = gen_pterm_ctor_alpha(left_var, op_str, right_var_type) # print("POOP") def impl(left_var, right_var): pt = PTerm(left_var, op_str, right_var) lbv = cast_structref(GenericVarType, left_var) return pt_to_cond(pt, lbv, None, negated) else: # ctor = gen_pterm_ctor_beta(left_var, op_str, right_var) def impl(left_var, right_var): pt = PTerm(left_var, op_str, right_var) lbv = cast_structref(GenericVarType, left_var) rbv = cast_structref(GenericVarType, right_var) return pt_to_cond(pt, lbv, rbv, negated) # return var_cmp_beta return impl
def _create_dataset_typer(args, kws): kwargs = dict(kws) name = args[0] if len(args) > 0 else types.unliteral(kwargs['name']) shape = args[1] if len(args) > 1 else types.unliteral(kwargs['shape']) dtype = args[2] if len(args) > 2 else types.unliteral(kwargs['dtype']) def create_dset_stub(name, shape, dtype): pass pysig = numba.utils.pysignature(create_dset_stub) return signature(h5dataset_type, name, shape, dtype).replace(pysig=pysig)
def _cmp_helper(self,op_str,other,negate): check_legal_cmp(self, op_str, other) opt_str = types.literal(types.unliteral(op_str)) if(not isinstance(other,(VarTypeTemplate,Var))): return var_cmp_alpha(self,op_str,other, negate) else: return var_cmp_beta(self,op_str,other, negate)
def var_cmp_alpha(left_var, op_str, right_var,negated): from cre.condition_node import pt_to_cond, gen_pterm_ctor_alpha, gen_pterm_ctor_beta right_var_type = types.unliteral(types.literal(right_var)) ctor = gen_pterm_ctor_alpha(left_var._numba_type_, op_str, right_var_type) pt = ctor(left_var, op_str, right_var) lbv = cast_structref(GenericVarType,left_var) return pt_to_cond(pt, lbv, None, negated)
def unify_pairs(self, first, second): """ Try to unify the two given types. A third type is returned, or None in case of failure. """ if first == second: return first if first is types.undefined: return second elif second is types.undefined: return first # Types with special unification rules unified = first.unify(self, second) if unified is not None: return unified unified = second.unify(self, first) if unified is not None: return unified # Other types with simple conversion rules conv = self.can_convert(fromty=first, toty=second) if conv is not None and conv <= Conversion.safe: # Can convert from first to second return second conv = self.can_convert(fromty=second, toty=first) if conv is not None and conv <= Conversion.safe: # Can convert from second to first return first if isinstance(first, types.Literal) or isinstance( second, types.Literal): first = types.unliteral(first) second = types.unliteral(second) return self.unify_pairs(first, second) # Cannot unify return None
def as_dtype(nbtype): """ Return a Pandas *dtype* instance corresponding to the given Numba type. NotImplementedError is raised if no correspondence is known. """ nbtype = types.unliteral(nbtype) if isinstance(nbtype, CategoricalDtypeType): return pd.CategoricalDtype(categories=nbtype.categories, ordered=nbtype.ordered) raise NotImplementedError("%r cannot be represented as a Pandas dtype" % (nbtype, ))
def unify_pairs(self, first, second): """ Try to unify the two given types. A third type is returned, or None in case of failure. """ if first == second: return first if first is types.undefined: return second elif second is types.undefined: return first # Types with special unification rules unified = first.unify(self, second) if unified is not None: return unified unified = second.unify(self, first) if unified is not None: return unified # Other types with simple conversion rules conv = self.can_convert(fromty=first, toty=second) if conv is not None and conv <= Conversion.safe: # Can convert from first to second return second conv = self.can_convert(fromty=second, toty=first) if conv is not None and conv <= Conversion.safe: # Can convert from second to first return first if isinstance(first, types.Literal) or isinstance(second, types.Literal): first = types.unliteral(first) second = types.unliteral(second) return self.unify_pairs(first, second) # Cannot unify return None
def _resolve_builtin_function_type(self, func, args, kws): # NOTE: we should reduce usage of this if func in self._functions: # Note: Duplicating code with types.Function.get_call_type(). # *defns* are CallTemplates. defns = self._functions[func] for defn in defns: for support_literals in [True, False]: if support_literals: res = defn.apply(args, kws) else: fixedargs = [types.unliteral(a) for a in args] res = defn.apply(fixedargs, kws) if res is not None: return res
def generic(self, args, kws): assert not kws assert len(args) == 1 column = types.unliteral(args[0]) ret_typ = column if (isinstance(column, types.List) and (isinstance(column.dtype, types.Number) or column.dtype == types.boolean)): ret_typ = types.Array(column.dtype, 1, 'C') if (isinstance(column, types.List) and (column.dtype == string_type or isinstance(column.dtype, types.Optional) and column.dtype.type == string_type)): ret_typ = string_array_type if isinstance(column, SeriesType): ret_typ = column.data # TODO: add other types return signature(ret_typ, column)
def resolve_getattr(self, typ, attr): """ Resolve getting the attribute *attr* (a string) on the Numba type. The attribute's type is returned, or None if resolution failed. """ def core(typ): out = self.find_matching_getattr_template(typ, attr) if out: return out['return_type'] out = core(typ) if out is not None: return out # Try again without literals out = core(types.unliteral(typ)) if out is not None: return out if isinstance(typ, types.Module): attrty = self.resolve_module_constants(typ, attr) if attrty is not None: return attrty
def resolve_getattr(self, typ, attr): """ Resolve getting the attribute *attr* (a string) on the Numba type. The attribute's type is returned, or None if resolution failed. """ def core(typ): for attrinfo in self._get_attribute_templates(typ): ret = attrinfo.resolve(typ, attr) if ret is not None: return ret out = core(typ) if out is not None: return out # Try again without literals out = core(types.unliteral(typ)) if out is not None: return out if isinstance(typ, types.Module): attrty = self.resolve_module_constants(typ, attr) if attrty is not None: return attrty
def generic(self, args, kws): assert not kws assert len(args) == 2 return signature(types.intp, args[0], types.unliteral(args[1]))
def unliteral_all(args): return tuple(types.unliteral(a) for a in args)
def resolve_split(self, dict, args, kws): assert not kws assert len(args) == 1 return signature(types.List(string_type), types.unliteral(args[0]))
def preprocess_fields(self, fields): return tuple((name, types.unliteral(typ)) for name, typ in fields)