3
0
mirror of https://github.com/triqs/dft_tools synced 2024-06-22 05:02:20 +02:00

Added defulats to optional group parameters

This commit is contained in:
Oleg E. Peil 2015-02-17 19:19:13 +01:00 committed by Michel Ferrero
parent 9d4fb22572
commit 74ef3ca93e
3 changed files with 13 additions and 9 deletions

View File

@ -65,9 +65,8 @@ class ConfigParameters:
'emax': ('emax', float)}
self.gr_optional = {
'normalize' : ('normalize', self.parse_string_logical),
'normion' : ('normion', self.parse_string_logical)}
'normalize' : ('normalize', self.parse_string_logical, True),
'normion' : ('normion', self.parse_string_logical, False)}
#
@ -168,13 +167,14 @@ class ConfigParameters:
# parse_parameter_set()
#
################################################################################
def parse_parameter_set(self, section, param_set, exception=False):
def parse_parameter_set(self, section, param_set, exception=False, defaults=True):
"""
Parses required or optional parameter set from a section.
For required parameters `exception=True` must be set.
"""
parsed = {}
for par in param_set.keys():
key = param_set[par][0]
try:
par_str = self.cp.get(section, par)
except ConfigParser.NoOptionError:
@ -182,12 +182,14 @@ class ConfigParameters:
message = "Required parameter '%s' not found in section [%s]"%(par, section)
raise Exception(message)
else:
# Use the default value if there is one
if defaults and len(param_set[par]) > 2:
parsed[key] = param_set[par][2]
continue
if self.verbosity > 0:
print " %s = %s"%(par, par_str)
key = param_set[par][0]
parse_fun = param_set[par][1]
parsed[key] = parse_fun(par_str)
@ -267,7 +269,7 @@ class ConfigParameters:
shell.update(parsed)
# Group optional parameters
parsed = self.parse_parameter_set(section, self.gr_optional, exception=False)
parsed = self.parse_parameter_set(section, self.gr_optional, exception=False, defaults=False)
shell.update(parsed)
self.shells.append(shell)

View File

@ -34,8 +34,10 @@ class TestParseGroups(arraytest.ArrayTestCase):
conf_pars = ConfigParameters('example.cfg')
conf_pars.parse_groups()
res = conf_pars.groups
expected = [{'index': 1, 'shells': [1, 2], 'emin': -7.6, 'emax': 3.0},
{'index': 2, 'shells': [3], 'emin': -1.6, 'emax': 2.0}]
expected = [{'index': 1, 'shells': [1, 2], 'emin': -7.6, 'emax': 3.0,
'normalize': True, 'normion': False},
{'index': 2, 'shells': [3], 'emin': -1.6, 'emax': 2.0,
'normalize': True, 'normion': False}]
self.assertListEqual(res, expected)

View File

@ -77,7 +77,7 @@ class TestParseInput(arraytest.ArrayTestCase):
[ 0., 0., 1.]]), 'ion_list': array([0, 1, 2, 3]), 'user_index': 2, 'lshell': 1}, {'ion_list': array([0, 1, 2, 3]), 'user_index': 3, 'lshell': 3}]
Groups:
[{'index': 1, 'shells': [0, 1], 'emin': -7.6, 'emax': 3.0}, {'index': 2, 'shells': [2], 'emin': -1.6, 'emax': 2.0}]"""
[{'normalize': True, 'index': 1, 'shells': [0, 1], 'normion': False, 'emax': 3.0, 'emin': -7.6}, {'normalize': True, 'index': 2, 'shells': [2], 'normion': False, 'emax': 2.0, 'emin': -1.6}]"""
self.assertEqual(res, expected)