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,10 +40,17 @@ class ASCIItable():
# ------------------------------------------------------------------ # ------------------------------------------------------------------
def output_write(self, def output_write(self,
what): what):
if isinstance(what,list): '''
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) for item in what: self.output_write(item)
else: except:
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()
# ------------------------------------------------------------------ # ------------------------------------------------------------------
@ -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)):
try:
for item in what: self.labels_append(item) for item in what: self.labels_append(item)
else: self.labels += [str(what)] 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)):
try:
for item in what: self.info_append(item) for item in what: self.info_append(item)
else: self.info += [str(what)] except:
self.info += [str(what)]
else:
self.info += [what]
# ------------------------------------------------------------------ # ------------------------------------------------------------------
def info_clear(self): def info_clear(self):
@ -200,6 +215,7 @@ 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:
@ -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)):
try:
for item in what: self.data_append(item) for item in what: self.data_append(item)
else: except:
self.data += [str(what)] self.data += [str(what)]
else:
self.data += [what]
# ------------------------------------------------------------------ # ------------------------------------------------------------------
def data_set(self, def data_set(self,