|
255 | 255 | "execution_count": null,
|
256 | 256 | "metadata": {},
|
257 | 257 | "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 | + }, |
258 | 266 | {
|
259 | 267 | "data": {
|
260 | 268 | "text/markdown": [
|
|
1227 | 1235 | "\n",
|
1228 | 1236 | "> L.groupby (key, val=<function noop>)\n",
|
1229 | 1237 | "\n",
|
1230 |
| - "*Same as `groupby`*" |
| 1238 | + "*Same as `fastcore.basics.groupby`*" |
1231 | 1239 | ],
|
1232 | 1240 | "text/plain": [
|
1233 | 1241 | "---\n",
|
|
1238 | 1246 | "\n",
|
1239 | 1247 | "> L.groupby (key, val=<function noop>)\n",
|
1240 | 1248 | "\n",
|
1241 |
| - "*Same as `groupby`*" |
| 1249 | + "*Same as `fastcore.basics.groupby`*" |
1242 | 1250 | ]
|
1243 | 1251 | },
|
1244 | 1252 | "execution_count": null,
|
|
2301 | 2309 | "#|export\n",
|
2302 | 2310 | "class Config:\n",
|
2303 | 2311 | " \"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", |
2305 | 2313 | " self.types = types or {}\n",
|
2306 | 2314 | " cfg_path = Path(cfg_path).expanduser().absolute()\n",
|
2307 | 2315 | " 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", |
2309 | 2317 | " self.d = self._cfg['DEFAULT']\n",
|
2310 | 2318 | " found = [Path(o) for o in self._cfg.read(L(extra_files)+[self.config_file], encoding='utf8')]\n",
|
2311 | 2319 | " if self.config_file not in found and create is not None:\n",
|
|
2421 | 2429 | "assert not Path('../tmp.ini').exists()"
|
2422 | 2430 | ]
|
2423 | 2431 | },
|
| 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 | + }, |
2424 | 2480 | {
|
2425 | 2481 | "cell_type": "code",
|
2426 | 2482 | "execution_count": null,
|
|
0 commit comments