Major update : views
This commit is contained in:
@ -1,13 +1,12 @@
|
||||
#cython: language_level=3
|
||||
|
||||
from .capi.obidmscolumn cimport obi_close_column,\
|
||||
obi_truncate_and_close_column, \
|
||||
obi_column_get_obistr_with_elt_name, \
|
||||
obi_column_get_obistr_with_elt_idx, \
|
||||
obi_column_set_obistr_with_elt_name, \
|
||||
obi_column_set_obistr_with_elt_idx
|
||||
from .capi.obidmscolumn cimport obi_truncate_and_close_column, \
|
||||
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 OBIIdx_NA, const_char_p
|
||||
from .capi.obitypes cimport OBIStr_NA, const_char_p
|
||||
|
||||
from obitools3.utils cimport str2bytes, bytes2str
|
||||
|
||||
@ -17,87 +16,68 @@ cdef class OBIDMS_column_str(OBIDMS_column):
|
||||
cpdef object get_line(self, index_t line_nb):
|
||||
cdef bytes value
|
||||
cdef object result
|
||||
value = <bytes> obi_column_get_obistr_with_elt_idx(self.pointer, line_nb, 0)
|
||||
value = <bytes> obi_column_get_obistr_with_elt_idx_in_view(self.view, (self.pointer)[0], line_nb, 0)
|
||||
if obi_errno > 0 :
|
||||
raise IndexError(line_nb)
|
||||
if value == OBIIdx_NA :
|
||||
if value == OBIStr_NA : # TODO
|
||||
result = None
|
||||
else :
|
||||
result = bytes2str(value)
|
||||
return result
|
||||
|
||||
cpdef set_line(self, index_t line_nb, object value):
|
||||
raise Exception("Column is read-only")
|
||||
|
||||
cpdef close(self):
|
||||
if obi_close_column(self.pointer) < 0 :
|
||||
raise Exception("Problem closing a column")
|
||||
|
||||
|
||||
cdef class OBIDMS_column_str_writable(OBIDMS_column_str):
|
||||
|
||||
cpdef set_line(self, index_t line_nb, object value):
|
||||
if obi_column_set_obistr_with_elt_idx(self.pointer, line_nb, 0, str2bytes(value)) < 0:
|
||||
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, (self.pointer)[0], line_nb, 0, value_b) < 0:
|
||||
raise Exception("Problem setting a value in a column")
|
||||
|
||||
cpdef close(self):
|
||||
if obi_truncate_and_close_column(self.pointer) < 0 :
|
||||
raise Exception("Problem closing a column")
|
||||
|
||||
|
||||
cdef class OBIDMS_column_str_multi_elts(OBIDMS_column_str):
|
||||
cdef class OBIDMS_column_multi_elts_str(OBIDMS_column_multi_elts):
|
||||
|
||||
cpdef object get_item(self, index_t line_nb, str element_name):
|
||||
cdef bytes value
|
||||
cdef object result
|
||||
value = <bytes> obi_column_get_obistr_with_elt_name(self.pointer, line_nb, str2bytes(element_name))
|
||||
value = <bytes> obi_column_get_obistr_with_elt_name_in_view(self.view, (self.pointer)[0], line_nb, str2bytes(element_name))
|
||||
if obi_errno > 0 :
|
||||
raise IndexError(line_nb, element_name)
|
||||
if value == OBIIdx_NA :
|
||||
if value == OBIStr_NA :
|
||||
result = None
|
||||
else :
|
||||
result = bytes2str(value)
|
||||
return result
|
||||
|
||||
|
||||
cpdef object get_line(self, index_t line_nb) :
|
||||
cdef bytes value
|
||||
cdef object result
|
||||
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 = <bytes> obi_column_get_obistr_with_elt_idx(self.pointer, line_nb, i)
|
||||
value = <bytes> obi_column_get_obistr_with_elt_idx_in_view(self.view, (self.pointer)[0], line_nb, i)
|
||||
if obi_errno > 0 :
|
||||
raise IndexError(line_nb)
|
||||
result[self.elements_names[i]] = bytes2str(value)
|
||||
if all_NA and (value != OBIIdx_NA) :
|
||||
raise IndexError(line_nb)
|
||||
if value == OBIStr_NA :
|
||||
value_in_result = None
|
||||
else :
|
||||
value_in_result = bytes2str(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, str element_name, str value):
|
||||
raise Exception("Column is read-only")
|
||||
|
||||
cpdef set_line(self, index_t line_nb, object values):
|
||||
raise Exception("Column is read-only")
|
||||
|
||||
|
||||
cdef class OBIDMS_column_str_multi_elts_writable(OBIDMS_column_str_multi_elts):
|
||||
|
||||
cpdef set_item(self, index_t line_nb, str element_name, str value):
|
||||
if obi_column_set_obistr_with_elt_name(self.pointer, line_nb, str2bytes(element_name), str2bytes(value)) < 0:
|
||||
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, (self.pointer)[0], line_nb, str2bytes(element_name), value_b) < 0:
|
||||
raise Exception("Problem setting a value in a column")
|
||||
|
||||
cpdef set_line(self, index_t line_nb, object values):
|
||||
cdef str value
|
||||
for element_name in values :
|
||||
value = values[element_name]
|
||||
self.set_item(line_nb, element_name, value)
|
||||
|
||||
cpdef close(self):
|
||||
if obi_truncate_and_close_column(self.pointer) < 0 :
|
||||
raise Exception("Problem closing a column")
|
||||
|
||||
|
Reference in New Issue
Block a user