Refactoring ...
This commit is contained in:
180
python/obitools3/dms/column/column.pyx
Normal file
180
python/obitools3/dms/column/column.pyx
Normal file
@ -0,0 +1,180 @@
|
||||
#cython: language_level=3
|
||||
|
||||
from .capi.obitypes cimport name_data_type
|
||||
|
||||
from .capi.obidmscolumn cimport OBIDMS_column_header_p, \
|
||||
obi_close_column, \
|
||||
obi_column_prepare_to_get_value
|
||||
|
||||
from .capi.obiutils cimport obi_format_date
|
||||
|
||||
from .dms cimport __OBIDMS_COLUMN_CLASS__
|
||||
|
||||
from obitools3.utils cimport bytes2str
|
||||
|
||||
|
||||
cdef class Column :
|
||||
"""
|
||||
The obitools3.dms.column.Column class wraps a c instance of a column in the context of a View
|
||||
"""
|
||||
|
||||
# Note: should only be initialized through a subclass
|
||||
def __init__(self,
|
||||
View view,
|
||||
int __internalCall__):
|
||||
'''
|
||||
Create a new OBDMS column objected referring to a already created column
|
||||
in the context of a view.
|
||||
|
||||
This constructor is normally only called by subclass constructor.
|
||||
|
||||
@param view: The view object containing the column.
|
||||
@type view: OBIView
|
||||
'''
|
||||
|
||||
cdef OBIDMS_column_p* column_pp
|
||||
|
||||
if __internalCall__!=987654:
|
||||
raise RuntimeError('OBIView constructor cannot be called directly')
|
||||
|
||||
# Check that the class is only created as a subclass instance
|
||||
if type(self)==Column or not isinstance(self, Column):
|
||||
raise RuntimeError('OBIDMS.Column constructor cannot be called directly')
|
||||
|
||||
# Fill structure
|
||||
self._pointer = NULL
|
||||
self._view = view
|
||||
|
||||
def __len__(self):
|
||||
'''
|
||||
implements the len() function for the Column class
|
||||
|
||||
@rtype: `int`
|
||||
'''
|
||||
return self.lines_used
|
||||
|
||||
def __sizeof__(self):
|
||||
'''
|
||||
returns the size of the C object wrapped by the Column instance
|
||||
'''
|
||||
cdef OBIDMS_column_header_p header = self._pointer[0].header
|
||||
return header.header_size + header.data_size
|
||||
|
||||
def __iter__(self):
|
||||
cdef index_t line_nb
|
||||
|
||||
for line_nb in range(self.lines_used):
|
||||
yield self[line_nb]
|
||||
|
||||
def __str__(self) :
|
||||
cdef str to_print
|
||||
to_print = ''
|
||||
for line in self :
|
||||
to_print = to_print + str(line) + "\n"
|
||||
return to_print
|
||||
|
||||
def __repr__(self) :
|
||||
return b"%s, original name: %s, version %d, data type: %d" % (
|
||||
self._alias,
|
||||
self.original_name,
|
||||
self.version,
|
||||
self.data_type
|
||||
)
|
||||
|
||||
cpdef close(self):
|
||||
if self._pointer != NULL:
|
||||
if obi_close_column(self._pointer[0]) < 0 :
|
||||
raise Exception("Problem closing column %s" % bytes2str(self.name))
|
||||
|
||||
# Column alias property getter and setter
|
||||
@property
|
||||
def name(self):
|
||||
return self._alias
|
||||
@name.setter
|
||||
def name(self, new_alias): # @DuplicatedSignature
|
||||
self._view.change_column_alias(self._alias, new_alias)
|
||||
|
||||
# elements_names property getter
|
||||
@property
|
||||
def elements_names(self):
|
||||
return (((self._pointer)[0].header).elements_names).split(b';')
|
||||
|
||||
# nb_elements_per_line property getter
|
||||
@property
|
||||
def nb_elements_per_line(self):
|
||||
return ((self._pointer)[0].header).nb_elements_per_line
|
||||
|
||||
# data_type property getter
|
||||
@property
|
||||
def data_type(self):
|
||||
return name_data_type(((self._pointer)[0].header).returned_data_type)
|
||||
|
||||
# original_name property getter
|
||||
@property
|
||||
def original_name(self):
|
||||
return ((self._pointer)[0].header).name
|
||||
|
||||
# version property getter
|
||||
@property
|
||||
def version(self):
|
||||
return ((self._pointer)[0].header).version
|
||||
|
||||
# lines_used property getter
|
||||
@property
|
||||
def lines_used(self):
|
||||
return (self._pointer)[0].header.lines_used
|
||||
|
||||
# comments property getter
|
||||
@property
|
||||
def comments(self):
|
||||
return (self._pointer)[0].header.comments
|
||||
|
||||
# creation_date property getter
|
||||
@property
|
||||
def creation_date(self):
|
||||
return obi_format_date((self._pointer)[0].header.creation_date)
|
||||
|
||||
|
||||
######################################################################################################
|
||||
|
||||
|
||||
cdef class Column_line :
|
||||
|
||||
def __init__(self, Column column, index_t line_nb) :
|
||||
self._index = line_nb
|
||||
self._column = column
|
||||
|
||||
if obi_column_prepare_to_get_value(self._column._pointer[0],line_nb) < 0:
|
||||
raise IndexError("Cannot access to the line %d" % line_nb)
|
||||
|
||||
|
||||
def __contains__(self, str element_name):
|
||||
pass
|
||||
#return (element_name in self._column.elements_names)
|
||||
|
||||
def __repr__(self) :
|
||||
return str(self._column.get_line(self._index))
|
||||
|
||||
cpdef update(self, data):
|
||||
if isinstance(data, dict):
|
||||
data=data.items()
|
||||
for key,value in data:
|
||||
if key in self:
|
||||
self[key]=value
|
||||
|
||||
|
||||
######################################################################################################
|
||||
|
||||
|
||||
cdef register_column_class(obitype_t obitype,
|
||||
type classe,
|
||||
type python):
|
||||
"""
|
||||
Each sub class of `OBIDMS_column` needs to be registered after its declaration
|
||||
to declare its relationship with an `OBIType_t`
|
||||
"""
|
||||
global __OBIDMS_COLUMN_CLASS__
|
||||
|
||||
assert issubclass(classe,Column)
|
||||
|
||||
__OBIDMS_COLUMN_CLASS__[obitype]=(classe,python)
|
Reference in New Issue
Block a user