From bc12360490ddc75d35018ea5c6a0e375df6dce58 Mon Sep 17 00:00:00 2001 From: Celine Mercier Date: Mon, 2 May 2016 15:16:06 +0200 Subject: [PATCH] Reworked and commented a bit the cython layer for dms, columns and views --- python/obitools3/obidms/_obidms.pyx | 99 ++++++++++++++--------------- 1 file changed, 46 insertions(+), 53 deletions(-) diff --git a/python/obitools3/obidms/_obidms.pyx b/python/obitools3/obidms/_obidms.pyx index 5f6e2ef..b2d2eba 100644 --- a/python/obitools3/obidms/_obidms.pyx +++ b/python/obitools3/obidms/_obidms.pyx @@ -90,7 +90,7 @@ cdef class OBIDMS_column : # Fill structure self.pointer = column_pp self.dms = view.dms - self.view = view.pointer # TODO pointer or instance? + self.view = view.pointer self.data_type = bytes2str(name_data_type((column_p.header).returned_data_type)) self.column_name = bytes2str((column_p.header).name) self.nb_elements_per_line = (column_p.header).nb_elements_per_line @@ -137,12 +137,15 @@ cdef class OBIDMS_column : cpdef str get_comments(self): return bytes2str((self.pointer)[0].header.comments) - def __repr__(self) : + 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 (self.column_name + ", version " + str((self.pointer)[0].header.version) + ", data type: " + self.data_type) cpdef close(self): if obi_close_column((self.pointer)[0]) < 0 : @@ -248,14 +251,16 @@ cdef class OBIView : self.dms = dms + # Create the C array for the line selection if needed if line_selection is not None : line_selection_p = malloc((len(line_selection) + 1) * sizeof(index_t)) for i in range(len(line_selection)) : - line_selection_p[i] = line_selection[i] # TODO type problem? + line_selection_p[i] = line_selection[i] line_selection_p[len(line_selection)] = -1 else : line_selection_p = NULL + # Create the view if needed if new : if view_to_clone is not None : if type(view_to_clone) == str : @@ -264,31 +269,27 @@ cdef class OBIView : view = obi_new_view(dms.pointer, str2bytes(view_name), ( view_to_clone).pointer, line_selection_p, str2bytes(comments)) elif view_to_clone is None : view = obi_new_view(dms.pointer, str2bytes(view_name), NULL, line_selection_p, str2bytes(comments)) + # Else, open the existing view elif not new : if view_name is not None : view = obi_open_view(dms.pointer, str2bytes(view_name)) elif view_name is None : - view = obi_open_view(dms.pointer, NULL) + view = obi_open_view(dms.pointer, NULL) # TODO discuss if view == NULL : - raise Exception("Error creating/opening view") + raise Exception("Error creating/opening a view") self.pointer = view self.name = bytes2str(view.name) - # go through columns to build list and open python object (TODO make separate function?) + # Go through columns to build list of corresponding python instances self.columns = {} - - i = 0 - while i < view.column_count : - column_p = (view.columns)[i] - header = (column_p).header - - col_name = bytes2str(header.name) - subclass = OBIDMS_column.get_subclass_type(column_p) + for i in range(view.column_count) : + column_p = (view.columns)[i] + header = (column_p).header + col_name = bytes2str(header.name) + subclass = OBIDMS_column.get_subclass_type(column_p) self.columns[col_name] = subclass(self, col_name) - - i+=1 def __repr__(self) : @@ -298,11 +299,9 @@ cdef class OBIView : cdef OBIDMS_column_p column_p s = self.name - s = s + ", " + self.comments + ", " + str(self.pointer.line_count) + " lines" - for column_name in self.columns : # TODO make function in OBIDMS_column class - column = self.columns[column_name] - column_p = (column.pointer)[0] - s = s + "\n" + column_name + ", version " + str(column_p.header.version) + ", data type: " + column.data_type + s = s + ", " + self.comments + ", " + str(self.pointer.line_count) + " lines\n" + for column_name in self.columns : + s = s + self.columns[column_name].__repr__() + '\n' return s @@ -332,11 +331,11 @@ cdef class OBIView : obiversion_t version_number=-1, str type='', index_t nb_lines=0, - index_t nb_elements_per_line=1, # TODO 1? + index_t nb_elements_per_line=1, list elements_names=None, str indexer_name="", str comments="", - bint create=True # TODO + bint create=True ) : cdef bytes column_name_b @@ -368,7 +367,7 @@ cdef class OBIView : else : raise Exception("Invalid provided data type") - if (obi_view_add_column(self.pointer, column_name_b, version_number, # should return pointer on column? + if (obi_view_add_column(self.pointer, column_name_b, version_number, # TODO should return pointer on column? data_type, nb_lines, nb_elements_per_line, elements_names_b, str2bytes(indexer_name), str2bytes(comments), create) < 0) : @@ -392,11 +391,11 @@ cdef class OBIView : # iter on each line of all columns # Declarations - cdef index_t lines_used - cdef index_t line_nb - cdef OBIView_line line # TODO for NUC SEQS View + cdef index_t lines_used + cdef index_t line_nb + cdef OBIView_line line # TODO Check that this works for NUC SEQ views - # Yield each line TODO line class + # Yield each line lines_used = (self.pointer).line_count for line_nb in range(lines_used) : @@ -407,7 +406,7 @@ cdef class OBIView : def __getitem__(self, object item) : if type(item) == str : return (self.columns)[item] - elif type(item) == int : # TODO int? + elif type(item) == int : return OBIView_line(self, item) @@ -420,7 +419,7 @@ cdef class OBIView : cdef index_t* line_selection_p line_selection_p = malloc((len(line_selection) + 1) * sizeof(index_t)) for i in range(len(line_selection)) : - line_selection_p[i] = line_selection[i] # TODO type problem? + line_selection_p[i] = line_selection[i] line_selection_p[len(line_selection)] = -1 if obi_select_lines(self.pointer, line_selection_p) < 0 : raise Exception("Problem selecting a list of lines") @@ -444,21 +443,21 @@ cdef class OBIView_NUC_SEQS(OBIView): def __init__(self, OBIDMS dms, str view_name, bint new=False, object view_to_clone=None, list line_selection=None, str comments=""): - cdef Obiview_p view = NULL - cdef int i - cdef list col_list - cdef str col_name - cdef OBIDMS_column column - cdef OBIDMS_column_p column_p + cdef Obiview_p view = NULL + cdef int i + cdef list col_list + cdef str col_name + cdef OBIDMS_column column + cdef OBIDMS_column_p column_p cdef OBIDMS_column_header_p header - cdef index_t* line_selection_p + cdef index_t* line_selection_p self.dms = dms if line_selection is not None : line_selection_p = malloc((len(line_selection) + 1) * sizeof(index_t)) for i in range(len(line_selection)) : - line_selection_p[i] = line_selection[i] # TODO type problem? + line_selection_p[i] = line_selection[i] line_selection_p[len(line_selection)] = -1 else : line_selection_p = NULL @@ -484,20 +483,14 @@ cdef class OBIView_NUC_SEQS(OBIView): self.name = bytes2str(view.name) self.comments = bytes2str(view.comments) - # go through columns to build list and open python object (TODO make separate function?) + # Go through columns to build list of corresponding python instances self.columns = {} - - i = 0 - while i < view.column_count : - column_p = (view.columns)[i] - header = (column_p).header - - col_name = bytes2str(header.name) - - subclass = OBIDMS_column.get_subclass_type(column_p) + for i in range(view.column_count) : + column_p = (view.columns)[i] + header = (column_p).header + col_name = bytes2str(header.name) + subclass = OBIDMS_column.get_subclass_type(column_p) self.columns[col_name] = subclass(self, col_name) - - i+=1 self.ids = self.columns[bytes2str(ID_COLUMN)] self.sequences = self.columns[bytes2str(NUC_SEQUENCE_COLUMN)] @@ -521,7 +514,7 @@ cdef class OBIView_NUC_SEQS(OBIView): if obi_view_delete_column(view, str2bytes(column_name)) < 0 : raise Exception("Problem deleting a column from a view") - # Update the dictionaries of column pointers and column objects, and update pointers in column objects (make function?): + # Remove instance from the dictionary (self.columns).pop(column_name) for column_n in self.columns : @@ -531,7 +524,7 @@ cdef class OBIView_NUC_SEQS(OBIView): def __getitem__(self, object item) : if type(item) == str : return (self.columns)[item] - elif type(item) == int : # TODO int? (range problem) + elif type(item) == int : return OBI_Nuc_Seq_Stored(self, item) @@ -650,7 +643,7 @@ cdef class OBIDMS : return all_views[view_name] - cpdef dict read_views(self) : # TODO function that prints the dic nicely and function that prints 1 view. Add column type in col ref + cpdef dict read_views(self) : # TODO function that prints the dic nicely and function that prints 1 view nicely. Add column type in col ref cdef Obiviews_infos_all_p all_views_p cdef Obiview_infos_p view_p