added method to return data as list of floats.
safer check whether ASCII file has ended.
This commit is contained in:
parent
bffc22fbe1
commit
a3e47b2d65
|
@ -11,6 +11,7 @@ class ASCIItable():
|
||||||
'data',
|
'data',
|
||||||
]
|
]
|
||||||
|
|
||||||
|
# ------------------------------------------------------------------
|
||||||
def __init__(self,
|
def __init__(self,
|
||||||
fileIn = sys.stdin,
|
fileIn = sys.stdin,
|
||||||
fileOut = sys.stdout,
|
fileOut = sys.stdout,
|
||||||
|
@ -27,6 +28,15 @@ class ASCIItable():
|
||||||
self.labels = []
|
self.labels = []
|
||||||
self.data = []
|
self.data = []
|
||||||
|
|
||||||
|
|
||||||
|
# ------------------------------------------------------------------
|
||||||
|
def _transliterateToFloat(self,x):
|
||||||
|
try:
|
||||||
|
return float(x)
|
||||||
|
except:
|
||||||
|
return 0.0
|
||||||
|
|
||||||
|
# ------------------------------------------------------------------
|
||||||
def output_write(self,
|
def output_write(self,
|
||||||
what):
|
what):
|
||||||
if isinstance(what,list):
|
if isinstance(what,list):
|
||||||
|
@ -35,14 +45,17 @@ class ASCIItable():
|
||||||
self.__IO__['output'] += [str(what)]
|
self.__IO__['output'] += [str(what)]
|
||||||
self.__IO__['buffered'] or self.output_flush()
|
self.__IO__['buffered'] or self.output_flush()
|
||||||
|
|
||||||
|
# ------------------------------------------------------------------
|
||||||
def output_flush(self,
|
def output_flush(self,
|
||||||
clear = True):
|
clear = True):
|
||||||
self.__IO__['output'] == [] or self.__IO__['out'].write('\n'.join(self.__IO__['output']) + '\n')
|
self.__IO__['output'] == [] or self.__IO__['out'].write('\n'.join(self.__IO__['output']) + '\n')
|
||||||
if clear: self.output_clear()
|
if clear: self.output_clear()
|
||||||
|
|
||||||
|
# ------------------------------------------------------------------
|
||||||
def output_clear(self):
|
def output_clear(self):
|
||||||
self.__IO__['output'] = []
|
self.__IO__['output'] = []
|
||||||
|
|
||||||
|
# ------------------------------------------------------------------
|
||||||
def head_read(self):
|
def head_read(self):
|
||||||
'''
|
'''
|
||||||
get column labels by either read the first row, or
|
get column labels by either read the first row, or
|
||||||
|
@ -66,41 +79,53 @@ class ASCIItable():
|
||||||
self.__IO__['validReadSize'] = len(self.labels)
|
self.__IO__['validReadSize'] = len(self.labels)
|
||||||
self.__IO__['dataStart'] = self.__IO__['in'].tell()
|
self.__IO__['dataStart'] = self.__IO__['in'].tell()
|
||||||
|
|
||||||
|
# ------------------------------------------------------------------
|
||||||
def head_write(self):
|
def head_write(self):
|
||||||
self.output_write (['%i\theader'%(len(self.info)+1),
|
self.output_write (['%i\theader'%(len(self.info)+1),
|
||||||
self.info,
|
self.info,
|
||||||
'\t'.join(self.labels)])
|
'\t'.join(self.labels)])
|
||||||
|
|
||||||
|
# ------------------------------------------------------------------
|
||||||
def labels_append(self,
|
def labels_append(self,
|
||||||
what):
|
what):
|
||||||
if isinstance(what,list):
|
if isinstance(what,list):
|
||||||
for item in what: self.labels_append(item)
|
for item in what: self.labels_append(item)
|
||||||
else: self.labels += [str(what)]
|
else: self.labels += [str(what)]
|
||||||
|
|
||||||
|
# ------------------------------------------------------------------
|
||||||
def info_append(self,
|
def info_append(self,
|
||||||
what):
|
what):
|
||||||
if isinstance(what,list):
|
if isinstance(what,list):
|
||||||
for item in what: self.info_append(item)
|
for item in what: self.info_append(item)
|
||||||
else: self.info += [str(what)]
|
else: self.info += [str(what)]
|
||||||
|
|
||||||
|
# ------------------------------------------------------------------
|
||||||
def data_rewind(self):
|
def data_rewind(self):
|
||||||
self.__IO__['in'].seek(self.__IO__['dataStart'])
|
self.__IO__['in'].seek(self.__IO__['dataStart'])
|
||||||
|
|
||||||
|
# ------------------------------------------------------------------
|
||||||
def data_read(self):
|
def data_read(self):
|
||||||
line = self.__IO__['in'].readline()
|
line = self.__IO__['in'].readline()
|
||||||
items = line.split()[:self.__IO__['validReadSize']] # get next data row
|
items = line.split()[:self.__IO__['validReadSize']] # get next data row
|
||||||
self.data = {False: [],
|
self.data = {False: [],
|
||||||
True: items}[len(items) == self.__IO__['validReadSize']] # take if correct number of entries
|
True: items}[len(items) == self.__IO__['validReadSize']] # take if correct number of entries
|
||||||
return line != ''
|
return self.data != []
|
||||||
|
|
||||||
|
# ------------------------------------------------------------------
|
||||||
def data_write(self):
|
def data_write(self):
|
||||||
if isinstance(self.data[0],list):
|
if isinstance(self.data[0],list):
|
||||||
self.output_write (['\t'.join(map(str,items)) for items in self.data])
|
self.output_write (['\t'.join(map(str,items)) for items in self.data])
|
||||||
else:
|
else:
|
||||||
self.output_write ('\t'.join(map(str,self.data)))
|
self.output_write ('\t'.join(map(str,self.data)))
|
||||||
|
|
||||||
|
# ------------------------------------------------------------------
|
||||||
def data_append(self,
|
def data_append(self,
|
||||||
what):
|
what):
|
||||||
if isinstance(what,list):
|
if isinstance(what,list):
|
||||||
for item in what: self.data_append(item)
|
for item in what: self.data_append(item)
|
||||||
else: self.data += [str(what)]
|
else: self.data += [str(what)]
|
||||||
|
|
||||||
|
# ------------------------------------------------------------------
|
||||||
|
def data_asFloat(self):
|
||||||
|
return map(self._transliterateToFloat,self.data)
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue