From ca0d995f8c48e4e6b5e591f38013961eafdb479d Mon Sep 17 00:00:00 2001 From: Xinyu Li Date: Thu, 24 Oct 2024 15:00:13 -0700 Subject: [PATCH] add python --- python/ionpy/Port.py | 131 +++++++++++++++++++++++++++++++---------- python/ionpy/native.py | 58 +++++++++++++++++- 2 files changed, 157 insertions(+), 32 deletions(-) diff --git a/python/ionpy/Port.py b/python/ionpy/Port.py index 0e5d5082..f9fccb50 100644 --- a/python/ionpy/Port.py +++ b/python/ionpy/Port.py @@ -23,7 +23,21 @@ ion_port_bind_f32, ion_port_bind_f64, ion_port_bind_buffer, - ion_port_bind_buffer_array + ion_port_bind_buffer_array, + + ion_port_bind_i8_array, + ion_port_bind_i16_array, + ion_port_bind_i32_array, + ion_port_bind_i64_array, + + ion_port_bind_u1_array, + ion_port_bind_u8_array, + ion_port_bind_u16_array, + ion_port_bind_u32_array, + ion_port_bind_u64_array, + + ion_port_bind_f32_array, + ion_port_bind_f64_array, ) from .Type import Type @@ -62,38 +76,93 @@ def __del__(self): if self.obj: # check not nullptr ion_port_destroy(self.obj) - def bind(self, v: Union[int, float, Buffer, List[Buffer]]): + def bind(self, v: Union[int, float, Buffer, List[Union[Buffer, int, float]]]): if self.dim == 0: - if self.bind_value is None: - self.bind_value = np.ctypeslib.as_ctypes_type(self.type.to_dtype())(v) - else: - self.bind_value.value = v # scalar - if self.type.code_ == TypeCode.Int: - if self.type.bits_ == 8 and ion_port_bind_i8(self.obj, ctypes.byref(self.bind_value)) != 0: - raise Exception('Invalid operation') - elif self.type.bits_ == 16 and ion_port_bind_i16(self.obj, ctypes.byref(self.bind_value)) != 0: - raise Exception('Invalid operation') - elif self.type.bits_ == 32 and ion_port_bind_i32(self.obj, ctypes.byref(self.bind_value)) != 0: - raise Exception('Invalid operation') - elif self.type.bits_ == 64 and ion_port_bind_i64(self.obj, ctypes.byref(self.bind_value)) != 0: - raise Exception('Invalid operation') - elif self.type.code_ == TypeCode.Uint: - if self.type.bits_ == 1 and ion_port_bind_u1(self.obj, ctypes.byref(self.bind_value)) != 0: - raise Exception('Invalid operation') - if self.type.bits_ == 8 and ion_port_bind_u8(self.obj, ctypes.byref(self.bind_value)) != 0: - raise Exception('Invalid operation') - if self.type.bits_ == 16 and ion_port_bind_u16(self.obj, ctypes.byref(self.bind_value)) != 0: - raise Exception('Invalid operation') - if self.type.bits_ == 32 and ion_port_bind_u32(self.obj, ctypes.byref(self.bind_value)) != 0: - raise Exception('Invalid operation') - if self.type.bits_ == 64 and ion_port_bind_u64(self.obj, ctypes.byref(self.bind_value)) != 0: - raise Exception('Invalid operation') - elif self.type.code_ == TypeCode.Float: - if self.type.bits_ == 32 and ion_port_bind_f32(self.obj, ctypes.byref(self.bind_value)) != 0: - raise Exception('Invalid operation') - if self.type.bits_ == 64 and ion_port_bind_f64(self.obj, ctypes.byref(self.bind_value)) != 0: - raise Exception('Invalid operation') + if type(v) is not list: + if self.bind_value is None: + self.bind_value = np.ctypeslib.as_ctypes_type(self.type.to_dtype())(v) + else: + self.bind_value.value = v + + if self.type.code_ == TypeCode.Int: + if self.type.bits_ == 8 and ion_port_bind_i8_array(self.obj, ctypes.byref(self.bind_value)) != 0: + raise Exception('Invalid operation') + elif self.type.bits_ == 16 and ion_port_bind_i16(self.obj, ctypes.byref(self.bind_value)) != 0: + raise Exception('Invalid operation') + elif self.type.bits_ == 32 and ion_port_bind_i32(self.obj, ctypes.byref(self.bind_value)) != 0: + raise Exception('Invalid operation') + elif self.type.bits_ == 64 and ion_port_bind_i64(self.obj, ctypes.byref(self.bind_value)) != 0: + raise Exception('Invalid operation') + elif self.type.code_ == TypeCode.Uint: + if self.type.bits_ == 1 and ion_port_bind_u1(self.obj, ctypes.byref(self.bind_value)) != 0: + raise Exception('Invalid operation') + if self.type.bits_ == 8 and ion_port_bind_u8(self.obj, ctypes.byref(self.bind_value)) != 0: + raise Exception('Invalid operation') + if self.type.bits_ == 16 and ion_port_bind_u16(self.obj, ctypes.byref(self.bind_value)) != 0: + raise Exception('Invalid operation') + if self.type.bits_ == 32 and ion_port_bind_u32(self.obj, ctypes.byref(self.bind_value)) != 0: + raise Exception('Invalid operation') + if self.type.bits_ == 64 and ion_port_bind_u64(self.obj, ctypes.byref(self.bind_value)) != 0: + raise Exception('Invalid operation') + elif self.type.code_ == TypeCode.Float: + if self.type.bits_ == 32 and ion_port_bind_f32(self.obj, ctypes.byref(self.bind_value)) != 0: + raise Exception('Invalid operation') + if self.type.bits_ == 64 and ion_port_bind_f64(self.obj, ctypes.byref(self.bind_value)) != 0: + raise Exception('Invalid operation') + else: + # scalar array + c_arr = None + if self.type.code_ == TypeCode.Int: + if self.type.bits_ == 8: + c_arr = (ctypes.c_int8 * len(v))(*v) + if ion_port_bind_i8_array(self.obj, c_arr, len(v)) != 0: + raise Exception('Invalid operation') + elif self.type.bits_ == 16: + c_arr = (ctypes.c_int16 * len(v))(*v) + if ion_port_bind_i16_array(self.obj, c_arr, len(v)) != 0: + raise Exception('Invalid operation') + elif self.type.bits_ == 32 : + c_arr = (ctypes.c_int32 * len(v))(*v) + if ion_port_bind_i32_array(self.obj, c_arr, len(v)) != 0: + raise Exception('Invalid operation') + elif self.type.bits_ == 64: + c_arr = (ctypes.c_int64 * len(v))(*v) + if ion_port_bind_i64_array(self.obj, c_arr, len(v)) != 0: + raise Exception('Invalid operation') + elif self.type.code_ == TypeCode.Uint: + if self.type.bits_ == 1: + c_arr = (ctypes.c_bool * len(v))(*v) + if ion_port_bind_u1_array(self.obj, c_arr, len(v)) != 0: + raise Exception('Invalid operation') + if self.type.bits_ == 8 : + c_arr = (ctypes.c_uint8 * len(v))(*v) + if ion_port_bind_u8_array(self.obj, c_arr, len(v)) != 0: + raise Exception('Invalid operation') + if self.type.bits_ == 16: + c_arr = (ctypes.c_uint16 * len(v))(*v) + if ion_port_bind_u16_array(self.obj, c_arr, len(v)) != 0: + raise Exception('Invalid operation') + if self.type.bits_ == 32: + c_arr = (ctypes.c_uint32* len(v))(*v) + if ion_port_bind_u32_array(self.obj, c_arr, len(v)) != 0: + raise Exception('Invalid operation') + if self.type.bits_ == 64: + c_arr = (ctypes.c_uint64 * len(v))(*v) + if ion_port_bind_u64_array(self.obj, c_arr, len(v)) != 0: + raise Exception('Invalid operation') + elif self.type.code_ == TypeCode.Float: + if self.type.bits_ == 32: + c_arr = (ctypes.c_float * len(v))(*v) + if ion_port_bind_f32(self.obj, c_arr, len(v)) != 0: + raise Exception('Invalid operation') + if self.type.bits_ == 64: + c_arr = (ctypes.c_double * len(v))(*v) + if ion_port_bind_f64(self.obj, c_arr, len(v)) != 0: + raise Exception('Invalid operation') + + self.bind_value = c_arr + # vector else: diff --git a/python/ionpy/native.py b/python/ionpy/native.py index 944a9192..d75f8f41 100644 --- a/python/ionpy/native.py +++ b/python/ionpy/native.py @@ -105,11 +105,67 @@ class c_builder_compile_option_t(ctypes.Structure): ion_port_bind_f32.restype = ctypes.c_int ion_port_bind_f32.argtypes = [ c_ion_port_t, ctypes.POINTER(ctypes.c_float) ] -# int ion_port_bind_f64(ion_port_t, double*; +# int ion_port_bind_f64(ion_port_t, double*;); ion_port_bind_f64 = ion_core.ion_port_bind_f64 ion_port_bind_f64.restype = ctypes.c_int ion_port_bind_f64.argtypes = [ c_ion_port_t, ctypes.POINTER(ctypes.c_double) ] +# int ion_port_bind_i8_array(ion_port_t, int8_t*, int); +ion_port_bind_i8_array = ion_core.ion_port_bind_i8_array +ion_port_bind_i8_array.restype = ctypes.c_int +ion_port_bind_i8_array.argtypes = [c_ion_port_t, ctypes.POINTER(ctypes.c_int8), ctypes.c_int ] + +# int ion_port_bind_i16_array(ion_port_t, int16_t*, int); +ion_port_bind_i16_array = ion_core.ion_port_bind_i16_array +ion_port_bind_i16_array.restype = ctypes.c_int +ion_port_bind_i16_array.argtypes = [c_ion_port_t, ctypes.POINTER(ctypes.c_int16), ctypes.c_int ] + +# int ion_port_bind_i32_array(ion_port_t, int32_t*, int); +ion_port_bind_i32_array = ion_core.ion_port_bind_i32_array +ion_port_bind_i32_array.restype = ctypes.c_int +ion_port_bind_i32_array.argtypes = [c_ion_port_t, ctypes.POINTER(ctypes.c_int32), ctypes.c_int ] + +# int ion_port_bind_i64_array(ion_port_t, int64_t*, int); +ion_port_bind_i64_array = ion_core.ion_port_bind_i64_array +ion_port_bind_i64_array.restype = ctypes.c_int +ion_port_bind_i64_array.argtypes = [ c_ion_port_t, ctypes.POINTER(ctypes.c_int64), ctypes.c_int ] + +# int ion_port_map_set_u1_array(ion_port_t, bool*, int); +ion_port_bind_u1_array = ion_core.ion_port_bind_u1_array +ion_port_bind_u1_array.restype = ctypes.c_int +ion_port_bind_u1_array.argtypes = [ c_ion_port_t, ctypes.POINTER(ctypes.c_bool), ctypes.c_int ] + +# int ion_port_bind_u8_array(ion_port_t, uint8_t*, int); +ion_port_bind_u8_array = ion_core.ion_port_bind_u8_array +ion_port_bind_u8_array.restype = ctypes.c_int +ion_port_bind_u8_array.argtypes = [ c_ion_port_t, ctypes.POINTER(ctypes.c_uint8), ctypes.c_int ] + +# int ion_port_bind_u16_array(ion_port_t, uint16_t*, int); +ion_port_bind_u16_array = ion_core.ion_port_bind_u16_array +ion_port_bind_u16_array.restype = ctypes.c_int +ion_port_bind_u16_array.argtypes = [ c_ion_port_t, ctypes.POINTER(ctypes.c_uint16), ctypes.c_int ] + +# int ion_port_bind_u32_array(ion_port_t, uint32_t*, int); +ion_port_bind_u32_array = ion_core.ion_port_bind_u32_array +ion_port_bind_u32_array.restype = ctypes.c_int +ion_port_bind_u32_array.argtypes = [ c_ion_port_t, ctypes.POINTER(ctypes.c_uint32), ctypes.c_int ] + +# int ion_port_bind_u64_array(ion_port_t, uint64_t*, int); +ion_port_bind_u64_array = ion_core.ion_port_bind_u64_array +ion_port_bind_u64_array.restype = ctypes.c_int +ion_port_bind_u64_array.argtypes = [ c_ion_port_t, ctypes.POINTER(ctypes.c_uint64), ctypes.c_int ] + +# int ion_port_bind_f32_array(ion_port_t, float*, int); +ion_port_bind_f32_array = ion_core.ion_port_bind_f32_array +ion_port_bind_f32_array.restype = ctypes.c_int +ion_port_bind_f32_array.argtypes = [ c_ion_port_t, ctypes.POINTER(ctypes.c_float), ctypes.c_int ] + +# int ion_port_bind_f64_array(ion_port_t, double*, int); +ion_port_bind_f64_array = ion_core.ion_port_bind_f64_array +ion_port_bind_f64_array.restype = ctypes.c_int +ion_port_bind_f64_array.argtypes = [ c_ion_port_t, ctypes.POINTER(ctypes.c_double), ctypes.c_int ] + + # int ion_port_bind_buffer(ion_port_t, ion_buffer_t); ion_port_bind_buffer = ion_core.ion_port_bind_buffer ion_port_bind_buffer.restype = ctypes.c_int