obi arrays that don't work because of cython bug passing wrong pointers
This commit is contained in:
121
python/obitools3/obidms/_obidmscolumn_str.pyx
Normal file
121
python/obitools3/obidms/_obidmscolumn_str.pyx
Normal file
@ -0,0 +1,121 @@
|
||||
#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.obierrno cimport obi_errno
|
||||
from .capi.obitypes cimport OBIIdx_NA, const_char_p
|
||||
|
||||
|
||||
from libc.string cimport strlen
|
||||
|
||||
from obitools3.utils cimport str2bytes, bytes2str
|
||||
|
||||
from cpython.int cimport PyInt_FromSsize_t
|
||||
|
||||
|
||||
cdef class OBIDMS_column_str(OBIDMS_column):
|
||||
|
||||
cpdef object get_line(self, size_t line_nb):
|
||||
cdef const_char_p cvalue
|
||||
cdef bytes value
|
||||
cdef object result
|
||||
|
||||
cvalue = obi_column_get_obistr_with_elt_idx(self.pointer, line_nb, 0)
|
||||
|
||||
print('test 1')
|
||||
|
||||
print(hex(<int> cvalue))
|
||||
|
||||
|
||||
print(strlen(cvalue))
|
||||
print("test")
|
||||
value = <bytes> cvalue
|
||||
print(value)
|
||||
if obi_errno > 0 :
|
||||
raise IndexError(line_nb)
|
||||
if value == OBIIdx_NA :
|
||||
result = None
|
||||
else :
|
||||
print(value)
|
||||
result = bytes2str(value)
|
||||
return result
|
||||
|
||||
cpdef set_line(self, size_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, size_t line_nb, object value):
|
||||
if obi_column_set_obistr_with_elt_idx(self.pointer, line_nb, 0, str2bytes(value)) < 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):
|
||||
|
||||
cpdef object get_item(self, size_t line_nb, str element_name):
|
||||
cdef bytes value
|
||||
cdef object result
|
||||
value = obi_column_get_obistr_with_elt_name(self.pointer, line_nb, str2bytes(element_name))
|
||||
if obi_errno > 0 :
|
||||
raise IndexError(line_nb, element_name)
|
||||
if value == OBIIdx_NA :
|
||||
result = None
|
||||
else :
|
||||
result = bytes2str(value)
|
||||
return result
|
||||
|
||||
cpdef object get_line(self, size_t line_nb) :
|
||||
cdef bytes value
|
||||
cdef object result
|
||||
cdef size_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(self.pointer, 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) :
|
||||
all_NA = False
|
||||
if all_NA :
|
||||
result = None
|
||||
return result
|
||||
|
||||
cpdef set_item(self, size_t line_nb, str element_name, str value):
|
||||
raise Exception("Column is read-only")
|
||||
|
||||
cpdef set_line(self, size_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, size_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:
|
||||
raise Exception("Problem setting a value in a column")
|
||||
|
||||
cpdef set_line(self, size_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