August 2018
Intermediate to advanced
366 pages
10h 14m
English
The steps for this recipe are:
import configparser def read_config(config_text, schema=None): """Read options from ``config_text`` applying given ``schema``""" schema = schema or {} cfg = configparser.ConfigParser( interpolation=configparser.ExtendedInterpolation() ) try: cfg.read_string(config_text) except configparser.MissingSectionHeaderError: config_text = '[main]\n' + config_text cfg.read_string(config_text) config = {} for section in schema: options = config.setdefault(section, {}) for option, option_schema in schema[section].items(): options[option] ...