made AutoPickParameter class more flexible in reading and handling parameters; export routine defined: exports in the new autoPyLoT.in format (see docstring)

This commit is contained in:
Sebastian Wehling-Benatelli 2015-05-04 05:25:40 +02:00
parent 426c2d0d4c
commit 367610d032

View File

@ -34,24 +34,29 @@ class AutoPickParameter(object):
========== ========== ======================================= ========== ========== =======================================
''' '''
def __init__(self, fn=None): def __init__(self, fnin=None, fnout=None, **kwargs):
''' '''
Initialize parameter object: Initialize parameter object:
read content of an ASCII file an form a type consistent dictionary read content of an ASCII file an form a type consistent dictionary
contain all parameters. contain all parameters.
''' '''
self.__filename = fn
self.__filename = fnin
parFileCont = {} parFileCont = {}
try: # read from parsed arguments alternatively
if self.__filename is not None: for key, val in kwargs.iteritems():
parFileCont[key] = val
if self.__filename is not None:
inputFile = open(self.__filename, 'r') inputFile = open(self.__filename, 'r')
lines = inputFile.readlines() else:
for line in lines: return
parspl = line.split('\t')[:2] try:
parFileCont[parspl[0].strip()] = parspl[1] lines = inputFile.readlines()
else: for line in lines:
parFileCont = {} parspl = line.split('\t')[:2]
parFileCont[parspl[0].strip()] = parspl[1]
except Exception, e: except Exception, e:
self._printParameterError(e) self._printParameterError(e)
inputFile.seek(0) inputFile.seek(0)
@ -78,12 +83,15 @@ class AutoPickParameter(object):
parFileCont[key] = val parFileCont[key] = val
self.__parameter = parFileCont self.__parameter = parFileCont
if fnout:
self.export2File(fnout)
# Human-readable string representation of the object # Human-readable string representation of the object
def __str__(self): def __str__(self):
string = '' string = ''
string += 'Automated picking parameter:\n\n' string += 'Automated picking parameter:\n\n'
if self.__parameter: if self.__parameter:
for key, value in self.__parameter.iteritems(): for key, value in self.iteritems():
string += '%s:\t\t%s\n' % (key, value) string += '%s:\t\t%s\n' % (key, value)
else: else:
string += 'Empty parameter dictionary.' string += 'Empty parameter dictionary.'
@ -112,6 +120,25 @@ class AutoPickParameter(object):
def __len__(self): def __len__(self):
return len(self.__parameter.keys()) return len(self.__parameter.keys())
def iteritems(self):
for key, value in self.__parameter.iteritems():
yield key, value
def hasParam(self, *args):
def test(param):
try:
self.__parameter[param]
return True
except KeyError:
return False
try:
for param in args:
test(param)
except TypeError:
test(args)
def getParam(self, *args): def getParam(self, *args):
try: try:
for param in args: for param in args:
@ -127,14 +154,18 @@ class AutoPickParameter(object):
def setParam(self, **kwargs): def setParam(self, **kwargs):
for param, value in kwargs.iteritems(): for param, value in kwargs.iteritems():
try: self.__setitem__(param, value)
self.__setitem__(param, value)
except KeyError, e:
self._printParameterError(e)
print self print self
def _printParameterError(self, errmsg): def _printParameterError(self, errmsg):
print 'ParameterReadError:\n non-existent parameter %s' % errmsg print 'ParameterError:\n non-existent parameter %s' % errmsg
def export2File(self, fnout):
fid_out = open(fnout, 'w')
lines = []
for key, value in self.iteritems():
lines.append('{key}\t{value}'.format(key=key, value=value))
fid_out.writelines(lines)
class FilterOptions(object): class FilterOptions(object):