|
1227 | 1227 | "\n",
|
1228 | 1228 | "> L.groupby (key, val=<function noop>)\n",
|
1229 | 1229 | "\n",
|
1230 |
| - "*Same as `groupby`*" |
| 1230 | + "*Same as `fastcore.basics.groupby`*" |
1231 | 1231 | ],
|
1232 | 1232 | "text/plain": [
|
1233 | 1233 | "---\n",
|
|
1238 | 1238 | "\n",
|
1239 | 1239 | "> L.groupby (key, val=<function noop>)\n",
|
1240 | 1240 | "\n",
|
1241 |
| - "*Same as `groupby`*" |
| 1241 | + "*Same as `fastcore.basics.groupby`*" |
1242 | 1242 | ]
|
1243 | 1243 | },
|
1244 | 1244 | "execution_count": null,
|
|
2301 | 2301 | "#|export\n",
|
2302 | 2302 | "class Config:\n",
|
2303 | 2303 | " \"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", |
2305 | 2305 | " self.types = types or {}\n",
|
2306 | 2306 | " cfg_path = Path(cfg_path).expanduser().absolute()\n",
|
2307 | 2307 | " 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", |
2309 | 2309 | " self.d = self._cfg['DEFAULT']\n",
|
2310 | 2310 | " found = [Path(o) for o in self._cfg.read(L(extra_files)+[self.config_file], encoding='utf8')]\n",
|
2311 | 2311 | " if self.config_file not in found and create is not None:\n",
|
|
2421 | 2421 | "assert not Path('../tmp.ini').exists()"
|
2422 | 2422 | ]
|
2423 | 2423 | },
|
| 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 | + }, |
2424 | 2472 | {
|
2425 | 2473 | "cell_type": "code",
|
2426 | 2474 | "execution_count": null,
|
|
0 commit comments