Skip to content

Commit 27f6974

Browse files
author
ncoop57
committed
Update Config to support ConfigParser keyword arguments
1 parent 37de43d commit 27f6974

File tree

2 files changed

+62
-6
lines changed

2 files changed

+62
-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: 60 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -255,6 +255,14 @@
255255
"execution_count": null,
256256
"metadata": {},
257257
"outputs": [
258+
{
259+
"name": "stderr",
260+
"output_type": "stream",
261+
"text": [
262+
"/Users/nathan/.venv/lib/python3.12/site-packages/tqdm/auto.py:21: TqdmWarning: IProgress not found. Please update jupyter and ipywidgets. See https://ipywidgets.readthedocs.io/en/stable/user_install.html\n",
263+
" from .autonotebook import tqdm as notebook_tqdm\n"
264+
]
265+
},
258266
{
259267
"data": {
260268
"text/markdown": [
@@ -1227,7 +1235,7 @@
12271235
"\n",
12281236
"> L.groupby (key, val=<function noop>)\n",
12291237
"\n",
1230-
"*Same as `groupby`*"
1238+
"*Same as `fastcore.basics.groupby`*"
12311239
],
12321240
"text/plain": [
12331241
"---\n",
@@ -1238,7 +1246,7 @@
12381246
"\n",
12391247
"> L.groupby (key, val=<function noop>)\n",
12401248
"\n",
1241-
"*Same as `groupby`*"
1249+
"*Same as `fastcore.basics.groupby`*"
12421250
]
12431251
},
12441252
"execution_count": null,
@@ -2301,11 +2309,11 @@
23012309
"#|export\n",
23022310
"class Config:\n",
23032311
" \"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",
2312+
" def __init__(self, cfg_path, cfg_name, create=None, save=True, extra_files=None, types=None, **cfg_kwargs):\n",
23052313
" self.types = types or {}\n",
23062314
" cfg_path = Path(cfg_path).expanduser().absolute()\n",
23072315
" self.config_path,self.config_file = cfg_path,cfg_path/cfg_name\n",
2308-
" self._cfg = ConfigParser()\n",
2316+
" self._cfg = ConfigParser(**cfg_kwargs)\n",
23092317
" self.d = self._cfg['DEFAULT']\n",
23102318
" found = [Path(o) for o in self._cfg.read(L(extra_files)+[self.config_file], encoding='utf8')]\n",
23112319
" if self.config_file not in found and create is not None:\n",
@@ -2421,6 +2429,54 @@
24212429
"assert not Path('../tmp.ini').exists()"
24222430
]
24232431
},
2432+
{
2433+
"cell_type": "markdown",
2434+
"metadata": {},
2435+
"source": [
2436+
"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. "
2437+
]
2438+
},
2439+
{
2440+
"cell_type": "code",
2441+
"execution_count": null,
2442+
"metadata": {},
2443+
"outputs": [],
2444+
"source": [
2445+
"# Create a complete example config file with comments\n",
2446+
"cfg_str = \"\"\"\\\n",
2447+
"[DEFAULT]\n",
2448+
"user = fastai # inline comment\n",
2449+
"\n",
2450+
"# Library configuration\n",
2451+
"lib_name = fastcore\n",
2452+
"\n",
2453+
"# Paths\n",
2454+
"some_path = test \n",
2455+
"\n",
2456+
"# Feature flags\n",
2457+
"some_bool = True\n",
2458+
"\n",
2459+
"# Numeric settings\n",
2460+
"some_num = # missing value\n",
2461+
"\"\"\"\n",
2462+
"\n",
2463+
"with open('../tmp.ini', 'w') as f:\n",
2464+
" f.write(cfg_str)"
2465+
]
2466+
},
2467+
{
2468+
"cell_type": "code",
2469+
"execution_count": null,
2470+
"metadata": {},
2471+
"outputs": [],
2472+
"source": [
2473+
"# Now read it back to verify\n",
2474+
"try: cfg = Config('..', 'tmp.ini', inline_comment_prefixes=('#'))\n",
2475+
"finally: os.unlink('../tmp.ini')\n",
2476+
"test_eq(cfg.user,'fastai')\n",
2477+
"test_eq(cfg.some_num,'')"
2478+
]
2479+
},
24242480
{
24252481
"cell_type": "code",
24262482
"execution_count": null,

0 commit comments

Comments
 (0)