Skip to content

Commit e4335a1

Browse files
authored
Merge pull request #659 from AnswerDotAI/config_parser
Update Config to support ConfigParser keyword arguments
2 parents 4f5a823 + f8ac714 commit e4335a1

File tree

2 files changed

+54
-6
lines changed

2 files changed

+54
-6
lines changed

fastcore/foundation.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -260,11 +260,11 @@ def read_config_file(file, **kwargs):
260260
# %% ../nbs/02_foundation.ipynb
261261
class Config:
262262
"Reading and writing `ConfigParser` ini files"
263-
def __init__(self, cfg_path, cfg_name, create=None, save=True, extra_files=None, types=None):
263+
def __init__(self, cfg_path, cfg_name, create=None, save=True, extra_files=None, types=None, **cfg_kwargs):
264264
self.types = types or {}
265265
cfg_path = Path(cfg_path).expanduser().absolute()
266266
self.config_path,self.config_file = cfg_path,cfg_path/cfg_name
267-
self._cfg = ConfigParser()
267+
self._cfg = ConfigParser(**cfg_kwargs)
268268
self.d = self._cfg['DEFAULT']
269269
found = [Path(o) for o in self._cfg.read(L(extra_files)+[self.config_file], encoding='utf8')]
270270
if self.config_file not in found and create is not None:

nbs/02_foundation.ipynb

Lines changed: 52 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1227,7 +1227,7 @@
12271227
"\n",
12281228
"> L.groupby (key, val=<function noop>)\n",
12291229
"\n",
1230-
"*Same as `groupby`*"
1230+
"*Same as `fastcore.basics.groupby`*"
12311231
],
12321232
"text/plain": [
12331233
"---\n",
@@ -1238,7 +1238,7 @@
12381238
"\n",
12391239
"> L.groupby (key, val=<function noop>)\n",
12401240
"\n",
1241-
"*Same as `groupby`*"
1241+
"*Same as `fastcore.basics.groupby`*"
12421242
]
12431243
},
12441244
"execution_count": null,
@@ -2301,11 +2301,11 @@
23012301
"#|export\n",
23022302
"class Config:\n",
23032303
" \"Reading and writing `ConfigParser` ini files\"\n",
2304-
" def __init__(self, cfg_path, cfg_name, create=None, save=True, extra_files=None, types=None):\n",
2304+
" def __init__(self, cfg_path, cfg_name, create=None, save=True, extra_files=None, types=None, **cfg_kwargs):\n",
23052305
" self.types = types or {}\n",
23062306
" cfg_path = Path(cfg_path).expanduser().absolute()\n",
23072307
" self.config_path,self.config_file = cfg_path,cfg_path/cfg_name\n",
2308-
" self._cfg = ConfigParser()\n",
2308+
" self._cfg = ConfigParser(**cfg_kwargs)\n",
23092309
" self.d = self._cfg['DEFAULT']\n",
23102310
" found = [Path(o) for o in self._cfg.read(L(extra_files)+[self.config_file], encoding='utf8')]\n",
23112311
" if self.config_file not in found and create is not None:\n",
@@ -2421,6 +2421,54 @@
24212421
"assert not Path('../tmp.ini').exists()"
24222422
]
24232423
},
2424+
{
2425+
"cell_type": "markdown",
2426+
"metadata": {},
2427+
"source": [
2428+
"You can also pass in `ConfigParser` `kwargs` to change the behavior of how your configuration file will be parsed. For example, by default, inline comments are not handled by `Config`. However, if you pass in the `inline_comment_prefixes` with whatever your comment symbol is, you'll overwrite this behavior. "
2429+
]
2430+
},
2431+
{
2432+
"cell_type": "code",
2433+
"execution_count": null,
2434+
"metadata": {},
2435+
"outputs": [],
2436+
"source": [
2437+
"# Create a complete example config file with comments\n",
2438+
"cfg_str = \"\"\"\\\n",
2439+
"[DEFAULT]\n",
2440+
"user = fastai # inline comment\n",
2441+
"\n",
2442+
"# Library configuration\n",
2443+
"lib_name = fastcore\n",
2444+
"\n",
2445+
"# Paths\n",
2446+
"some_path = test \n",
2447+
"\n",
2448+
"# Feature flags\n",
2449+
"some_bool = True\n",
2450+
"\n",
2451+
"# Numeric settings\n",
2452+
"some_num = # missing value\n",
2453+
"\"\"\"\n",
2454+
"\n",
2455+
"with open('../tmp.ini', 'w') as f:\n",
2456+
" f.write(cfg_str)"
2457+
]
2458+
},
2459+
{
2460+
"cell_type": "code",
2461+
"execution_count": null,
2462+
"metadata": {},
2463+
"outputs": [],
2464+
"source": [
2465+
"# Now read it back to verify\n",
2466+
"try: cfg = Config('..', 'tmp.ini', inline_comment_prefixes=('#'))\n",
2467+
"finally: os.unlink('../tmp.ini')\n",
2468+
"test_eq(cfg.user,'fastai')\n",
2469+
"test_eq(cfg.some_num,'')"
2470+
]
2471+
},
24242472
{
24252473
"cell_type": "code",
24262474
"execution_count": null,

0 commit comments

Comments
 (0)