86 lines
3.5 KiB
Cython
86 lines
3.5 KiB
Cython
#cython: language_level=3
|
|
|
|
from .capi.obiview cimport obi_column_get_obistr_with_elt_name_in_view, \
|
|
obi_column_get_obistr_with_elt_idx_in_view, \
|
|
obi_column_set_obistr_with_elt_name_in_view, \
|
|
obi_column_set_obistr_with_elt_idx_in_view
|
|
from .capi.obierrno cimport obi_errno
|
|
from .capi.obitypes cimport OBIStr_NA, const_char_p
|
|
|
|
from obitools3.utils cimport str2bytes, bytes2str
|
|
|
|
|
|
cdef class OBIDMS_column_str(OBIDMS_column):
|
|
|
|
cpdef object get_line(self, index_t line_nb):
|
|
cdef const_char_p value
|
|
cdef object result
|
|
value = obi_column_get_obistr_with_elt_idx_in_view(self.view.pointer, (self.pointer)[0], line_nb, 0)
|
|
if obi_errno > 0 :
|
|
raise IndexError(line_nb)
|
|
if value == OBIStr_NA :
|
|
result = None
|
|
else :
|
|
result = bytes2str(value)
|
|
# NOTE: value is not freed because the pointer points to a mmapped region in an AVL data file. (TODO discuss)
|
|
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 = str2bytes(value)
|
|
if obi_column_set_obistr_with_elt_idx_in_view(self.view.pointer, (self.pointer)[0], line_nb, 0, value_b) < 0:
|
|
raise Exception("Problem setting a value in a column")
|
|
|
|
|
|
cdef class OBIDMS_column_multi_elts_str(OBIDMS_column_multi_elts):
|
|
|
|
cpdef object get_item(self, index_t line_nb, str element_name):
|
|
cdef const_char_p value
|
|
cdef object result
|
|
value = obi_column_get_obistr_with_elt_name_in_view(self.view.pointer, (self.pointer)[0], line_nb, str2bytes(element_name))
|
|
if obi_errno > 0 :
|
|
raise IndexError(line_nb, element_name)
|
|
if value == OBIStr_NA :
|
|
result = None
|
|
else :
|
|
result = bytes2str(value)
|
|
# NOTE: value is not freed because the pointer points to a mmapped region in an AVL data file. (TODO discuss)
|
|
return result
|
|
|
|
cpdef object get_line(self, index_t line_nb) :
|
|
cdef const_char_p 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_column_get_obistr_with_elt_idx_in_view(self.view.pointer, (self.pointer)[0], line_nb, i)
|
|
if obi_errno > 0 :
|
|
raise IndexError(line_nb)
|
|
if value == OBIStr_NA :
|
|
value_in_result = None
|
|
else :
|
|
value_in_result = bytes2str(value)
|
|
# NOTE: value is not freed because the pointer points to a mmapped region in an AVL data file. (TODO discuss)
|
|
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, str element_name, object value):
|
|
cdef bytes value_b
|
|
if value is None :
|
|
value_b = OBIStr_NA
|
|
else :
|
|
value_b = str2bytes(value)
|
|
if obi_column_set_obistr_with_elt_name_in_view(self.view.pointer, (self.pointer)[0], line_nb, str2bytes(element_name), value_b) < 0:
|
|
raise Exception("Problem setting a value in a column")
|
|
|