Source code for nupic.research.support.parse_config

#  Numenta Platform for Intelligent Computing (NuPIC)
#  Copyright (C) 2019, Numenta, Inc.  Unless you have an agreement
#  with Numenta, Inc., for a separate license for this software code, the
#  following terms and conditions apply:
#
#  This program is free software: you can redistribute it and/or modify
#  it under the terms of the GNU Affero Public License version 3 as
#  published by the Free Software Foundation.
#
#  This program is distributed in the hope that it will be useful,
#  but WITHOUT ANY WARRANTY; without even the implied warranty of
#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
#  See the GNU Affero Public License for more details.
#
#  You should have received a copy of the GNU Affero Public License
#  along with this program.  If not, see http://www.gnu.org/licenses.
#
#  http://numenta.org/licenses/
#

import configparser


[docs]def parse_config(config_file, experiments=None, globals_param=None, locals_param=None): """Parse configuration file optionally filtering for specific experiments/sections. :param config_file: Configuration file :param experiments: Optional list of experiments :param globals_param: global symbol table to use during `eval` :param locals_param: local symbol table to use during `eval` :return: Dictionary with the parsed configuration """ cfgparser = configparser.ConfigParser() cfgparser.read_file(config_file) params = {} for exp in cfgparser.sections(): if not experiments or exp in experiments: values = dict(cfgparser.defaults()) values.update(dict(cfgparser.items(exp))) item = {} for k, v in values.items(): try: item[k] = eval(v, globals_param, locals_param) except (NameError, SyntaxError): item[k] = v params[exp] = item return params