python - Passing Function names as parameter to define a generic function -
i have following python codes (some functions defined in same class), can see. 2 functions same, except calling different functions: 1 calling self._init_module
; , calling self._config_module
. define common generic function can pass function name parameter. how should implement this?
# ulong laurakit_api laurakitinitmodule_1b(laura_handle modhandle, ulong *regaddr, ulong *regdata, uchar *size) self._init_module = _lib.laurakitinitmodule_1b # ulong laurakit_api laurakitconfigregister_1b # (laura_handle modhandle, operation *ope, ulong *regaddr, ulong *regdata, int *size) self._config_module = _lib.laurakitconfigregister_1b def init_module(self, mod_h): """ :param mod_h: module handler :return: operation, reg_addr, reg_data, size.value """ mod_handle = ct.c_ulong(mod_h) reg_addr_arr = ct.c_ulong * max_arr_len reg_data_arr = ct.c_ulong * max_arr_len reg_oper_seq = ct.c_ulong * max_arr_len operation = reg_oper_seq() reg_addr = reg_addr_arr() reg_data = reg_data_arr() size = ct.c_int(0) self._ok = self._init_module(mod_handle, operation, reg_addr, reg_data, ct.byref(size)) return operation, reg_addr, reg_data, size.value def config_module(self, mod_h): """ same function implementation init_module :param mod_h: module handler :return: operation, reg_addr, reg_data, size.value """ mod_handle = ct.c_ulong(mod_h) reg_addr_arr = ct.c_ulong * max_arr_len reg_data_arr = ct.c_ulong * max_arr_len reg_oper_seq = ct.c_ulong * max_arr_len operation = reg_oper_seq() reg_addr = reg_addr_arr() reg_data = reg_data_arr() size = ct.c_int(0) self._ok = self._config_module(mod_handle, operation, reg_addr, reg_data, ct.byref(size)) return operation, reg_addr, reg_data, size.value
you pass function directly, like:
def process_module(self, mod_h, process_func): # pre-processing... self._ok = process_func(mod_handle, operation, reg_addr, reg_data, ct.byref(size)) return operation, reg_addr, reg_data, size.value def init_module(self, mod_h): self.process_module(mod_h, self._init_module) def config_module(self, mod_h): self.process_module(mod_h, self._config_module)
Comments
Post a Comment