slightly more picky in what exceptions to catch, now complain when losing all labels
This commit is contained in:
parent
81a95b5158
commit
273874d3fb
|
@ -7,7 +7,7 @@ import numpy as np
|
||||||
# python 3 has no unicode object, this ensures that the code works on Python 2&3
|
# python 3 has no unicode object, this ensures that the code works on Python 2&3
|
||||||
try:
|
try:
|
||||||
test = isinstance('test', unicode)
|
test = isinstance('test', unicode)
|
||||||
except(NameError):
|
except NameError:
|
||||||
unicode = str
|
unicode = str
|
||||||
|
|
||||||
# ------------------------------------------------------------------
|
# ------------------------------------------------------------------
|
||||||
|
@ -63,16 +63,17 @@ class ASCIItable():
|
||||||
x):
|
x):
|
||||||
try:
|
try:
|
||||||
return float(x)
|
return float(x)
|
||||||
except:
|
except ValueError:
|
||||||
return 0.0
|
return 0.0
|
||||||
|
|
||||||
# ------------------------------------------------------------------
|
# ------------------------------------------------------------------
|
||||||
def _removeCRLF(self,
|
def _removeCRLF(self,
|
||||||
string):
|
string):
|
||||||
|
"""delete any carriage return and line feed from string"""
|
||||||
try:
|
try:
|
||||||
return string.replace('\n','').replace('\r','')
|
return string.replace('\n','').replace('\r','')
|
||||||
except:
|
except AttributeError:
|
||||||
return string
|
return str(string)
|
||||||
|
|
||||||
|
|
||||||
# ------------------------------------------------------------------
|
# ------------------------------------------------------------------
|
||||||
|
@ -93,22 +94,22 @@ class ASCIItable():
|
||||||
|
|
||||||
# ------------------------------------------------------------------
|
# ------------------------------------------------------------------
|
||||||
def input_close(self):
|
def input_close(self):
|
||||||
try:
|
# try:
|
||||||
if self.__IO__['in'] != sys.stdin: self.__IO__['in'].close()
|
if self.__IO__['in'] != sys.stdin: self.__IO__['in'].close()
|
||||||
except:
|
# except:
|
||||||
pass
|
# pass
|
||||||
|
|
||||||
# ------------------------------------------------------------------
|
# ------------------------------------------------------------------
|
||||||
def output_write(self,
|
def output_write(self,
|
||||||
what):
|
what):
|
||||||
"""Aggregate a single row (string) or list of (possibly containing further lists of) rows into output"""
|
"""Aggregate a single row (string) or list of (possibly containing further lists of) rows into output"""
|
||||||
if not isinstance(what, (str, unicode)):
|
if isinstance(what, (str, unicode)):
|
||||||
|
self.__IO__['output'] += [what]
|
||||||
|
else:
|
||||||
try:
|
try:
|
||||||
for item in what: self.output_write(item)
|
for item in what: self.output_write(item)
|
||||||
except:
|
except TypeError:
|
||||||
self.__IO__['output'] += [str(what)]
|
self.__IO__['output'] += [str(what)]
|
||||||
else:
|
|
||||||
self.__IO__['output'] += [what]
|
|
||||||
|
|
||||||
return self.__IO__['buffered'] or self.output_flush()
|
return self.__IO__['buffered'] or self.output_flush()
|
||||||
|
|
||||||
|
@ -129,10 +130,10 @@ class ASCIItable():
|
||||||
# ------------------------------------------------------------------
|
# ------------------------------------------------------------------
|
||||||
def output_close(self,
|
def output_close(self,
|
||||||
dismiss = False):
|
dismiss = False):
|
||||||
try:
|
# try:
|
||||||
if self.__IO__['out'] != sys.stdout: self.__IO__['out'].close()
|
if self.__IO__['out'] != sys.stdout: self.__IO__['out'].close()
|
||||||
except:
|
# except:
|
||||||
pass
|
# pass
|
||||||
if dismiss and os.path.isfile(self.__IO__['out'].name):
|
if dismiss and os.path.isfile(self.__IO__['out'].name):
|
||||||
os.remove(self.__IO__['out'].name)
|
os.remove(self.__IO__['out'].name)
|
||||||
elif self.__IO__['inPlace']:
|
elif self.__IO__['inPlace']:
|
||||||
|
@ -150,7 +151,7 @@ class ASCIItable():
|
||||||
|
|
||||||
try:
|
try:
|
||||||
self.__IO__['in'].seek(0)
|
self.__IO__['in'].seek(0)
|
||||||
except:
|
except IOError:
|
||||||
pass
|
pass
|
||||||
|
|
||||||
firstline = self.__IO__['in'].readline().strip()
|
firstline = self.__IO__['in'].readline().strip()
|
||||||
|
@ -170,7 +171,7 @@ class ASCIItable():
|
||||||
else: # other table format
|
else: # other table format
|
||||||
try:
|
try:
|
||||||
self.__IO__['in'].seek(0) # try to rewind
|
self.__IO__['in'].seek(0) # try to rewind
|
||||||
except:
|
except IOError:
|
||||||
self.__IO__['readBuffer'] = [firstline] # or at least save data in buffer
|
self.__IO__['readBuffer'] = [firstline] # or at least save data in buffer
|
||||||
|
|
||||||
while self.data_read(advance = False, respectLabels = False):
|
while self.data_read(advance = False, respectLabels = False):
|
||||||
|
@ -197,7 +198,9 @@ class ASCIItable():
|
||||||
"""Write current header information (info + labels)"""
|
"""Write current header information (info + labels)"""
|
||||||
head = ['{}\theader'.format(len(self.info)+self.__IO__['labeled'])] if header else []
|
head = ['{}\theader'.format(len(self.info)+self.__IO__['labeled'])] if header else []
|
||||||
head.append(self.info)
|
head.append(self.info)
|
||||||
if self.__IO__['labeled']: head.append('\t'.join(map(self._quote,self.tags)))
|
if self.__IO__['labeled']:
|
||||||
|
head.append('\t'.join(map(self._quote,self.tags)))
|
||||||
|
if len(self.tags) == 0: raise ValueError('no labels present.')
|
||||||
|
|
||||||
return self.output_write(head)
|
return self.output_write(head)
|
||||||
|
|
||||||
|
@ -257,13 +260,13 @@ class ASCIItable():
|
||||||
what,
|
what,
|
||||||
reset = False):
|
reset = False):
|
||||||
"""Add item or list to existing set of labels (and switch on labeling)"""
|
"""Add item or list to existing set of labels (and switch on labeling)"""
|
||||||
if not isinstance(what, (str, unicode)):
|
if isinstance(what, (str, unicode)):
|
||||||
|
self.tags += [self._removeCRLF(what)]
|
||||||
|
else:
|
||||||
try:
|
try:
|
||||||
for item in what: self.labels_append(item)
|
for item in what: self.labels_append(item)
|
||||||
except:
|
except TypeError:
|
||||||
self.tags += [self._removeCRLF(str(what))]
|
self.tags += [self._removeCRLF(str(what))]
|
||||||
else:
|
|
||||||
self.tags += [self._removeCRLF(what)]
|
|
||||||
|
|
||||||
self.__IO__['labeled'] = True # switch on processing (in particular writing) of tags
|
self.__IO__['labeled'] = True # switch on processing (in particular writing) of tags
|
||||||
if reset: self.__IO__['tags'] = list(self.tags) # subsequent data_read uses current tags as data size
|
if reset: self.__IO__['tags'] = list(self.tags) # subsequent data_read uses current tags as data size
|
||||||
|
@ -410,13 +413,13 @@ class ASCIItable():
|
||||||
def info_append(self,
|
def info_append(self,
|
||||||
what):
|
what):
|
||||||
"""Add item or list to existing set of infos"""
|
"""Add item or list to existing set of infos"""
|
||||||
if not isinstance(what, (str, unicode)):
|
if isinstance(what, (str, unicode)):
|
||||||
|
self.info += [self._removeCRLF(what)]
|
||||||
|
else:
|
||||||
try:
|
try:
|
||||||
for item in what: self.info_append(item)
|
for item in what: self.info_append(item)
|
||||||
except:
|
except TypeError:
|
||||||
self.info += [self._removeCRLF(str(what))]
|
self.info += [self._removeCRLF(str(what))]
|
||||||
else:
|
|
||||||
self.info += [self._removeCRLF(what)]
|
|
||||||
|
|
||||||
# ------------------------------------------------------------------
|
# ------------------------------------------------------------------
|
||||||
def info_clear(self):
|
def info_clear(self):
|
||||||
|
@ -468,10 +471,8 @@ class ASCIItable():
|
||||||
"""Read whole data of all (given) labels as numpy array"""
|
"""Read whole data of all (given) labels as numpy array"""
|
||||||
from collections import Iterable
|
from collections import Iterable
|
||||||
|
|
||||||
try:
|
try: self.data_rewind() # try to wind back to start of data
|
||||||
self.data_rewind() # try to wind back to start of data
|
except: pass # assume/hope we are at data start already...
|
||||||
except:
|
|
||||||
pass # assume/hope we are at data start already...
|
|
||||||
|
|
||||||
if labels is None or labels == []:
|
if labels is None or labels == []:
|
||||||
use = None # use all columns (and keep labels intact)
|
use = None # use all columns (and keep labels intact)
|
||||||
|
@ -530,13 +531,13 @@ class ASCIItable():
|
||||||
# ------------------------------------------------------------------
|
# ------------------------------------------------------------------
|
||||||
def data_append(self,
|
def data_append(self,
|
||||||
what):
|
what):
|
||||||
if not isinstance(what, (str, unicode)):
|
if isinstance(what, (str, unicode)):
|
||||||
|
self.data += [what]
|
||||||
|
else:
|
||||||
try:
|
try:
|
||||||
for item in what: self.data_append(item)
|
for item in what: self.data_append(item)
|
||||||
except:
|
except TypeError:
|
||||||
self.data += [str(what)]
|
self.data += [str(what)]
|
||||||
else:
|
|
||||||
self.data += [what]
|
|
||||||
|
|
||||||
# ------------------------------------------------------------------
|
# ------------------------------------------------------------------
|
||||||
def data_set(self,
|
def data_set(self,
|
||||||
|
|
Loading…
Reference in New Issue