class AutoPickParameter is now working; work on ticket #119 not finished yet (closure pending)

This commit is contained in:
Sebastian Wehling-Benatelli 2014-02-18 09:33:36 +01:00
parent 5a093ed736
commit f9d8173b2f

View File

@ -10,7 +10,10 @@ Created on Thu Feb 13 09:58:45 2014
class AutoPickParameter(object): class AutoPickParameter(object):
''' '''
AutoPickParameters is a parameter type object capable to read and/or write AutoPickParameters is a parameter type object capable to read and/or write
parameter ASCII and binary files. Parameters are given by example: parameter ASCII and binary files.
Parameters are given for example as follows:
phl S # phaselabel phl S # phaselabel
ff1 0.1 # freqmin ff1 0.1 # freqmin
ff2 0.5 # freqmax ff2 0.5 # freqmax
@ -31,7 +34,7 @@ class AutoPickParameter(object):
proPh Sn # nextprominentphase proPh Sn # nextprominentphase
''' '''
def __init__(self, fn): def __init__(self, fn=None):
''' '''
Initialize parameter object: Initialize parameter object:
@ -41,45 +44,47 @@ class AutoPickParameter(object):
self.filename = fn self.filename = fn
parFileCont = {} parFileCont = {}
try: try:
inputFile = open(self.filename, 'r') if self.filename is not None:
lines = inputFile.readlines() inputFile = open(self.filename, 'r')
for line in lines: lines = inputFile.readlines()
parspl = line.split('\t')[:2] for line in lines:
parFileCont[parspl[0]] = parspl[1] parspl = line.split('\t')[:2]
for key, value in parFileCont.iteritems(): parFileCont[parspl[0].strip()] = parspl[1]
try: for key, value in parFileCont.iteritems():
val = int(value)
except:
try: try:
val = float(value) val = int(value)
except: except:
if value.find(';') > 0: try:
vallist = value.strip().split(';') val = float(value)
val = [] except:
for val0 in vallist: if value.find(';') > 0:
val0 = float(val0) vallist = value.strip().split(';')
val.append(val0) val = []
else: for val0 in vallist:
val = str(value.strip()) val0 = float(val0)
parFileCont[key] = val val.append(val0)
else:
val = str(value.strip())
parFileCont[key] = val
else:
parFileCont = {}
except Exception, e: except Exception, e:
raise 'ParameterError:\n %s' % e self._printParameterError(e)
finally: parFileCont = {}
parFileCont = None
self.__parameter = parFileCont self.__parameter = parFileCont
def __str__(self): def __str__(self):
string = '' string = ''
string += 'Automated picking parameter:\n\n' string += 'Automated picking parameter:\n\n'
if self.__parameter is not None: if self.__parameter:
for key, value in self.__parameter.iteritems(): for key, value in self.__parameter.iteritems():
string += '%s:\t\t%s' % (key, value) string += '%s:\t\t%s\n' % (key, value)
else: else:
string += 'Empty parameter dictionary.' string += 'Empty parameter dictionary.'
return string return string
def __repr__(self): def __repr__(self):
return 'AutoPickParameter(%s)' % self.filename return "AutoPickParameter('%s')" % self.filename
def __nonzero__(self): def __nonzero__(self):
return self.__parameter return self.__parameter
@ -90,22 +95,21 @@ class AutoPickParameter(object):
try: try:
return self.__parameter[param] return self.__parameter[param]
except KeyError, e: except KeyError, e:
raise 'ParameterError:\n %s' % e self._printParameterError(e)
except TypeError: except TypeError:
try: try:
return self.__parameter[param] return self.__parameter[args]
except KeyError, e: except KeyError, e:
raise 'ParameterError:\n %s' % e self._printParameterError(e)
def setParam(self, **kwargs): def setParam(self, **kwargs):
for param, value in kwargs: for param, value in kwargs.iteritems():
try: try:
self.__parameter[param] = value self.__parameter[param] = value
except KeyError, e: except KeyError, e:
raise 'ParameterError:\n %s' % e self._printParameterError(e)
finally: finally:
self.__str__() print self
def _printParameterError(self, errmsg):
class ParameterError(Exception): print 'ParameterError:\n non-existent parameter %s' % errmsg
pass