Cython API: added a Column subclass to allow direct access to indexes
for columns that store indexes referring to other data
This commit is contained in:
21
python/obitools3/dms/column/column_idx.pxd
Normal file
21
python/obitools3/dms/column/column_idx.pxd
Normal file
@ -0,0 +1,21 @@
|
||||
# #cython: language_level=3
|
||||
|
||||
|
||||
from ..capi.obitypes cimport index_t
|
||||
|
||||
from .column cimport Column, \
|
||||
Column_multi_elts
|
||||
|
||||
|
||||
cdef class Column_idx(Column) :
|
||||
|
||||
cpdef object get_line_idx(self, index_t line_nb)
|
||||
cpdef set_line_idx(self, index_t line_nb, object value)
|
||||
|
||||
|
||||
cdef class Column_multi_elts_idx(Column_multi_elts) :
|
||||
|
||||
cpdef object get_item_idx(self, index_t line_nb, object elt_id)
|
||||
cpdef object get_line_idx(self, index_t line_nb)
|
||||
cpdef set_item_idx(self, index_t line_nb, object elt_id, object value)
|
||||
cpdef set_line_idx(self, index_t line_nb, object values)
|
114
python/obitools3/dms/column/column_idx.pyx
Normal file
114
python/obitools3/dms/column/column_idx.pyx
Normal file
@ -0,0 +1,114 @@
|
||||
#cython: language_level=3
|
||||
|
||||
from obitools3.dms.capi.obierrno cimport obi_errno
|
||||
|
||||
from ..view.view cimport View
|
||||
|
||||
from obitools3.utils cimport tobytes, \
|
||||
obi_errno_to_exception
|
||||
|
||||
from ..capi.obiview cimport obi_get_index_with_elt_name_and_col_p_in_view, \
|
||||
obi_get_index_with_elt_idx_and_col_p_in_view, \
|
||||
obi_set_index_with_elt_name_and_col_p_in_view, \
|
||||
obi_set_index_with_elt_idx_and_col_p_in_view, \
|
||||
Obiview_p
|
||||
|
||||
from ..capi.obidmscolumn cimport OBIDMS_column_p
|
||||
|
||||
from ..capi.obitypes cimport OBI_IDX, OBIIdx_NA, index_t
|
||||
|
||||
from cpython.long cimport PyLong_FromLongLong
|
||||
|
||||
|
||||
# TODO overwrite other functions from Column and Column_multi_elts
|
||||
|
||||
|
||||
cdef class Column_idx(Column):
|
||||
|
||||
cpdef object get_line_idx(self, index_t line_nb):
|
||||
global obi_errno
|
||||
cdef index_t value
|
||||
cdef object result
|
||||
value = obi_get_index_with_elt_idx_and_col_p_in_view(self._view.pointer(), self.pointer(), line_nb, 0)
|
||||
obi_errno_to_exception(obi_errno, line_nb=line_nb, elt_id=None, error_message="Problem getting a value from a column")
|
||||
if value == OBIIdx_NA :
|
||||
result = None
|
||||
else :
|
||||
result = PyLong_FromLongLong(value)
|
||||
return result
|
||||
|
||||
cpdef set_line_idx(self, index_t line_nb, object value):
|
||||
global obi_errno
|
||||
if value is None :
|
||||
value = OBIIdx_NA
|
||||
if obi_set_index_with_elt_idx_and_col_p_in_view(self._view.pointer(), self.pointer(), line_nb, 0, <index_t> value) < 0:
|
||||
obi_errno_to_exception(obi_errno, line_nb=line_nb, elt_id=None, error_message="Problem setting a value in a column")
|
||||
|
||||
|
||||
cdef class Column_multi_elts_idx(Column_multi_elts):
|
||||
|
||||
cpdef object get_item_idx(self, index_t line_nb, object elt_id) :
|
||||
global obi_errno
|
||||
cdef int value
|
||||
cdef object result
|
||||
cdef bytes elt_name
|
||||
if type(elt_id) == int :
|
||||
value = obi_get_index_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_index_with_elt_name_and_col_p_in_view(self._view.pointer(), self.pointer(), line_nb, elt_name)
|
||||
obi_errno_to_exception(obi_errno, line_nb=line_nb, elt_id=elt_id, error_message="Problem getting a value from a column")
|
||||
if value == OBIIdx_NA :
|
||||
result = None
|
||||
else :
|
||||
result = PyLong_FromLongLong(value)
|
||||
return result
|
||||
|
||||
cpdef object get_line_idx(self, index_t line_nb) :
|
||||
global obi_errno
|
||||
cdef index_t value
|
||||
cdef int value_in_result
|
||||
cdef dict result
|
||||
cdef index_t i
|
||||
cdef bint all_NA
|
||||
cdef list elements_names
|
||||
cdef Obiview_p view_p
|
||||
cdef OBIDMS_column_p column_p
|
||||
result = {}
|
||||
all_NA = True
|
||||
view_p = self._view.pointer()
|
||||
column_p = self.pointer()
|
||||
elements_names = self.elements_names
|
||||
for i in range(self.nb_elements_per_line) :
|
||||
value = obi_get_index_with_elt_idx_and_col_p_in_view(view_p, column_p, line_nb, i)
|
||||
obi_errno_to_exception(obi_errno, line_nb=line_nb, elt_id=i, error_message="Problem getting a value from a column")
|
||||
if value != OBIIdx_NA :
|
||||
value_in_result = PyLong_FromLongLong(value)
|
||||
result[elements_names[i]] = value_in_result
|
||||
if all_NA :
|
||||
all_NA = False
|
||||
if all_NA :
|
||||
result = None
|
||||
return result
|
||||
|
||||
cpdef set_item_idx(self, index_t line_nb, object elt_id, object value) :
|
||||
global obi_errno
|
||||
cdef bytes elt_name
|
||||
if value is None :
|
||||
value = OBIIdx_NA
|
||||
if type(elt_id) == int :
|
||||
if obi_set_index_with_elt_idx_and_col_p_in_view(self._view.pointer(), self.pointer(), line_nb, elt_id, <index_t> value) < 0 :
|
||||
obi_errno_to_exception(obi_errno, line_nb=line_nb, elt_id=elt_id, error_message="Problem setting a value in a column")
|
||||
else :
|
||||
elt_name = tobytes(elt_id)
|
||||
if obi_set_index_with_elt_name_and_col_p_in_view(self._view.pointer(), self.pointer(), line_nb, elt_name, <index_t> value) < 0 :
|
||||
obi_errno_to_exception(obi_errno, line_nb=line_nb, elt_id=elt_id, error_message="Problem setting a value in a column")
|
||||
|
||||
cpdef set_line_idx(self, index_t line_nb, object values):
|
||||
if values is None :
|
||||
for element_name in self.elements_names :
|
||||
self.set_item_idx(line_nb, element_name, None)
|
||||
else :
|
||||
for element_name in values :
|
||||
self.set_item_idx(line_nb, element_name, values[element_name])
|
||||
|
@ -3,17 +3,17 @@
|
||||
|
||||
from ...capi.obitypes cimport index_t
|
||||
|
||||
from ..column cimport Column, \
|
||||
Column_multi_elts
|
||||
from ..column_idx cimport Column_idx, \
|
||||
Column_multi_elts_idx
|
||||
|
||||
|
||||
cdef class Column_seq(Column) :
|
||||
cdef class Column_seq(Column_idx) :
|
||||
|
||||
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) :
|
||||
cdef class Column_multi_elts_seq(Column_multi_elts_idx) :
|
||||
|
||||
cpdef object get_item(self, index_t line_nb, object elt_id)
|
||||
cpdef object get_line(self, index_t line_nb)
|
||||
|
@ -6,6 +6,8 @@ from ..column cimport register_column_class
|
||||
|
||||
from ...view.view cimport View
|
||||
|
||||
from ..column cimport Column
|
||||
|
||||
from obitools3.utils cimport tobytes, \
|
||||
obi_errno_to_exception
|
||||
|
||||
@ -22,7 +24,7 @@ from ...capi.obitypes cimport OBI_SEQ, OBISeq_NA
|
||||
from libc.stdlib cimport free
|
||||
|
||||
|
||||
cdef class Column_seq(Column):
|
||||
cdef class Column_seq(Column_idx):
|
||||
|
||||
@staticmethod
|
||||
def new(View view,
|
||||
@ -68,7 +70,7 @@ cdef class Column_seq(Column):
|
||||
obi_errno_to_exception(obi_errno, line_nb=line_nb, elt_id=None, error_message="Problem setting a value in a column")
|
||||
|
||||
|
||||
cdef class Column_multi_elts_seq(Column_multi_elts):
|
||||
cdef class Column_multi_elts_seq(Column_multi_elts_idx):
|
||||
|
||||
cpdef object get_item(self, index_t line_nb, object elt_id) :
|
||||
global obi_errno
|
||||
|
@ -3,17 +3,17 @@
|
||||
|
||||
from ...capi.obitypes cimport index_t
|
||||
|
||||
from ..column cimport Column, \
|
||||
Column_multi_elts
|
||||
from ..column_idx cimport Column_idx, \
|
||||
Column_multi_elts_idx
|
||||
|
||||
|
||||
cdef class Column_str(Column) :
|
||||
cdef class Column_str(Column_idx) :
|
||||
|
||||
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) :
|
||||
cdef class Column_multi_elts_str(Column_multi_elts_idx) :
|
||||
|
||||
cpdef object get_item(self, index_t line_nb, object elt_id)
|
||||
cpdef object get_line(self, index_t line_nb)
|
||||
|
@ -6,6 +6,8 @@ from ..column cimport register_column_class
|
||||
|
||||
from ...view.view cimport View
|
||||
|
||||
from ..column cimport Column
|
||||
|
||||
from obitools3.utils cimport tobytes, \
|
||||
obi_errno_to_exception
|
||||
|
||||
@ -20,7 +22,7 @@ from ...capi.obidmscolumn cimport OBIDMS_column_p
|
||||
from ...capi.obitypes cimport OBI_STR, OBIStr_NA, const_char_p
|
||||
|
||||
|
||||
cdef class Column_str(Column):
|
||||
cdef class Column_str(Column_idx):
|
||||
|
||||
@staticmethod
|
||||
def new(View view,
|
||||
@ -63,7 +65,7 @@ cdef class Column_str(Column):
|
||||
obi_errno_to_exception(obi_errno, line_nb=line_nb, elt_id=None, error_message="Problem setting a value in a column")
|
||||
|
||||
|
||||
cdef class Column_multi_elts_str(Column_multi_elts):
|
||||
cdef class Column_multi_elts_str(Column_multi_elts_idx):
|
||||
|
||||
cpdef object get_item(self, index_t line_nb, object elt_id) :
|
||||
global obi_errno
|
||||
|
Reference in New Issue
Block a user