Cython: Added Column line methods to get a Column line as a str or

bytes, and elements (keys, values) with None values are not returned
anymore
This commit is contained in:
Celine Mercier
2018-10-17 11:23:07 +02:00
parent 135d3b6e67
commit 8a9ba8b0a8
2 changed files with 45 additions and 5 deletions

View File

@ -43,7 +43,11 @@ cdef class Column_line:
cdef Column _column cdef Column _column
cdef index_t _index cdef index_t _index
cpdef list keys(self)
cpdef list items(self)
cpdef dict dict(self)
cpdef bytes bytes(self)
cpdef update(self, object data) cpdef update(self, object data)

View File

@ -543,15 +543,51 @@ cdef class Column_line :
def __iter__(self) : def __iter__(self) :
cdef list elements_names cdef list elements_names
cdef bytes element_name cdef bytes element_name
elements_names = self._column._elements_names for element_name in self._column.get_line(self._index) :
for element_name in elements_names :
yield element_name yield element_name
def keys(self): # TODO Discuss if keys with None values should be returned too cpdef list keys(self):
return self._column._elements_names # TODO just returning all element names of the column would be faster but
# potentially lots of keys associated with None values on that given line.
# get_line() methods don't return None values
return [element_name for element_name in self._column.get_line(self._index)]
cpdef list items(self):
return [(k, self[k]) for k in self.keys()]
cpdef dict dict(self):
cdef dict d
d = {}
for k,v in self.items():
d[k] = v
return d
def __str__(self):
cdef dict d
cdef str s
d = {}
for k,v in self.items():
if type(v) == bytes:
value = bytes2str(v)
else:
value = v
d[bytes2str(k)] = value
s = str(d)
return s
cpdef bytes bytes(self):
cdef str s
cdef bytes b
s = self.__str__()
b = str2bytes(s)
return b
# column property getter # column property getter
@property @property
def column(self): def column(self):