_obidmscolumn_float.pyx 2.89 KB
Newer Older
1 2
#cython: language_level=3

3
from .capi.obiview      cimport obi_column_get_obifloat_with_elt_name_in_view, \
Celine Mercier's avatar
Celine Mercier committed
4 5 6
                                obi_column_get_obifloat_with_elt_idx_in_view, \
                                obi_column_set_obifloat_with_elt_name_in_view, \
                                obi_column_set_obifloat_with_elt_idx_in_view
7
from .capi.obierrno     cimport obi_errno
Celine Mercier's avatar
Celine Mercier committed
8
from .capi.obitypes     cimport OBIFloat_NA, obifloat_t
9 10 11 12 13 14

from obitools3.utils cimport str2bytes


cdef class OBIDMS_column_float(OBIDMS_column):

15
    cpdef object get_line(self, index_t line_nb):
16 17
        cdef obifloat_t value
        cdef object result
Celine Mercier's avatar
Celine Mercier committed
18
        value = obi_column_get_obifloat_with_elt_idx_in_view(self.view, (self.pointer)[0], line_nb, 0)
19 20 21 22 23 24 25 26
        if obi_errno > 0 :
            raise IndexError(line_nb)
        if value == OBIFloat_NA :
            result = None
        else :
            result = <double> value
        return result

27
    cpdef set_line(self, index_t line_nb, object value):
Celine Mercier's avatar
Celine Mercier committed
28 29 30
        if value is None :
            value = OBIFloat_NA
        if obi_column_set_obifloat_with_elt_idx_in_view(self.view, (self.pointer)[0], line_nb, 0, <obifloat_t> value) < 0:
31 32 33
            raise Exception("Problem setting a value in a column")


Celine Mercier's avatar
Celine Mercier committed
34
cdef class OBIDMS_column_multi_elts_float(OBIDMS_column_multi_elts):
35

36
    cpdef object get_item(self, index_t line_nb, str element_name):
37 38
        cdef obifloat_t value
        cdef object result
Celine Mercier's avatar
Celine Mercier committed
39
        value = obi_column_get_obifloat_with_elt_name_in_view(self.view, (self.pointer)[0], line_nb, str2bytes(element_name))
40 41 42 43 44
        if obi_errno > 0 :
            raise IndexError(line_nb, element_name)
        if value == OBIFloat_NA :
            result = None
        else :
45
            result = <double> value
46
        return result
Celine Mercier's avatar
Celine Mercier committed
47

48
    cpdef object get_line(self, index_t line_nb) :
49
        cdef obifloat_t value
Celine Mercier's avatar
Celine Mercier committed
50 51
        cdef object value_in_result
        cdef dict result
52
        cdef index_t i
53 54 55 56
        cdef bint all_NA
        result = {}
        all_NA = True
        for i in range(self.nb_elements_per_line) :
Celine Mercier's avatar
Celine Mercier committed
57
            value = obi_column_get_obifloat_with_elt_idx_in_view(self.view, (self.pointer)[0], line_nb, i)
58
            if obi_errno > 0 :
Celine Mercier's avatar
Celine Mercier committed
59 60 61 62 63 64 65
                raise IndexError(line_nb)
            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) :
66 67 68 69
                all_NA = False
        if all_NA :
            result = None
        return result
Celine Mercier's avatar
Celine Mercier committed
70 71 72 73 74
   
    cpdef set_item(self, index_t line_nb, str element_name, object value):
        if value is None :
            value = OBIFloat_NA
        if obi_column_set_obifloat_with_elt_name_in_view(self.view, (self.pointer)[0], line_nb, str2bytes(element_name), <obifloat_t> value) < 0:
75 76
            raise Exception("Problem setting a value in a column")