polishing and changed to comply with PEP 8 style guide

This commit is contained in:
Claudio Zambaldi 2011-11-23 15:42:18 +00:00
parent 205bb49b34
commit 99b7dcf7b5
1 changed files with 20 additions and 51 deletions

View File

@ -13,21 +13,20 @@ class DAMASK_TOOLS():
class ASCII_TABLE(): class ASCII_TABLE():
'''
There should be a doc string here :)
'''
import sys import sys
__slots__ = ['__IO__', __slots__ = ['__IO__',
'info', 'info',
'labels', 'labels',
'data', 'data',
] ]
#.............................................
def __init__(self, def __init__(self,
fileIn = sys.stdin, fileIn = sys.stdin,
fileOut = sys.stdout, fileOut = sys.stdout,
buffered = True): buffered = True):
self.__IO__ = {'in': fileIn, self.__IO__ = {'in': fileIn,
'out':fileOut, 'out':fileOut,
'output':[], 'output':[],
@ -38,41 +37,31 @@ class ASCII_TABLE():
self.labels = [] self.labels = []
self.data = [] self.data = []
#.............................................
def output_write(self, def output_write(self,
what): what):
if isinstance(what,list): if isinstance(what,list):
for item in what: self.output_write(item) for item in what: self.output_write(item)
else: else:
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 - if keyword "head[*]" is present - the last line of the header '''
get column labels by either read the first row, or
--if keyword "head[*]" is present-- the last line of the header
'''
try: try:
self.__IO__['in'].seek(0) self.__IO__['in'].seek(0)
except: except:
pass pass
firstline = self.__IO__['in'].readline() firstline = self.__IO__['in'].readline()
m = re.search('(\d+)\s*head', firstline.lower()) m = re.search('(\d+)\s*head', firstline.lower())
if m: if m:
@ -81,72 +70,50 @@ class ASCII_TABLE():
else: else:
self.info = [] self.info = []
self.labels = firstline.split() self.labels = firstline.split()
self.__IO__['validReadSize'] = len(self.labels) self.__IO__['validReadSize'] = len(self.labels)
#.............................................
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_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 line != ''
#.............................................
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)]
class MATERIAL_CONFIG(): class MATERIAL_CONFIG():
'''
Reads, manipulates and writes material.config files
'''
__slots__ = ['data'] __slots__ = ['data']
def __init__(self): def __init__(self):
self.parts = [ self.parts = [
@ -310,17 +277,19 @@ class MATERIAL_CONFIG():
'__order__':['crystallite','(constituent)']} '__order__':['crystallite','(constituent)']}
self.add_data(part='microstructure',section=label,data=data) self.add_data(part='microstructure',section=label,data=data)
def change_value(self, part=None,
def change_value(self,part=None,section=None,key=None,value=None): section=None,
key=None,
value=None):
if type(value) is not type([]): if type(value) is not type([]):
if type(value) is not type('s'): if type(value) is not type('s'):
value='%s'%value value = '%s'%value
value=[value] value = [value]
newlen=len(value) newlen = len(value)
oldval=self.data[part][section][key] oldval = self.data[part][section][key]
oldlen=len(oldval) oldlen = len(oldval)
print('changing %s:%s:%s:%s'%(part,section,key,oldval)) print('changing %s:%s:%s:%s'%(part,section,key,oldval))
self.data[part][section][key]=value self.data[part][section][key] = value
print('new: %s'%self.data[part][section][key]) print('new: %s'%self.data[part][section][key])
if newlen is not oldlen: if newlen is not oldlen:
print('Length of value was changed from %i to %i!'%(oldlen,newlen)) print('Length of value was changed from %i to %i!'%(oldlen,newlen))