Commit e39c1a7f by Celine Mercier

Cython: added tab formatter and parser (for obi export)

parent 6841d879
#cython: language_level=3
cdef class TabFormat:
cdef bint header
cdef bint first_line
cdef bytes NAString
cdef list tags
cdef bytes sep
\ No newline at end of file
#cython: language_level=3
cimport cython
from obitools3.dms.view.view cimport Line
from obitools3.utils cimport bytes2str_object, str2bytes, tobytes
from obitools3.dms.column.column cimport Column_line
cdef class TabFormat:
def __init__(self, header=True, bytes NAString=b"NA", bytes sep=b"\t"):
self.header = True
self.first_line = True
self.NAString = NAString
self.sep = sep
def __call__(self, object data):
line = []
if self.first_line:
self.tags = [k for k in data.keys()]
for k in self.tags:
if self.header and self.first_line:
value = tobytes(k)
value = data[k]
if value is not None:
if type(value) == Column_line:
value = value.bytes()
value = str2bytes(str(bytes2str_object(value))) # genius programming
if value is None:
value = self.NAString
if self.first_line:
self.first_line = False
return self.sep.join(value for value in line)
#cython: language_level=3
cdef class TabWriter:
cdef object formatter
cdef object output
cdef int only
cdef int skip
cdef int skipped
cdef int read
\ No newline at end of file
#cython: language_level=3
Created on oct 12th 2018
cdef class TabWriter:
def __init__(self,
object formatter,
object output_object,
int skip=0,
if only is None:
self.only = -1
self.only = int(only)
if header:
self.only += 1
self.formatter = formatter
self.output = output_object
self.skip = skip
if header:
self.skip -= 1
self.skipped = 0 = 0
def __call__(self, object seq):
if self.only > -1 and == self.only:
raise StopIteration
if self.skip > 0 and self.skipped < self.skip:
self.skipped += 1
self.output.write(b"\n") # TODO is that clean? += 1
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment