def preset_arguments(self, arg_types, *args, **kwargs): ''' arg_types : i,I: np.int32 d,D: np.float64 o,O: numpy array Lowercase letter means that it can be set before function call. Uppercase letter means that it should set when function call. # kwargs gsize : global thread size for CUDA and OpenCL ''' # classify the argument types self.preset_atypes = preset_atypes = list() self.require_atypes = list() for atype in arg_types: if atype not in ('i','d','o','I','D','O'): logger.error("The arg_type '%s' is not defined.\n{}".format(atype,get_stack())) raise SystemExit if atype.islower(): self.preset_atypes.append( atype ) else: self.require_atypes.append( atype.lower() ) # set the preset_args if not len(preset_atypes) == len(args): logger.error("len(preset_atypes)={} is not same as len(args)={}\n{}".format(len(preset_atypes), len(args), get_stack())) self.preset_args = list() for seq, (atype, arg) in enumerate( zip(preset_atypes, args) ): if atype=='o': assert arg.__class__.__name__ in ['Array','ArrayAs'], "The {}-th arguemnt is not a Array or ArrayAs instance.".format(seq) self.preset_args.append( self.cast_dict[atype](arg) )
def get_casted_arguments(self, *args): if len(self.require_atypes) != len(args): logger.error("len(require_atypes)={} is not same as len(args)={}\n{}".format(len(self.require_atypes), len(args), get_stack())) raise SystemExit casted_args = self.preset_args[:] # copy for seq, (atype, arg) in enumerate( zip(self.require_atypes, args) ): if atype == 'O' and \ arg.__class__.__name__ not in ['Array','ArrayAs']: logger.error("The {}-th arguemnt is not a Array or ArrayAs instance.\n{}".format(seq+1, get_stack()) ) raise SystemExit casted_args.append( self.cast_dict[atype](arg) ) return casted_args