In [1]:
import configparser
In [2]:
cfg = configparser.ConfigParser()
list(cfg.keys())
Out[2]:
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))
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]:
In [5]:
list(cfg['DMN'].items())
Out[5]:
In [15]:
with open('default.cfg', 'w') as configfile:
cfg.write(configfile)
In [54]:
cfg.read('default.cfg')
Out[54]:
In [55]:
cfg
Out[55]:
In [56]:
cfg.sections()
Out[56]:
In [57]:
list(cfg['DMN'].items())
Out[57]:
In [60]:
lazyparse(cfg['DMN']['blank'])
Out[60]:
In [39]:
bool('false')
Out[39]:
In [40]:
cfg.getboolean('False')
In [52]:
list('[123.4]'.lower())
Out[52]:
In [ ]: