Cython API: None values aren't inclued anymore in the dictionary

returned when getting a line from a column with multiple elements per
line, and reworked that function to be more optimized
This commit is contained in:
Celine Mercier
2017-07-07 17:28:53 +02:00
parent 143bddf1d1
commit b94ec9557f
7 changed files with 155 additions and 104 deletions

View File

@ -12,7 +12,10 @@ from obitools3.utils cimport tobytes, \
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
obi_set_bool_with_elt_idx_and_col_p_in_view, \
Obiview_p
from ...capi.obidmscolumn cimport OBIDMS_column_p
from ...capi.obitypes cimport OBI_BOOL, OBIBool_NA, obibool_t
@ -32,7 +35,6 @@ cdef class Column_bool(Column):
elements_names=elements_names,
comments=comments)
cpdef object get_line(self, index_t line_nb):
global obi_errno
cdef obibool_t value
@ -45,7 +47,6 @@ cdef class Column_bool(Column):
result = PyBool_FromLong(value)
return result
cpdef set_line(self, index_t line_nb, object value):
global obi_errno
if value is None :
@ -75,23 +76,27 @@ cdef class Column_multi_elts_bool(Column_multi_elts):
cpdef object get_line(self, index_t line_nb) :
global obi_errno
cdef obibool_t value
cdef object value_in_result
cdef dict result
cdef index_t i
cdef bint all_NA
cdef obibool_t value
cdef bint 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_bool_with_elt_idx_and_col_p_in_view(self._view.pointer(), self.pointer(), line_nb, i)
value = obi_get_bool_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 == OBIBool_NA :
value_in_result = None
else :
if value != OBIBool_NA :
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
result[elements_names[i]] = value_in_result
if all_NA :
all_NA = False
if all_NA :
result = None
return result

View File

@ -12,7 +12,10 @@ from obitools3.utils cimport 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
obi_set_char_with_elt_idx_and_col_p_in_view, \
Obiview_p
from ...capi.obidmscolumn cimport OBIDMS_column_p
from ...capi.obitypes cimport OBI_CHAR, OBIChar_NA, obichar_t
@ -76,23 +79,27 @@ cdef class Column_multi_elts_char(Column_multi_elts):
cpdef object get_line(self, index_t line_nb) :
global obi_errno
cdef obichar_t value
cdef object value_in_result
cdef dict result
cdef index_t i
cdef bint all_NA
cdef obichar_t value
cdef bytes 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_char_with_elt_idx_and_col_p_in_view(self._view.pointer(), self.pointer(), line_nb, i)
value = obi_get_char_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 == OBIChar_NA :
value_in_result = None
else :
value_in_result = <bytes>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 value != OBIChar_NA :
value_in_result = <bytes>value
result[elements_names[i]] = value_in_result
if all_NA :
all_NA = False
if all_NA :
result = None
return result

View File

@ -12,7 +12,10 @@ from obitools3.utils cimport 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
obi_set_float_with_elt_idx_and_col_p_in_view, \
Obiview_p
from ...capi.obidmscolumn cimport OBIDMS_column_p
from ...capi.obitypes cimport OBI_FLOAT, OBIFloat_NA, obifloat_t
@ -73,23 +76,27 @@ cdef class Column_multi_elts_float(Column_multi_elts):
cpdef object get_line(self, index_t line_nb) :
global obi_errno
cdef obifloat_t value
cdef object value_in_result
cdef dict result
cdef index_t i
cdef bint all_NA
cdef obifloat_t value
cdef double 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_float_with_elt_idx_and_col_p_in_view(self._view.pointer(), self.pointer(), line_nb, i)
value = obi_get_float_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 == OBIFloat_NA :
value_in_result = None
else :
value_in_result = <double> value
result[self.elements_names[i]] = value_in_result
if all_NA and (value_in_result is not None) :
all_NA = False
if value != OBIFloat_NA :
value_in_result = <double> value
result[elements_names[i]] = value_in_result
if all_NA :
all_NA = False
if all_NA :
result = None
return result

View File

@ -12,7 +12,10 @@ from obitools3.utils cimport 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
obi_set_int_with_elt_idx_and_col_p_in_view, \
Obiview_p
from ...capi.obidmscolumn cimport OBIDMS_column_p
from ...capi.obitypes cimport OBI_INT, OBIInt_NA, obiint_t
@ -76,23 +79,27 @@ cdef class Column_multi_elts_int(Column_multi_elts):
cpdef object get_line(self, index_t line_nb) :
global obi_errno
cdef obiint_t value
cdef object value_in_result
cdef dict result
cdef index_t i
cdef bint all_NA
cdef obiint_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_int_with_elt_idx_and_col_p_in_view(self._view.pointer(), self.pointer(), line_nb, i)
value = obi_get_int_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 == 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 value != OBIInt_NA :
value_in_result = PyInt_FromLong(value)
result[elements_names[i]] = value_in_result
if all_NA :
all_NA = False
if all_NA :
result = None
return result

View File

@ -16,7 +16,10 @@ from ...capi.obiview cimport obi_get_qual_char_with_elt_name_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
obi_set_qual_int_with_elt_idx_and_col_p_in_view, \
Obiview_p
from ...capi.obidmscolumn cimport OBIDMS_column_p
from ...capi.obitypes cimport OBI_QUAL, OBIQual_char_NA, OBIQual_int_NA, const_char_p
@ -145,25 +148,29 @@ cdef class Column_multi_elts_qual(Column_multi_elts):
global obi_errno
cdef const uint8_t* value
cdef int value_length
cdef object value_in_result
cdef list value_in_result
cdef dict result
cdef index_t i
cdef int j
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_qual_int_with_elt_idx_and_col_p_in_view(self._view.pointer(), self.pointer(), line_nb, i, &value_length)
obi_errno_to_exception(obi_errno, line_nb=line_nb, elt_id=i, error_message="Problem getting a value from a column")
if value == OBIQual_int_NA :
value_in_result = None
else :
if value != OBIQual_int_NA :
value_in_result = []
for j in range(value_length) :
value_in_result.append(<int>value[j])
result[self.elements_names[i]] = value_in_result
if all_NA and (value_in_result is not None) :
all_NA = False
value_in_result.append(<int>value[j])
result[elements_names[i]] = value_in_result
if all_NA :
all_NA = False
if all_NA :
result = None
return result
@ -171,24 +178,28 @@ cdef class Column_multi_elts_qual(Column_multi_elts):
cpdef object get_str_line(self, index_t line_nb) :
global obi_errno
cdef char* value
cdef object value_in_result
cdef dict result
cdef index_t i
cdef bint all_NA
cdef char* value
cdef object 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_qual_char_with_elt_idx_and_col_p_in_view(self._view.pointer(), self.pointer(), 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 == OBIQual_char_NA :
value_in_result = None
else :
if value != OBIQual_char_NA :
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
result[elements_names[i]] = value_in_result
if all_NA :
all_NA = False
if all_NA :
result = None
return result

View File

@ -12,7 +12,10 @@ from obitools3.utils cimport 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
obi_set_seq_with_elt_idx_and_col_p_in_view, \
Obiview_p
from ...capi.obidmscolumn cimport OBIDMS_column_p
from ...capi.obitypes cimport OBI_SEQ, OBISeq_NA
@ -89,26 +92,30 @@ cdef class Column_multi_elts_seq(Column_multi_elts):
cpdef object get_line(self, index_t line_nb) :
global obi_errno
cdef char* value
cdef object value_in_result
cdef dict result
cdef index_t i
cdef bint all_NA
cdef char* value
cdef object 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_seq_with_elt_idx_and_col_p_in_view(self._view.pointer(), self.pointer(), line_nb, i)
value = obi_get_seq_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 == OBISeq_NA :
value_in_result = None
else :
if value != OBISeq_NA :
try:
value_in_result = <bytes> 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
result[elements_names[i]] = value_in_result
if all_NA :
all_NA = False
if all_NA :
result = None
return result

View File

@ -12,7 +12,10 @@ from obitools3.utils cimport 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
obi_set_str_with_elt_idx_and_col_p_in_view, \
Obiview_p
from ...capi.obidmscolumn cimport OBIDMS_column_p
from ...capi.obitypes cimport OBI_STR, OBIStr_NA, const_char_p
@ -81,23 +84,27 @@ cdef class Column_multi_elts_str(Column_multi_elts):
cpdef object get_line(self, index_t line_nb) :
global obi_errno
cdef const_char_p value
cdef object value_in_result
cdef dict result
cdef index_t i
cdef bint all_NA
cdef const_char_p value
cdef object 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_str_with_elt_idx_and_col_p_in_view(self._view.pointer(), self.pointer(), line_nb, i)
value = obi_get_str_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 == OBIStr_NA :
value_in_result = None
else :
value_in_result = <bytes> value
result[self.elements_names[i]] = value_in_result
if all_NA and (value_in_result is not None) :
all_NA = False
if value != OBIStr_NA :
value_in_result = <bytes> value
result[elements_names[i]] = value_in_result
if all_NA :
all_NA = False
if all_NA :
result = None
return result