diff --git a/python/obitools3/dms/column/bool.pxd b/python/obitools3/dms/column/bool.pxd deleted file mode 100644 index 32689e9..0000000 --- a/python/obitools3/dms/column/bool.pxd +++ /dev/null @@ -1,53 +0,0 @@ -# #cython: language_level=3 -# -# from cpython.bool cimport bool, PyBool_FromLong -# -# from ..capi.obitypes cimport index_t, \ -# const_char_p, \ -# OBIType_t, \ -# obibool_t, \ -# OBI_BOOL, \ -# OBIBool_NA -# -# from ..capi.obiview cimport obi_get_bool_with_elt_name_and_col_p_in_view, \ -# obi_get_bool_with_elt_idx_and_col_p_in_view, \ -# obi_set_bool_with_elt_name_and_col_p_in_view, \ -# obi_set_bool_with_elt_idx_and_col_p_in_view -# -# from ..capi.obidmscolumn cimport obi_column_get_obibool_with_elt_name, \ -# obi_column_get_obibool_with_elt_idx, \ -# obi_column_set_obibool_with_elt_name, \ -# obi_column_set_obibool_with_elt_idx, \ -# OBIDMS_column_p -# -# from ..capi.obierrno cimport obi_errno -# -# from .column cimport Column, \ -# Column_line, \ -# register_column_class -# -# from ..view.view cimport View -# -# from obitools3.utils cimport str2bytes, bytes2str -# -# cdef class Column_line_bool(Column_line) : -# -# @staticmethod -# cdef bool obibool_t2bool(obibool_t value) -# -# @staticmethod -# cdef bool2obibool_t(bool value) -# -# cpdef bool get_bool_item_by_name(self,bytes element_name) -# cpdef bool get_bool_item_by_idx(self,index_t index) -# cpdef set_bool_item_by_name(self,bytes element_name,bool value) -# cpdef set_bool_item_by_idx(self,index_t index,bool value) -# -# -# # cdef obibool_t [:] _data_view -# -# cdef class Column_bool(Column): -# -# cpdef object get_line(self, index_t line_nb) -# cpdef set_line(self, index_t line_nb, object value) - diff --git a/python/obitools3/dms/column/typed_column/__init__.py b/python/obitools3/dms/column/typed_column/__init__.py new file mode 100644 index 0000000..889d5ff --- /dev/null +++ b/python/obitools3/dms/column/typed_column/__init__.py @@ -0,0 +1,3 @@ +#from .bool import Column_bool +#from .int import Column_int +# TODO why is this needed? \ No newline at end of file diff --git a/python/obitools3/dms/column/typed_column/bool.pxd b/python/obitools3/dms/column/typed_column/bool.pxd new file mode 100644 index 0000000..741c639 --- /dev/null +++ b/python/obitools3/dms/column/typed_column/bool.pxd @@ -0,0 +1,44 @@ +# #cython: language_level=3 + + +from ...capi.obitypes cimport index_t + +from ..column cimport Column, \ + Column_multi_elts + + +cdef class Column_bool(Column) : + + cpdef object get_line(self, index_t line_nb) + cpdef set_line(self, index_t line_nb, object value) + + +cdef class Column_multi_elts_bool(Column_multi_elts) : + + cpdef object get_item(self, index_t line_nb, object elt_id) + cpdef object get_line(self, index_t line_nb) + cpdef set_item(self, index_t line_nb, object elt_id, object value) + + + + + + + + +# cdef class Column_line_bool(Column_line) : +# +# @staticmethod +# cdef bool obibool_t2bool(obibool_t value) +# +# @staticmethod +# cdef bool2obibool_t(bool value) +# +# cpdef bool get_bool_item_by_name(self,bytes element_name) +# cpdef bool get_bool_item_by_idx(self,index_t index) +# cpdef set_bool_item_by_name(self,bytes element_name,bool value) +# cpdef set_bool_item_by_idx(self,index_t index,bool value) +# +# +# # cdef obibool_t [:] _data_view +# \ No newline at end of file diff --git a/python/obitools3/dms/column/bool.pyx b/python/obitools3/dms/column/typed_column/bool.pyx similarity index 54% rename from python/obitools3/dms/column/bool.pyx rename to python/obitools3/dms/column/typed_column/bool.pyx index e1231e5..53db1dc 100644 --- a/python/obitools3/dms/column/bool.pyx +++ b/python/obitools3/dms/column/typed_column/bool.pyx @@ -1,21 +1,142 @@ #cython: language_level=3 + + +from ..column cimport register_column_class + +from ...view.view cimport View + +from obitools3.utils cimport str2bytes, bytes2str, tobytes + +from ...capi.obiview cimport obi_view_add_column, \ + obi_get_bool_with_elt_name_and_col_p_in_view, \ + obi_get_bool_with_elt_idx_and_col_p_in_view, \ + obi_set_bool_with_elt_name_and_col_p_in_view, \ + obi_set_bool_with_elt_idx_and_col_p_in_view + +from ...capi.obierrno cimport obi_errno +from ...capi.obitypes cimport OBI_BOOL, OBIBool_NA, obibool_t, obitype_t + +from cpython.bool cimport PyBool_FromLong + + +cdef class Column_bool(Column): + + @staticmethod + def new(View view, + object column_name, + index_t nb_elements_per_line=1, + object elements_names=None, + object comments=b""): + + return Column.new_column(view, column_name, OBI_BOOL, + nb_elements_per_line=nb_elements_per_line, + elements_names=elements_names, + comments=comments) + + + cpdef object get_line(self, index_t line_nb): + cdef obibool_t value + cdef object result + global obi_errno + value = obi_get_bool_with_elt_idx_and_col_p_in_view(self._view.pointer(), self.pointer(), line_nb, 0) + if obi_errno > 0 : + raise IndexError(line_nb) + if value == OBIBool_NA : + result = None + else : + result = PyBool_FromLong(value) + return result + + + cpdef set_line(self, index_t line_nb, object value): + if value is None : + value = OBIBool_NA + if obi_set_bool_with_elt_idx_and_col_p_in_view(self._view.pointer(), self.pointer(), line_nb, 0, value) < 0 : + raise Exception("Problem setting a value in a column") + + +cdef class Column_multi_elts_bool(Column_multi_elts): + + cpdef object get_item(self, index_t line_nb, object elt_id) : + cdef obibool_t value + cdef object result + cdef bytes elt_name + if type(elt_id) == int : + value = obi_get_bool_with_elt_idx_and_col_p_in_view(self._view.pointer(), self.pointer(), line_nb, elt_id) + else : + elt_name = tobytes(elt_id) + value = obi_get_bool_with_elt_name_and_col_p_in_view(self._view.pointer(), self.pointer(), line_nb, elt_name) + if obi_errno > 0 : + raise IndexError(line_nb, elt_id) + if value == OBIBool_NA : + result = None + else : + result = PyBool_FromLong(value) + return result + + cpdef object get_line(self, index_t line_nb) : + cdef obibool_t value + cdef object value_in_result + cdef dict result + cdef index_t i + cdef bint all_NA + result = {} + all_NA = True + for i in range(self.nb_elements_per_line) : + value = obi_get_bool_with_elt_idx_and_col_p_in_view(self._view.pointer(), self.pointer(), line_nb, i) + if obi_errno > 0 : + raise IndexError(line_nb) + if value == OBIBool_NA : + value_in_result = None + else : + value_in_result = PyBool_FromLong(value) + result[self.elements_names[i]] = value_in_result + if all_NA and (value_in_result is not None) : + all_NA = False + if all_NA : + result = None + return result + + cpdef set_item(self, index_t line_nb, object elt_id, object value) : + cdef bytes elt_name + if value is None : + value = OBIBool_NA + if type(elt_id) == int : + if obi_set_bool_with_elt_idx_and_col_p_in_view(self._view.pointer(), self.pointer(), line_nb, elt_id, value) < 0 : + raise Exception("Problem setting a value in a column") + else : + elt_name = tobytes(elt_id) + if obi_set_bool_with_elt_name_and_col_p_in_view(self._view.pointer(), self.pointer(), line_nb, elt_name, value) < 0 : + raise Exception("Problem setting a value in a column") + + +def register_class() : + register_column_class(OBI_BOOL, False, Column_bool, bool) + register_column_class(OBI_BOOL, True, Column_multi_elts_bool, bool) + + + + + + + # cdef class Column_line_bool(Column_line) : # -# # cdef update_pointer(self): -# # """ -# # Checks if the obicolumn address changed since the last call and update -# # if need the `_column_p` and `_data_view` data structure fields. -# # """ -# # cdef OBIDMS_column_p* column_pp -# # column_pp = self._pointer -# # cdef OBIDMS_column_p column_p = column_pp[0] -# # -# # if column_p != self._column_p: -# # self._column_p = column_p -# # # self._data_view = ( (column_p.data)) + \ -# # # self._index * column_p.header.nb_elements_per_line +# cdef update_pointer(self): +# """ +# Checks if the obicolumn address changed since the last call and update +# if need the `_column_p` and `_data_view` data structure fields. +# """ +# cdef OBIDMS_column_p* column_pp +# column_pp = self._pointer +# cdef OBIDMS_column_p column_p = column_pp[0] +# +# if column_p != self._column_p: +# self._column_p = column_p +# self._data_view = ( (column_p.data)) + \ +# self._index * column_p.header.nb_elements_per_line # # @staticmethod # cdef bool obibool_t2bool(obibool_t value): @@ -197,109 +318,3 @@ # # def __len__(self): # return self._len - - - -# cdef class Column_bool(Column): -# -# @staticmethod -# def new(View view, -# object column_name, -# index_t nb_elements_per_line=1, -# object elements_names=None, -# object comments=b""): -# -# cdef bytes column_name_b = tobytes(column_name) -# cdef bytes comments_b -# cdef bytes elements_names_b -# cdef char* elements_names_p -# cdef Column new_column -# -# if comments is not None: -# comments_b = tobytes(comments) -# else: -# comments_b = b'' -# -# if elements_names is not None: -# elements_names_b = b''.join([tobytes(x) for x in elements_names]) -# elements_names_p = elements_names_b -# else: -# elements_names_p = NULL -# -# if (obi_view_add_column(view = view._pointer, -# column_name = column_name_b, -# version_number = -1, -# alias = NULL, -# data_type = OBI_BOOL, -# nb_lines = len(view), -# nb_elements_per_line = nb_elements_per_line, -# elements_names = elements_names_p, -# indexer_name = NULL, -# associated_column_name = NULL, -# associated_column_version = -1, -# comments = comments_b, -# create = True)<0): -# raise RuntimeError("Cannot create column %s in view %s" % (bytes2str(column_name), -# bytes2str(view.name))) -# -# view.__init_columns__() -# new_column = self._columns[column_name] -# -# return new_column -# -# def add_to_view(self, -# View view, -# object column_name=None, -# object comments=b""): -# -# cdef OBIDMS_column_p column_p = (self._pointer)[0] -# cdef bytes alias -# -# if (column_name is None): -# alias = self._alias -# else: -# alias = tobytes(column_name) -# -# if (obi_view_add_column(view = view._ponter, -# column_name = column_p.header.name, -# version_number = column_p.header.version, -# alias = alias, -# data_type = OBI_BOOL, -# nb_lines = column_p.header.lines_used, -# nb_elements_per_line = column_p.header.nb_elements_per_line, -# elements_names = column_p.header.elements_names, -# indexer_name = NULL, -# associated_column_name = NULL, -# associated_column_version = NULL, -# comments = tobytes(comments), -# create = False) < 0): -# raise RuntimeError("Cannot insert column %s (%s@%d) into view %s" % -# ( bytes2str(alias), -# bytes2str(column_p.header.name), -# column_p.header.version, -# bytes2str(view.name) -# )) -# -# cpdef object get_line(self, index_t line_nb): -# cdef obibool_t value -# cdef object result -# global obi_errno -# -# value = obi_get_bool_with_elt_idx_and_col_p_in_view(self._view._pointer, (self._pointer)[0], line_nb, 0) -# if obi_errno > 0 : -# raise IndexError(line_nb) -# if value == OBIBool_NA : -# result = None -# else : -# result = PyBool_FromLong(value) -# return result -# -# cpdef set_line(self, index_t line_nb, object value): -# if value is None : -# value = OBIBool_NA -# if obi_set_bool_with_elt_idx_and_col_p_in_view(self._view._pointer, (self._pointer)[0], line_nb, 0, value) < 0: -# raise Exception("Problem setting a value in a column") -# -# -# register_column_class(OBI_BOOL, Column_bool, bool) - diff --git a/python/obitools3/dms/column/typed_column/char.pxd b/python/obitools3/dms/column/typed_column/char.pxd new file mode 100644 index 0000000..1d70e00 --- /dev/null +++ b/python/obitools3/dms/column/typed_column/char.pxd @@ -0,0 +1,20 @@ +# #cython: language_level=3 + + +from ...capi.obitypes cimport index_t + +from ..column cimport Column, \ + Column_multi_elts + + +cdef class Column_char(Column) : + + cpdef object get_line(self, index_t line_nb) + cpdef set_line(self, index_t line_nb, object value) + + +cdef class Column_multi_elts_char(Column_multi_elts) : + + cpdef object get_item(self, index_t line_nb, object elt_id) + cpdef object get_line(self, index_t line_nb) + cpdef set_item(self, index_t line_nb, object elt_id, object value) diff --git a/python/obitools3/dms/column/typed_column/char.pyx b/python/obitools3/dms/column/typed_column/char.pyx new file mode 100644 index 0000000..8c08f67 --- /dev/null +++ b/python/obitools3/dms/column/typed_column/char.pyx @@ -0,0 +1,115 @@ +#cython: language_level=3 + + +from ..column cimport register_column_class + +from ...view.view cimport View + +from obitools3.utils cimport str2bytes, bytes2str, tobytes + +from ...capi.obiview cimport obi_get_char_with_elt_name_and_col_p_in_view, \ + obi_get_char_with_elt_idx_and_col_p_in_view, \ + obi_set_char_with_elt_name_and_col_p_in_view, \ + obi_set_char_with_elt_idx_and_col_p_in_view + +from ...capi.obierrno cimport obi_errno +from ...capi.obitypes cimport OBI_CHAR, OBIChar_NA, obichar_t, obitype_t + + + +cdef class Column_char(Column): + + @staticmethod + def new(View view, + object column_name, + index_t nb_elements_per_line=1, + object elements_names=None, + object comments=b""): + + return Column.new_column(view, column_name, OBI_CHAR, + nb_elements_per_line=nb_elements_per_line, + elements_names=elements_names, + comments=comments) + + + cpdef object get_line(self, index_t line_nb): + cdef obichar_t value + cdef object result + global obi_errno + + value = obi_get_char_with_elt_idx_and_col_p_in_view(self._view.pointer(), self.pointer(), line_nb, 0) + if obi_errno > 0 : + raise IndexError(line_nb) + if value == OBIChar_NA : + result = None + else : + result = value # TODO return bytes or str? + return result + + + cpdef set_line(self, index_t line_nb, object value): + if value is None : + value = OBIChar_NA + if obi_set_char_with_elt_idx_and_col_p_in_view(self._view.pointer(), self.pointer(), line_nb, 0, tobytes(value)[0]) < 0: + raise Exception("Problem setting a value in a column") + + +cdef class Column_multi_elts_char(Column_multi_elts): + + cpdef object get_item(self, index_t line_nb, object elt_id) : + cdef obichar_t value + cdef object result + cdef bytes elt_name + if type(elt_id) == int : + value = obi_get_char_with_elt_idx_and_col_p_in_view(self._view.pointer(), self.pointer(), line_nb, elt_id) + else : + elt_name = tobytes(elt_id) + value = obi_get_char_with_elt_name_and_col_p_in_view(self._view.pointer(), self.pointer(), line_nb, elt_name) + if obi_errno > 0 : + raise IndexError(line_nb, elt_id) + if value == OBIChar_NA : + result = None + else : + result = value # TODO return bytes or str? + return result + + cpdef object get_line(self, index_t line_nb) : + cdef obichar_t value + cdef object value_in_result + cdef dict result + cdef index_t i + cdef bint all_NA + result = {} + all_NA = True + for i in range(self.nb_elements_per_line) : + value = obi_get_char_with_elt_idx_and_col_p_in_view(self._view.pointer(), self.pointer(), line_nb, i) + if obi_errno > 0 : + raise IndexError(line_nb) + if value == OBIChar_NA : + value_in_result = None + else : + value_in_result = value # TODO return bytes or str? + result[self.elements_names[i]] = value_in_result + if all_NA and (value_in_result is not None) : + all_NA = False + if all_NA : + result = None + return result + + cpdef set_item(self, index_t line_nb, object elt_id, object value) : + cdef bytes elt_name + if value is None : + value = OBIChar_NA + if type(elt_id) == int : + if obi_set_char_with_elt_idx_and_col_p_in_view(self._view.pointer(), self.pointer(), line_nb, elt_id, tobytes(value)[0]) < 0 : + raise Exception("Problem setting a value in a column") + else : + elt_name = tobytes(elt_id) + if obi_set_char_with_elt_name_and_col_p_in_view(self._view.pointer(), self.pointer(), line_nb, elt_name, tobytes(value)[0]) < 0 : + raise Exception("Problem setting a value in a column") + + +def register_class(): + register_column_class(OBI_CHAR, False, Column_char, bytes) # TODO bytes or str? + register_column_class(OBI_CHAR, True, Column_multi_elts_char, bytes) # TODO bytes or str? + diff --git a/python/obitools3/dms/column/typed_column/float.pxd b/python/obitools3/dms/column/typed_column/float.pxd new file mode 100644 index 0000000..29d22ca --- /dev/null +++ b/python/obitools3/dms/column/typed_column/float.pxd @@ -0,0 +1,21 @@ +# #cython: language_level=3 + + +from ...capi.obitypes cimport index_t + +from ..column cimport Column, \ + Column_multi_elts + + +cdef class Column_float(Column) : + + cpdef object get_line(self, index_t line_nb) + cpdef set_line(self, index_t line_nb, object value) + + +cdef class Column_multi_elts_float(Column_multi_elts) : + + cpdef object get_item(self, index_t line_nb, object elt_id) + cpdef object get_line(self, index_t line_nb) + cpdef set_item(self, index_t line_nb, object elt_id, object value) + \ No newline at end of file diff --git a/python/obitools3/dms/column/typed_column/float.pyx b/python/obitools3/dms/column/typed_column/float.pyx new file mode 100644 index 0000000..0df60bf --- /dev/null +++ b/python/obitools3/dms/column/typed_column/float.pyx @@ -0,0 +1,115 @@ +#cython: language_level=3 + + +from ..column cimport register_column_class + +from ...view.view cimport View + +from obitools3.utils cimport str2bytes, bytes2str, tobytes + +from ...capi.obiview cimport obi_get_float_with_elt_name_and_col_p_in_view, \ + obi_get_float_with_elt_idx_and_col_p_in_view, \ + obi_set_float_with_elt_name_and_col_p_in_view, \ + obi_set_float_with_elt_idx_and_col_p_in_view + +from ...capi.obierrno cimport obi_errno +from ...capi.obitypes cimport OBI_FLOAT, OBIFloat_NA, obifloat_t, obitype_t + + + +cdef class Column_float(Column): + + @staticmethod + def new(View view, + object column_name, + index_t nb_elements_per_line=1, + object elements_names=None, + object comments=b""): + + return Column.new_column(view, column_name, OBI_FLOAT, + nb_elements_per_line=nb_elements_per_line, + elements_names=elements_names, + comments=comments) + + + cpdef object get_line(self, index_t line_nb): + cdef obifloat_t value + cdef object result + global obi_errno + + value = obi_get_float_with_elt_idx_and_col_p_in_view(self._view.pointer(), self.pointer(), line_nb, 0) + if obi_errno > 0 : + raise IndexError(line_nb) + if value == OBIFloat_NA : + result = None + else : + result = value + return result + + + cpdef set_line(self, index_t line_nb, object value): + if value is None : + value = OBIFloat_NA + if obi_set_float_with_elt_idx_and_col_p_in_view(self._view.pointer(), self.pointer(), line_nb, 0, value) < 0: + raise Exception("Problem setting a value in a column") + + +cdef class Column_multi_elts_float(Column_multi_elts): + + cpdef object get_item(self, index_t line_nb, object elt_id) : + cdef obifloat_t value + cdef object result + cdef bytes elt_name + if type(elt_id) == int : + value = obi_get_float_with_elt_idx_and_col_p_in_view(self._view.pointer(), self.pointer(), line_nb, elt_id) + else : + elt_name = tobytes(elt_id) + value = obi_get_float_with_elt_name_and_col_p_in_view(self._view.pointer(), self.pointer(), line_nb, elt_name) + if obi_errno > 0 : + raise IndexError(line_nb, elt_id) + if value == OBIFloat_NA : + result = None + else : + result = value + return result + + cpdef object get_line(self, index_t line_nb) : + cdef obifloat_t value + cdef object value_in_result + cdef dict result + cdef index_t i + cdef bint all_NA + result = {} + all_NA = True + for i in range(self.nb_elements_per_line) : + value = obi_get_float_with_elt_idx_and_col_p_in_view(self._view.pointer(), self.pointer(), line_nb, i) + if obi_errno > 0 : + raise IndexError(line_nb) + if value == OBIFloat_NA : + value_in_result = None + else : + value_in_result = value + result[self.elements_names[i]] = value_in_result + if all_NA and (value_in_result is not None) : + all_NA = False + if all_NA : + result = None + return result + + cpdef set_item(self, index_t line_nb, object elt_id, object value) : + cdef bytes elt_name + if value is None : + value = OBIFloat_NA + if type(elt_id) == int : + if obi_set_float_with_elt_idx_and_col_p_in_view(self._view.pointer(), self.pointer(), line_nb, elt_id, value) < 0 : + raise Exception("Problem setting a value in a column") + else : + elt_name = tobytes(elt_id) + if obi_set_float_with_elt_name_and_col_p_in_view(self._view.pointer(), self.pointer(), line_nb, elt_name, value) < 0 : + raise Exception("Problem setting a value in a column") + + +def register_class(): + register_column_class(OBI_FLOAT, False, Column_float, float) # TODO why not double? + register_column_class(OBI_FLOAT, True, Column_multi_elts_float, float) # TODO why not double? + diff --git a/python/obitools3/dms/column/typed_column/int.pxd b/python/obitools3/dms/column/typed_column/int.pxd new file mode 100644 index 0000000..23a57ff --- /dev/null +++ b/python/obitools3/dms/column/typed_column/int.pxd @@ -0,0 +1,21 @@ +# #cython: language_level=3 + + +from ...capi.obitypes cimport index_t + +from ..column cimport Column, \ + Column_multi_elts + + +cdef class Column_int(Column) : + + cpdef object get_line(self, index_t line_nb) + cpdef set_line(self, index_t line_nb, object value) + + +cdef class Column_multi_elts_int(Column_multi_elts) : + + cpdef object get_item(self, index_t line_nb, object elt_id) + cpdef object get_line(self, index_t line_nb) + cpdef set_item(self, index_t line_nb, object elt_id, object value) + \ No newline at end of file diff --git a/python/obitools3/dms/column/typed_column/int.pyx b/python/obitools3/dms/column/typed_column/int.pyx new file mode 100644 index 0000000..905a8f3 --- /dev/null +++ b/python/obitools3/dms/column/typed_column/int.pyx @@ -0,0 +1,116 @@ +#cython: language_level=3 + + +from ..column cimport register_column_class + +from ...view.view cimport View + +from obitools3.utils cimport str2bytes, bytes2str, tobytes + +from ...capi.obiview cimport obi_get_int_with_elt_name_and_col_p_in_view, \ + obi_get_int_with_elt_idx_and_col_p_in_view, \ + obi_set_int_with_elt_name_and_col_p_in_view, \ + obi_set_int_with_elt_idx_and_col_p_in_view + +from ...capi.obierrno cimport obi_errno +from ...capi.obitypes cimport OBI_INT, OBIInt_NA, obiint_t, obitype_t + +from cpython.int cimport PyInt_FromLong + + +cdef class Column_int(Column): + + @staticmethod + def new(View view, + object column_name, + index_t nb_elements_per_line=1, + object elements_names=None, + object comments=b""): + + return Column.new_column(view, column_name, OBI_INT, + nb_elements_per_line=nb_elements_per_line, + elements_names=elements_names, + comments=comments) + + + cpdef object get_line(self, index_t line_nb): + cdef obiint_t value + cdef object result + global obi_errno + + value = obi_get_int_with_elt_idx_and_col_p_in_view(self._view.pointer(), self.pointer(), line_nb, 0) + if obi_errno > 0 : + raise IndexError(line_nb) + if value == OBIInt_NA : + result = None + else : + result = PyInt_FromLong(value) + return result + + + cpdef set_line(self, index_t line_nb, object value): + if value is None : + value = OBIInt_NA + if obi_set_int_with_elt_idx_and_col_p_in_view(self._view.pointer(), self.pointer(), line_nb, 0, value) < 0: + raise Exception("Problem setting a value in a column") + + +cdef class Column_multi_elts_int(Column_multi_elts): + + cpdef object get_item(self, index_t line_nb, object elt_id) : + cdef obiint_t value + cdef object result + cdef bytes elt_name + if type(elt_id) == int : + value = obi_get_int_with_elt_idx_and_col_p_in_view(self._view.pointer(), self.pointer(), line_nb, elt_id) + else : + elt_name = tobytes(elt_id) + value = obi_get_int_with_elt_name_and_col_p_in_view(self._view.pointer(), self.pointer(), line_nb, elt_name) + if obi_errno > 0 : + raise IndexError(line_nb, elt_id) + if value == OBIInt_NA : + result = None + else : + result = PyInt_FromLong(value) + return result + + cpdef object get_line(self, index_t line_nb) : + cdef obiint_t value + cdef object value_in_result + cdef dict result + cdef index_t i + cdef bint all_NA + result = {} + all_NA = True + for i in range(self.nb_elements_per_line) : + value = obi_get_int_with_elt_idx_and_col_p_in_view(self._view.pointer(), self.pointer(), line_nb, i) + if obi_errno > 0 : + raise IndexError(line_nb) + if value == OBIInt_NA : + value_in_result = None + else : + value_in_result = PyInt_FromLong(value) + result[self.elements_names[i]] = value_in_result + if all_NA and (value_in_result is not None) : + all_NA = False + if all_NA : + result = None + return result + + cpdef set_item(self, index_t line_nb, object elt_id, object value) : + cdef bytes elt_name + if value is None : + value = OBIInt_NA + if type(elt_id) == int : + if obi_set_int_with_elt_idx_and_col_p_in_view(self._view.pointer(), self.pointer(), line_nb, elt_id, value) < 0 : + raise Exception("Problem setting a value in a column") + else : + elt_name = tobytes(elt_id) + if obi_set_int_with_elt_name_and_col_p_in_view(self._view.pointer(), self.pointer(), line_nb, elt_name, value) < 0 : + raise Exception("Problem setting a value in a column") + + +def register_class(): + register_column_class(OBI_INT, False, Column_int, int) + register_column_class(OBI_INT, True, Column_multi_elts_int, int) + diff --git a/python/obitools3/dms/column/typed_column/qual.pxd b/python/obitools3/dms/column/typed_column/qual.pxd new file mode 100644 index 0000000..f9cbba4 --- /dev/null +++ b/python/obitools3/dms/column/typed_column/qual.pxd @@ -0,0 +1,26 @@ +# #cython: language_level=3 + + +from ...capi.obitypes cimport index_t + +from ..column cimport Column, \ + Column_multi_elts + + +cdef class Column_qual(Column) : + + cpdef object get_line(self, index_t line_nb) + cpdef object get_str_line(self, index_t line_nb) + cpdef set_line(self, index_t line_nb, object value) + cpdef set_str_line(self, index_t line_nb, object value) + + +cdef class Column_multi_elts_qual(Column_multi_elts) : + + cpdef object get_item(self, index_t line_nb, object elt_id) + cpdef object get_str_item(self, index_t line_nb, object elt_id) + cpdef object get_line(self, index_t line_nb) + cpdef object get_str_line(self, index_t line_nb) + cpdef set_item(self, index_t line_nb, object elt_id, object value) + cpdef set_str_item(self, index_t line_nb, object elt_id, object value) + \ No newline at end of file diff --git a/python/obitools3/dms/column/typed_column/qual.pyx b/python/obitools3/dms/column/typed_column/qual.pyx new file mode 100644 index 0000000..05246c9 --- /dev/null +++ b/python/obitools3/dms/column/typed_column/qual.pyx @@ -0,0 +1,258 @@ +#cython: language_level=3 + + +from ..column cimport register_column_class + +from ...view.view cimport View + +from obitools3.utils cimport str2bytes, bytes2str, tobytes + +from ...capi.obiview cimport obi_get_qual_char_with_elt_name_and_col_p_in_view, \ + obi_get_qual_char_with_elt_idx_and_col_p_in_view, \ + obi_set_qual_char_with_elt_name_and_col_p_in_view, \ + obi_set_qual_char_with_elt_idx_and_col_p_in_view, \ + obi_get_qual_int_with_elt_name_and_col_p_in_view, \ + obi_get_qual_int_with_elt_idx_and_col_p_in_view, \ + obi_set_qual_int_with_elt_name_and_col_p_in_view, \ + obi_set_qual_int_with_elt_idx_and_col_p_in_view + +from ...capi.obierrno cimport obi_errno +from ...capi.obitypes cimport OBI_QUAL, OBIQual_char_NA, OBIQual_int_NA, const_char_p + +from libc.stdlib cimport free +from libc.string cimport strcmp +from libc.stdint cimport uint8_t +from libc.stdlib cimport malloc + + +cdef class Column_qual(Column): + + @staticmethod + def new(View view, + object column_name, + index_t nb_elements_per_line=1, + object elements_names=None, + object comments=b""): + + return Column.new_column(view, column_name, OBI_QUAL, + nb_elements_per_line=nb_elements_per_line, + elements_names=elements_names, + comments=comments) + + + cpdef object get_line(self, index_t line_nb): + cdef const uint8_t* value + cdef int value_length + cdef object result + global obi_errno + + value = obi_get_qual_int_with_elt_idx_and_col_p_in_view(self._view.pointer(), self.pointer(), line_nb, 0, &value_length) + if obi_errno > 0 : + raise IndexError(line_nb) + if value == OBIQual_int_NA : + result = None + else : + result = [] + for i in range(value_length) : + result.append(value[i]) + return result + + + cpdef object get_str_line(self, index_t line_nb): + cdef char* value + cdef object result + cdef int i + global obi_errno + + value = obi_get_qual_char_with_elt_idx_and_col_p_in_view(self._view.pointer(), self.pointer(), line_nb, 0) + if obi_errno > 0 : + raise IndexError(line_nb) + if value == OBIQual_char_NA : + result = None + else : # TODO discuss + result = bytes2str(value) + free(value) + return result + + + cpdef set_line(self, index_t line_nb, object value): + cdef uint8_t* value_b + cdef int value_length + if value is None : + if obi_set_qual_int_with_elt_idx_and_col_p_in_view(self._view.pointer(), self.pointer(), line_nb, 0, OBIQual_int_NA, 0) < 0: + raise Exception("Problem setting a value in a column") + else : + value_length = len(value) + value_b = malloc(value_length * sizeof(uint8_t)) + for i in range(value_length) : + value_b[i] = value[i] + if obi_set_qual_int_with_elt_idx_and_col_p_in_view(self._view.pointer(), self.pointer(), line_nb, 0, value_b, value_length) < 0: + raise Exception("Problem setting a value in a column") + free(value_b) + + + cpdef set_str_line(self, index_t line_nb, object value): + cdef bytes value_b + if value is None : + if obi_set_qual_char_with_elt_idx_and_col_p_in_view(self._view.pointer(), self.pointer(), line_nb, 0, OBIQual_char_NA) < 0: + raise Exception("Problem setting a value in a column") + else : + if type(value) == str: + value_b = str2bytes(value) + else : + value_b = value + if obi_set_qual_char_with_elt_idx_and_col_p_in_view(self._view.pointer(), self.pointer(), line_nb, 0, value_b) < 0: + raise Exception("Problem setting a value in a column") + + +cdef class Column_multi_elts_qual(Column_multi_elts): + + cpdef object get_item(self, index_t line_nb, object elt_id): + cdef const uint8_t* value + cdef int value_length + cdef object result + cdef int i + global obi_errno # TODO add everywhere + + if type(elt_id) == int : + value = obi_get_qual_int_with_elt_idx_and_col_p_in_view(self._view.pointer(), self.pointer(), line_nb, elt_id, &value_length) + else : + elt_name = tobytes(elt_id) + value = obi_get_qual_int_with_elt_name_and_col_p_in_view(self._view.pointer(), self.pointer(), line_nb, elt_name, &value_length) + if obi_errno > 0 : + raise IndexError(line_nb, elt_id) + if value == OBIQual_int_NA : + result = None + else : + result = [] + for i in range(value_length) : + result.append(value[i]) + return result + + + cpdef object get_str_item(self, index_t line_nb, object elt_id): + cdef char* value + cdef object result + global obi_errno + + if type(elt_id) == int : + value = obi_get_qual_char_with_elt_idx_and_col_p_in_view(self._view.pointer(), self.pointer(), line_nb, elt_id) + else : + elt_name = tobytes(elt_id) + value = obi_get_qual_char_with_elt_name_and_col_p_in_view(self._view.pointer(), self.pointer(), line_nb, elt_name) + if obi_errno > 0 : + raise IndexError(line_nb, elt_name) + if value == OBIQual_char_NA : + result = None + else : + result = bytes2str(value) # TODO return bytes? + free(value) + return result + + + cpdef object get_line(self, index_t line_nb) : + cdef const uint8_t* value + cdef int value_length + cdef object value_in_result + cdef dict result + cdef index_t i + cdef int j + cdef bint all_NA + global obi_errno + + result = {} + all_NA = True + for i in range(self.nb_elements_per_line) : + value = obi_get_qual_int_with_elt_idx_and_col_p_in_view(self._view.pointer(), self.pointer(), line_nb, i, &value_length) + if obi_errno > 0 : + raise IndexError(line_nb) + if value == OBIQual_int_NA : + value_in_result = None + else : + value_in_result = [] + for j in range(value_length) : + value_in_result.append(value[j]) + result[self.elements_names[i]] = value_in_result + if all_NA and (value_in_result is not None) : + all_NA = False + if all_NA : + result = None + return result + + + cpdef object get_str_line(self, index_t line_nb) : + cdef char* value + cdef object value_in_result + cdef dict result + cdef index_t i + cdef bint all_NA + global obi_errno + + result = {} + all_NA = True + for i in range(self.nb_elements_per_line) : + value = obi_get_qual_char_with_elt_idx_and_col_p_in_view(self._view.pointer(), self.pointer(), line_nb, i) + if obi_errno > 0 : + raise IndexError(line_nb) + if value == OBIQual_char_NA : + value_in_result = None + else : + value_in_result = bytes2str(value) + free(value) + result[self.elements_names[i]] = value_in_result + if all_NA and (value_in_result is not None) : + all_NA = False + if all_NA : + result = None + return result + + + cpdef set_item(self, index_t line_nb, object elt_id, object value): + cdef uint8_t* value_b + cdef int value_length + cdef bytes elt_name + + if value is None : + value_b = OBIQual_int_NA + value_length = 0 + else : + value_length = len(value) + value_b = malloc(value_length * sizeof(uint8_t)) + for i in range(value_length) : + value_b[i] = value[i] + + if type(elt_id) == int : + if obi_set_qual_int_with_elt_idx_and_col_p_in_view(self._view.pointer(), self.pointer(), line_nb, elt_id, value_b, value_length) < 0 : + raise Exception("Problem setting a value in a column") + else : + elt_name = tobytes(elt_id) + if obi_set_qual_int_with_elt_name_and_col_p_in_view(self._view.pointer(), self.pointer(), line_nb, elt_name, value_b, value_length) < 0: + raise Exception("Problem setting a value in a column") + + if value is not None : + free(value_b) + + + cpdef set_str_item(self, index_t line_nb, object elt_id, object value): + cdef bytes value_b + cdef bytes elt_name + + if value is None : + value_b = OBIQual_char_NA + else : + value_b = tobytes(value) + + if type(elt_id) == int : + if obi_set_qual_char_with_elt_idx_and_col_p_in_view(self._view.pointer(), self.pointer(), line_nb, elt_id, value_b) < 0 : + raise Exception("Problem setting a value in a column") + else : + elt_name = tobytes(elt_id) + if obi_set_qual_char_with_elt_name_and_col_p_in_view(self._view.pointer(), self.pointer(), line_nb, elt_name, value_b) < 0: + raise Exception("Problem setting a value in a column") + + + +def register_class(): + register_column_class(OBI_QUAL, False, Column_qual, bytes) # TODO str? int? + register_column_class(OBI_QUAL, True, Column_multi_elts_qual, bytes) # TODO str? int? + diff --git a/python/obitools3/dms/column/typed_column/seq.pxd b/python/obitools3/dms/column/typed_column/seq.pxd new file mode 100644 index 0000000..5a195bb --- /dev/null +++ b/python/obitools3/dms/column/typed_column/seq.pxd @@ -0,0 +1,21 @@ +# #cython: language_level=3 + + +from ...capi.obitypes cimport index_t + +from ..column cimport Column, \ + Column_multi_elts + + +cdef class Column_seq(Column) : + + cpdef object get_line(self, index_t line_nb) + cpdef set_line(self, index_t line_nb, object value) + + +cdef class Column_multi_elts_seq(Column_multi_elts) : + + cpdef object get_item(self, index_t line_nb, object elt_id) + cpdef object get_line(self, index_t line_nb) + cpdef set_item(self, index_t line_nb, object elt_id, object value) + \ No newline at end of file diff --git a/python/obitools3/dms/column/typed_column/seq.pyx b/python/obitools3/dms/column/typed_column/seq.pyx new file mode 100644 index 0000000..e44dd91 --- /dev/null +++ b/python/obitools3/dms/column/typed_column/seq.pyx @@ -0,0 +1,133 @@ +#cython: language_level=3 + + +from ..column cimport register_column_class + +from ...view.view cimport View + +from obitools3.utils cimport str2bytes, bytes2str, tobytes + +from ...capi.obiview cimport obi_get_seq_with_elt_name_and_col_p_in_view, \ + obi_get_seq_with_elt_idx_and_col_p_in_view, \ + obi_set_seq_with_elt_name_and_col_p_in_view, \ + obi_set_seq_with_elt_idx_and_col_p_in_view + +from ...capi.obierrno cimport obi_errno +from ...capi.obitypes cimport OBI_SEQ, OBISeq_NA + +from libc.stdlib cimport free + + +cdef class Column_seq(Column): + + @staticmethod + def new(View view, + object column_name, + index_t nb_elements_per_line=1, + object elements_names=None, + object comments=b""): + + return Column.new_column(view, column_name, OBI_SEQ, + nb_elements_per_line=nb_elements_per_line, + elements_names=elements_names, + comments=comments) + + + cpdef object get_line(self, index_t line_nb): + cdef char* value + cdef object result + global obi_errno + value = obi_get_seq_with_elt_idx_and_col_p_in_view(self._view.pointer(), self.pointer(), line_nb, 0) + if obi_errno > 0 : + raise IndexError(line_nb) + if value == OBISeq_NA : + result = None + else : # TODO + try: + result = value + finally: + free(value) + return result + + + cpdef set_line(self, index_t line_nb, object value): + cdef bytes value_b + if value is None : + value_b = OBISeq_NA + else : + value_b = tobytes(value) + if obi_set_seq_with_elt_idx_and_col_p_in_view(self._view.pointer(), self.pointer(), line_nb, 0, value_b) < 0: + raise Exception("Problem setting a value in a column") + + +cdef class Column_multi_elts_seq(Column_multi_elts): + + cpdef object get_item(self, index_t line_nb, object elt_id) : + cdef char* value + cdef object result + global obi_errno + if type(elt_id) == int : + value = obi_get_seq_with_elt_idx_and_col_p_in_view(self._view.pointer(), self.pointer(), line_nb, elt_id) + else : + elt_name = tobytes(elt_id) + value = obi_get_seq_with_elt_name_and_col_p_in_view(self._view.pointer(), self.pointer(), line_nb, elt_name) + if obi_errno > 0 : + raise IndexError(line_nb, elt_id) + if value == OBISeq_NA : + result = None + else : + try: + result = value + finally: + free(value) + return result + + + cpdef object get_line(self, index_t line_nb) : + cdef char* value + cdef object value_in_result + cdef dict result + cdef index_t i + cdef bint all_NA + global obi_errno + result = {} + all_NA = True + for i in range(self.nb_elements_per_line) : + value = obi_get_seq_with_elt_idx_and_col_p_in_view(self._view.pointer(), self.pointer(), line_nb, i) + if obi_errno > 0 : + raise IndexError(line_nb) + if value == OBISeq_NA : + value_in_result = None + else : + try: + value_in_result = value + finally: + free(value) + result[self.elements_names[i]] = value_in_result + if all_NA and (value_in_result is not None) : + all_NA = False + if all_NA : + result = None + return result + + + cpdef set_item(self, index_t line_nb, object elt_id, object value): + cdef bytes value_b + cdef bytes elt_name + if value is None : + value_b = OBISeq_NA + else : + value_b = tobytes(value) + if type(elt_id) == int : + if obi_set_seq_with_elt_idx_and_col_p_in_view(self._view.pointer(), self.pointer(), line_nb, elt_id, value_b) < 0: + raise Exception("Problem setting a value in a column") + else : + elt_name = tobytes(elt_id) + if obi_set_seq_with_elt_name_and_col_p_in_view(self._view.pointer(), self.pointer(), line_nb, elt_name, value_b) < 0: + raise Exception("Problem setting a value in a column") + + +def register_class(): + register_column_class(OBI_SEQ, False, Column_seq, bytes) # TODO str? + register_column_class(OBI_SEQ, True, Column_multi_elts_seq, bytes) # TODO str? + diff --git a/python/obitools3/dms/column/typed_column/str.pxd b/python/obitools3/dms/column/typed_column/str.pxd new file mode 100644 index 0000000..5bb8ddc --- /dev/null +++ b/python/obitools3/dms/column/typed_column/str.pxd @@ -0,0 +1,21 @@ +# #cython: language_level=3 + + +from ...capi.obitypes cimport index_t + +from ..column cimport Column, \ + Column_multi_elts + + +cdef class Column_str(Column) : + + cpdef object get_line(self, index_t line_nb) + cpdef set_line(self, index_t line_nb, object value) + + +cdef class Column_multi_elts_str(Column_multi_elts) : + + cpdef object get_item(self, index_t line_nb, object elt_id) + cpdef object get_line(self, index_t line_nb) + cpdef set_item(self, index_t line_nb, object elt_id, object value) + \ No newline at end of file diff --git a/python/obitools3/dms/column/typed_column/str.pyx b/python/obitools3/dms/column/typed_column/str.pyx new file mode 100644 index 0000000..89a183f --- /dev/null +++ b/python/obitools3/dms/column/typed_column/str.pyx @@ -0,0 +1,122 @@ +#cython: language_level=3 + + +from ..column cimport register_column_class + +from ...view.view cimport View + +from obitools3.utils cimport str2bytes, bytes2str, tobytes + +from ...capi.obiview cimport obi_get_str_with_elt_name_and_col_p_in_view, \ + obi_get_str_with_elt_idx_and_col_p_in_view, \ + obi_set_str_with_elt_name_and_col_p_in_view, \ + obi_set_str_with_elt_idx_and_col_p_in_view + +from ...capi.obierrno cimport obi_errno +from ...capi.obitypes cimport OBI_STR, OBIStr_NA + + +cdef class Column_str(Column): + + @staticmethod + def new(View view, + object column_name, + index_t nb_elements_per_line=1, + object elements_names=None, + object comments=b""): + + return Column.new_column(view, column_name, OBI_STR, + nb_elements_per_line=nb_elements_per_line, + elements_names=elements_names, + comments=comments) + + + cpdef object get_line(self, index_t line_nb): + cdef char* value + cdef object result + global obi_errno + value = obi_get_str_with_elt_idx_and_col_p_in_view(self._view.pointer(), self.pointer(), line_nb, 0) + if obi_errno > 0 : + raise IndexError(line_nb) + if value == OBIStr_NA : + result = None + else : + result = value # NOTE: value is not freed because the pointer points to a mmapped region in an AVL data file. + return result + + + cpdef set_line(self, index_t line_nb, object value): + cdef bytes value_b + if value is None : + value_b = OBIStr_NA + else : + value_b = tobytes(value) + if obi_set_str_with_elt_idx_and_col_p_in_view(self._view.pointer(), self.pointer(), line_nb, 0, value_b) < 0: + raise Exception("Problem setting a value in a column") + + +cdef class Column_multi_elts_str(Column_multi_elts): + + cpdef object get_item(self, index_t line_nb, object elt_id) : + cdef char* value + cdef object result + global obi_errno + if type(elt_id) == int : + value = obi_get_str_with_elt_idx_and_col_p_in_view(self._view.pointer(), self.pointer(), line_nb, elt_id) + else : + elt_name = tobytes(elt_id) + value = obi_get_str_with_elt_name_and_col_p_in_view(self._view.pointer(), self.pointer(), line_nb, elt_name) + if obi_errno > 0 : + raise IndexError(line_nb, elt_id) + if value == OBIStr_NA : + result = None + else : + result = value # NOTE: value is not freed because the pointer points to a mmapped region in an AVL data file. + return result + + + cpdef object get_line(self, index_t line_nb) : + cdef char* value + cdef object value_in_result + cdef dict result + cdef index_t i + cdef bint all_NA + global obi_errno + result = {} + all_NA = True + for i in range(self.nb_elements_per_line) : + value = obi_get_str_with_elt_idx_and_col_p_in_view(self._view.pointer(), self.pointer(), line_nb, i) + if obi_errno > 0 : + raise IndexError(line_nb) + if value == OBIStr_NA : + value_in_result = None + else : + value_in_result = value + result[self.elements_names[i]] = value_in_result + if all_NA and (value_in_result is not None) : + all_NA = False + if all_NA : + result = None + return result + + + cpdef set_item(self, index_t line_nb, object elt_id, object value): + cdef bytes value_b + cdef bytes elt_name + if value is None : + value_b = OBIStr_NA + else : + value_b = tobytes(value) + if type(elt_id) == int : + if obi_set_str_with_elt_idx_and_col_p_in_view(self._view.pointer(), self.pointer(), line_nb, elt_id, value_b) < 0: + raise Exception("Problem setting a value in a column") + else : + elt_name = tobytes(elt_id) + if obi_set_str_with_elt_name_and_col_p_in_view(self._view.pointer(), self.pointer(), line_nb, elt_name, value_b) < 0: + raise Exception("Problem setting a value in a column") + + +def register_class(): + register_column_class(OBI_STR, False, Column_str, bytes) # TODO str? + register_column_class(OBI_STR, True, Column_multi_elts_str, bytes) # TODO str? +