mirror of
https://github.com/triqs/dft_tools
synced 2025-01-03 01:55:56 +01:00
Added defulats to optional group parameters
This commit is contained in:
parent
9d4fb22572
commit
74ef3ca93e
@ -65,9 +65,8 @@ class ConfigParameters:
|
|||||||
'emax': ('emax', float)}
|
'emax': ('emax', float)}
|
||||||
|
|
||||||
self.gr_optional = {
|
self.gr_optional = {
|
||||||
'normalize' : ('normalize', self.parse_string_logical),
|
'normalize' : ('normalize', self.parse_string_logical, True),
|
||||||
'normion' : ('normion', self.parse_string_logical)}
|
'normion' : ('normion', self.parse_string_logical, False)}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
#
|
#
|
||||||
@ -168,13 +167,14 @@ class ConfigParameters:
|
|||||||
# parse_parameter_set()
|
# 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.
|
Parses required or optional parameter set from a section.
|
||||||
For required parameters `exception=True` must be set.
|
For required parameters `exception=True` must be set.
|
||||||
"""
|
"""
|
||||||
parsed = {}
|
parsed = {}
|
||||||
for par in param_set.keys():
|
for par in param_set.keys():
|
||||||
|
key = param_set[par][0]
|
||||||
try:
|
try:
|
||||||
par_str = self.cp.get(section, par)
|
par_str = self.cp.get(section, par)
|
||||||
except ConfigParser.NoOptionError:
|
except ConfigParser.NoOptionError:
|
||||||
@ -182,12 +182,14 @@ class ConfigParameters:
|
|||||||
message = "Required parameter '%s' not found in section [%s]"%(par, section)
|
message = "Required parameter '%s' not found in section [%s]"%(par, section)
|
||||||
raise Exception(message)
|
raise Exception(message)
|
||||||
else:
|
else:
|
||||||
|
# Use the default value if there is one
|
||||||
|
if defaults and len(param_set[par]) > 2:
|
||||||
|
parsed[key] = param_set[par][2]
|
||||||
continue
|
continue
|
||||||
|
|
||||||
if self.verbosity > 0:
|
if self.verbosity > 0:
|
||||||
print " %s = %s"%(par, par_str)
|
print " %s = %s"%(par, par_str)
|
||||||
|
|
||||||
key = param_set[par][0]
|
|
||||||
parse_fun = param_set[par][1]
|
parse_fun = param_set[par][1]
|
||||||
parsed[key] = parse_fun(par_str)
|
parsed[key] = parse_fun(par_str)
|
||||||
|
|
||||||
@ -267,7 +269,7 @@ class ConfigParameters:
|
|||||||
shell.update(parsed)
|
shell.update(parsed)
|
||||||
|
|
||||||
# Group optional parameters
|
# 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)
|
shell.update(parsed)
|
||||||
|
|
||||||
self.shells.append(shell)
|
self.shells.append(shell)
|
||||||
|
@ -34,8 +34,10 @@ class TestParseGroups(arraytest.ArrayTestCase):
|
|||||||
conf_pars = ConfigParameters('example.cfg')
|
conf_pars = ConfigParameters('example.cfg')
|
||||||
conf_pars.parse_groups()
|
conf_pars.parse_groups()
|
||||||
res = conf_pars.groups
|
res = conf_pars.groups
|
||||||
expected = [{'index': 1, 'shells': [1, 2], 'emin': -7.6, 'emax': 3.0},
|
expected = [{'index': 1, 'shells': [1, 2], 'emin': -7.6, 'emax': 3.0,
|
||||||
{'index': 2, 'shells': [3], 'emin': -1.6, 'emax': 2.0}]
|
'normalize': True, 'normion': False},
|
||||||
|
{'index': 2, 'shells': [3], 'emin': -1.6, 'emax': 2.0,
|
||||||
|
'normalize': True, 'normion': False}]
|
||||||
self.assertListEqual(res, expected)
|
self.assertListEqual(res, expected)
|
||||||
|
|
||||||
|
|
||||||
|
@ -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}]
|
[ 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:
|
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)
|
self.assertEqual(res, expected)
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user