improved stability of xxx_append commands when dealing with iterable content

This commit is contained in:
Philip Eisenlohr 2013-12-12 02:36:05 +00:00
parent 10c8a1a3ce
commit 0e84db8f19
1 changed files with 34 additions and 15 deletions

View File

@ -40,11 +40,18 @@ class ASCIItable():
# ------------------------------------------------------------------ # ------------------------------------------------------------------
def output_write(self, def output_write(self,
what): what):
if isinstance(what,list): '''
for item in what: self.output_write(item) aggregate a single row (string) or list of (possibly containing further lists of) rows into output
'''
if not isinstance(what, (str, unicode)):
try:
for item in what: self.output_write(item)
except:
self.__IO__['output'] += [str(what)]
else: else:
self.__IO__['output'] += [str(what)] self.__IO__['output'] += [what]
return self.__IO__['buffered'] or self.output_flush()
return self.__IO__['buffered'] or self.output_flush()
# ------------------------------------------------------------------ # ------------------------------------------------------------------
def output_flush(self, def output_flush(self,
@ -118,9 +125,13 @@ class ASCIItable():
''' '''
add item or list to existing set of labels add item or list to existing set of labels
''' '''
if isinstance(what,list): if not isinstance(what, (str, unicode)):
for item in what: self.labels_append(item) try:
else: self.labels += [str(what)] for item in what: self.labels_append(item)
except:
self.labels += [str(what)]
else:
self.labels += [what]
self.__IO__['labels'] = True # switch on processing (in particular writing) of labels self.__IO__['labels'] = True # switch on processing (in particular writing) of labels
@ -161,9 +172,13 @@ class ASCIItable():
''' '''
add item or list to existing set of infos add item or list to existing set of infos
''' '''
if isinstance(what,list): if not isinstance(what, (str, unicode)):
for item in what: self.info_append(item) try:
else: self.info += [str(what)] for item in what: self.info_append(item)
except:
self.info += [str(what)]
else:
self.info += [what]
# ------------------------------------------------------------------ # ------------------------------------------------------------------
def info_clear(self): def info_clear(self):
@ -200,10 +215,11 @@ class ASCIItable():
# ------------------------------------------------------------------ # ------------------------------------------------------------------
def data_write(self): def data_write(self):
if len(self.data) == 0: return if len(self.data) == 0: return
if isinstance(self.data[0],list): if isinstance(self.data[0],list):
return self.output_write (['\t'.join(map(str,items)) for items in self.data]) return self.output_write(['\t'.join(map(str,items)) for items in self.data])
else: else:
return self.output_write ('\t'.join(map(str,self.data))) return self.output_write('\t'.join(map(str,self.data)))
# ------------------------------------------------------------------ # ------------------------------------------------------------------
def data_writeArray(self,format='%g'): def data_writeArray(self,format='%g'):
@ -216,10 +232,13 @@ class ASCIItable():
# ------------------------------------------------------------------ # ------------------------------------------------------------------
def data_append(self, def data_append(self,
what): what):
if isinstance(what,list): if not isinstance(what, (str, unicode)):
for item in what: self.data_append(item) try:
for item in what: self.data_append(item)
except:
self.data += [str(what)]
else: else:
self.data += [str(what)] self.data += [what]
# ------------------------------------------------------------------ # ------------------------------------------------------------------
def data_set(self, def data_set(self,