In [1]:
import configparser

In [2]:
cfg = configparser.ConfigParser()
list(cfg.keys())


Out[2]:
['DEFAULT']

In [50]:
def lazyparse(value):
    """Life is short, use fast-and-loose parsers. This function will try very hard to coerce the value. 
    If it can't, it'll cowardly give up without an exception and return the original value"""
    value = value.lower()
    if value in ['yes', 'true', 'on', 'y']:
        return True
    if value in ['no', 'false', 'off', 'n']:
        return False
    try:
        return int(value)
    except ValueError:
        pass
    try:
        return float(value)
    except ValueError:
        pass
    return value

a = lazyparse('trUe')
b = lazyparse('1337')
c = lazyparse('1337.1415')
print(a, type(a))
print(b, type(b))
print(c, type(c))


True <class 'bool'>
1337 <class 'int'>
1337.1415 <class 'float'>

In [53]:
lazyparse('')


Out[53]:
''

In [3]:
sec1 = 'DMN'
cfg.add_section(sec1)
cfg.set(sec1, 'bidirect', 'True')
cfg.set(sec1, 'tdd', 'True')
cfg.set(sec1, 'n_lstm', '32')
# config.set('Section1', 'baz', 'fun')
# config.set('Section1', 'bar', 'Python')
# config.set('Section1', 'foo', '%(bar)s is %(baz)s!')

In [4]:
list(cfg.keys())


Out[4]:
['DEFAULT', 'DMN']

In [5]:
list(cfg['DMN'].items())


Out[5]:
[('bidirect', 'True'), ('tdd', 'True'), ('n_lstm', '32')]

In [15]:
with open('default.cfg', 'w') as configfile:
    cfg.write(configfile)

In [54]:
cfg.read('default.cfg')


Out[54]:
['default.cfg']

In [55]:
cfg


Out[55]:
<configparser.ConfigParser at 0x7efdfbffb6a0>

In [56]:
cfg.sections()


Out[56]:
['DMN']

In [57]:
list(cfg['DMN'].items())


Out[57]:
[('bidirect', 'False'), ('tdd', 'True'), ('n_lstm', '32'), ('blank', '')]

In [60]:
lazyparse(cfg['DMN']['blank'])


Out[60]:
''

In [39]:
bool('false')


Out[39]:
True

In [40]:
cfg.getboolean('False')


---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-40-3dc6d16d1fe9> in <module>()
----> 1 cfg.getboolean('False')

TypeError: getboolean() missing 1 required positional argument: 'option'

In [52]:
list('[123.4]'.lower())


Out[52]:
['[', '1', '2', '3', '.', '4', ']']

In [ ]: