def mergue(type1, type2): """ Merque two union types into one if possible :param type1: :param type2: :return: """ from stypy.invokation.handlers import call_utilities if call_utilities.is_numpy_array( type1) and call_utilities.is_numpy_array(type2): return type1 import types as python_types try: dir_t1 = dir(type1) dir_t2 = dir(type2) if len(dir_t2) != len(dir_t1): return None for member in dir_t2: value_t2 = getattr(type2, member) if member in dir_t1: if inspect.ismethod(value_t2) or inspect.isfunction( value_t2): continue if getattr(type1, member) is not value_t2: setattr( type1, member, UnionType.add(getattr(type1, member), getattr(type2, member))) # else: # setattr(type1, member, UnionType.add(python_types.NoneType, getattr(type2, member))) return type1 except: return None
def pow(localization, proxy_obj, arguments): if call_utilities.is_numpy_array(arguments[1]): return arguments[1] if call_utilities.is_numpy_array(arguments[0]): return arguments[0] return None # Type rule results
def __xor__(localization, proxy_obj, arguments): if call_utilities.is_numpy_array( arguments[0]) or call_utilities.is_numpy_array(arguments[1]): return numpy__type_modifiers.TypeModifiers.bitwise_xor( localization, proxy_obj, arguments) return None # Type rule results
def ne(localization, proxy_obj, arguments): if call_utilities.is_numpy_array( arguments[0]) and call_utilities.is_numpy_array(arguments[1]): return call_utilities.create_numpy_array_n_dimensions( bool(), call_utilities.get_dimensions(localization, arguments[0])) return None # Type rule results
def i0(localization, proxy_obj, arguments): if call_utilities.is_numpy_array(arguments[0]): return arguments[0] else: if Number == type(arguments[0]): return call_utilities.create_numpy_array(arguments[0]) else: return call_utilities.create_numpy_array( get_contained_elements_type(arguments[0]))
def can_be_mergued(type1, type2): """ Checks if two union types can be mergued into one :param type1: :param type2: :return: """ import types as python_types from stypy.invokation.handlers import call_utilities if type(type1) is python_types.InstanceType and type( type2) is python_types.InstanceType: return type1.__class__ == type2.__class__ if call_utilities.is_numpy_array( type1) and call_utilities.is_numpy_array(type2): return type(type1.contained_types) == type(type2.contained_types) return False
def get_type_of_for_loop_variable(localization, condition_type): """ A loop must iterate an iterable object or data structure or an string. This function returns the contents of whatever the loop is iterating :param localization: Caller information :param condition_type: Type of the condition :return: """ if type(condition_type) is RecursionType: return condition_type if type(condition_type) is StypyTypeError: return condition_type if type(condition_type) is types.FileType: return str() if condition_type is undefined_type.UndefinedType: return condition_type if can_store_keypairs(condition_type): return wrap_contained_type(get_key_types(condition_type)) # If the type of the condition can store elements, return the type of stored elements if can_store_elements(condition_type): Localization.set_current(localization) return wrap_contained_type(get_contained_elements_type(condition_type)) # If the type of the condition is some kind of string, return the type of string if can_represent_type(Str, condition_type): return wrap_contained_type(condition_type) # If the type of the condition is something iterable, return the result of calling its __iter__ method if can_represent_type(IterableObject, condition_type): iter_method = condition_type.get_type_of_member(localization, "__iter__") return wrap_contained_type(stypy_interface.invoke(localization, iter_method)) if call_utilities.is_numpy_array(condition_type): return condition_type.get_contained_type() if call_utilities.is_ndenumerate(condition_type): contained = None for typ in condition_type.iter.coords: contained = union_type.UnionType.add(contained, typ) t = wrap_contained_type((contained,)) t.set_contained_type(contained) return union_type.UnionType.add(t, numpy.int32) if call_utilities.is_ndindex(condition_type): t = wrap_contained_type((numpy.int32(),)) t.set_contained_type(numpy.int32()) return t return StypyTypeError(localization, "Invalid iterable type for a loop target ({0})".format(str(condition_type)))
def __is_operator(element, list_): """ Executes the is operator with all the types in the union, passed as a list :param element: :param list_: :return: """ for elem in list_: if element is types.NoneType: if elem is types.NoneType: return True if type(element) is invokation.type_rules.type_groups.type_groups.DynamicType and type(elem) is \ invokation.type_rules.type_groups.type_groups.DynamicType: return True if element is elem: return True if UnionType.__is_same_base_type(element, elem): return True if isinstance(element, TypeWrapper) and isinstance( elem, TypeWrapper): if element == elem: return True else: # Tuples with the same types are considered equal if isinstance(elem.wrapped_type, tuple) and isinstance( element.wrapped_type, tuple): if UnionType.compare_tuple_contents(elem, element): return True from stypy.invokation.handlers import call_utilities if call_utilities.is_numpy_array( element) and call_utilities.is_numpy_array(elem): if type(element.contained_types) == type(elem.contained_types): return True # if type(element).__name__ == 'ndarray' and type(elem).__name__ == 'ndarray': # return element.tolist() == elem.tolist() return False
def argpartition(localization, proxy_obj, arguments): dvar = call_utilities.parse_varargs_and_kwargs( localization, arguments, ['kth', 'axis', 'kind', 'order'], { 'kth': [Integer, IterableDataStructureWithTypedElements(Integer)], 'axis': Integer, 'kind': Str, 'order': [Str, IterableDataStructureWithTypedElements(Str)] }, 'argpartition') if isinstance(dvar, StypyTypeError): return dvar if Number == type(arguments[0]): return call_utilities.create_numpy_array(arguments[0]) if call_utilities.is_numpy_array(arguments[0]): return arguments[0] return call_utilities.create_numpy_array(arguments[0])
def argsort(localization, proxy_obj, arguments, fname='argsort'): dvar = call_utilities.parse_varargs_and_kwargs( localization, arguments, ['axis', 'kind', 'order'], { 'axis': Integer, 'kind': Str, 'order': [Str, IterableDataStructureWithTypedElements(Str)] }, fname) if isinstance(dvar, StypyTypeError): return dvar t = call_utilities.check_possible_values( dvar, 'kind', ['quicksort', 'mergesort', 'heapsort']) if isinstance(t, StypyTypeError): return t if Number == type(arguments[0]): return call_utilities.create_numpy_array(arguments[0]) if call_utilities.is_numpy_array(arguments[0]): return arguments[0] return call_utilities.create_numpy_array(arguments[0])
def fix(localization, proxy_obj, arguments): if call_utilities.is_numpy_array(arguments[0]): return arguments[0] else: return call_utilities.create_numpy_array(arguments[0])
def real_if_close(localization, proxy_obj, arguments): if call_utilities.is_numpy_array(arguments[0]): return call_utilities.cast_to_numpy_type(arguments[0]) return call_utilities.create_numpy_array(arguments[0], False)
def bitwise_and(localization, proxy_obj, arguments, func_name='bitwise_and'): dvar = call_utilities.parse_varargs_and_kwargs( localization, arguments, ['where'], { 'where': [Str, IterableDataStructureWithTypedElements(bool)], }, func_name, 2) if isinstance(dvar, StypyTypeError): return dvar if call_utilities.is_iterable( arguments[0]) and call_utilities.is_iterable(arguments[1]): if not call_utilities.check_possible_types( call_utilities.get_inner_type(localization, arguments[0]), [bool, Integer]) or not call_utilities.check_possible_types( call_utilities.get_inner_type(localization, arguments[1]), [bool, Integer]): return StypyTypeError( localization, " ufunc '" + func_name + "' not supported for the input types, and the inputs could not be safely coerced to any supported types according to the casting rule ''safe''" ) if call_utilities.is_numpy_array(arguments[0]): return call_utilities.create_numpy_array( call_utilities.get_inner_type(localization, arguments[0])) if call_utilities.is_numpy_array(arguments[1]): return call_utilities.create_numpy_array( call_utilities.get_inner_type(localization, arguments[1])) return arguments[0] else: if call_utilities.is_iterable( arguments[0]) and not call_utilities.is_iterable( arguments[1]): if not call_utilities.check_possible_types( call_utilities.get_inner_type(localization, arguments[0]), [bool, Integer ]) or not call_utilities.check_possible_types( arguments[1], [bool, Integer]): return StypyTypeError( localization, " ufunc '" + func_name + "' not supported for the input types, and the inputs could not be safely coerced to any supported types according to the casting rule ''safe''" ) if call_utilities.is_numpy_array(arguments[0]): return call_utilities.create_numpy_array( call_utilities.get_inner_type(localization, arguments[0])) return arguments[0] else: if not call_utilities.is_iterable( arguments[0]) and call_utilities.is_iterable( arguments[1]): if not call_utilities.check_possible_types( call_utilities.get_inner_type( localization, arguments[1]), [bool, Integer ]) or not call_utilities.check_possible_types( arguments[0], [bool, Integer]): return StypyTypeError( localization, " ufunc '" + func_name + "' not supported for the input types, and the inputs could not be safely coerced to any supported types according to the casting rule ''safe''" ) if call_utilities.is_numpy_array(arguments[1]): return call_utilities.create_numpy_array( call_utilities.get_inner_type( localization, arguments[1])) return arguments[1] else: return arguments[0]
def unwrap(localization, proxy_obj, arguments): if call_utilities.is_numpy_array(arguments[0]): return call_utilities.create_numpy_array(arguments[0], False) else: return call_utilities.create_numpy_array( get_contained_elements_type(arguments[0]))