Skip to content

Commit

Permalink
Merge pull request #455 from seeM/in-memory-config
Browse files Browse the repository at this point in the history
in-memory `Config` by passing `save=False`
  • Loading branch information
jph00 authored Aug 6, 2022
2 parents 76603b8 + 96f724d commit 67c0f3f
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 62 deletions.
11 changes: 5 additions & 6 deletions fastcore/foundation.py
Original file line number Diff line number Diff line change
Expand Up @@ -249,16 +249,15 @@ def read_config_file(file, **kwargs):
# %% ../nbs/02_foundation.ipynb 131
class Config:
"Reading and writing `ConfigParser` ini files"
def __init__(self, cfg_path, cfg_name, create=None):
def __init__(self, cfg_path, cfg_name, create=None, save=True):
cfg_path = Path(cfg_path).expanduser().absolute()
self.config_path,self.config_file = cfg_path,cfg_path/cfg_name
if not self.config_file.exists():
if create:
self.d = create
if self.config_file.exists(): self.d = read_config_file(self.config_file)
elif create is not None:
self.d = ConfigParser(create)['DEFAULT']
if save:
cfg_path.mkdir(exist_ok=True, parents=True)
self.save()
else: raise FileNotFoundError(f"Could not find {cfg_name}")
self.d = read_config_file(self.config_file)

def __setitem__(self,k,v): self.d[k] = str(v)
def __contains__(self,k): return k in self.d
Expand Down
110 changes: 54 additions & 56 deletions nbs/02_foundation.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -266,7 +266,7 @@
"Test whether `o` can be used in a `for` loop"
],
"text/plain": [
"<nbdev.showdoc.BasicMarkdownRenderer at 0x103ad5760>"
"<nbdev.showdoc.BasicMarkdownRenderer>"
]
},
"execution_count": null,
Expand Down Expand Up @@ -798,7 +798,7 @@
{
"data": {
"text/plain": [
"['j', 3, 1]"
"[5, 0, 11]"
]
},
"execution_count": null,
Expand All @@ -807,6 +807,7 @@
}
],
"source": [
"random.seed(0)\n",
"random.sample(t, 3)"
]
},
Expand Down Expand Up @@ -1013,7 +1014,7 @@
"Retrieve `idx` (can be list of indices, or mask, or int) items"
],
"text/plain": [
"<nbdev.showdoc.BasicMarkdownRenderer at 0x1268a5a90>"
"<nbdev.showdoc.BasicMarkdownRenderer>"
]
},
"execution_count": null,
Expand Down Expand Up @@ -1056,7 +1057,7 @@
"Set `idx` (can be list of indices, or mask, or int) items to `o` (which is broadcast if not iterable)"
],
"text/plain": [
"<nbdev.showdoc.BasicMarkdownRenderer at 0x1268c9790>"
"<nbdev.showdoc.BasicMarkdownRenderer>"
]
},
"execution_count": null,
Expand Down Expand Up @@ -1097,7 +1098,7 @@
"Unique items, in stable order"
],
"text/plain": [
"<nbdev.showdoc.BasicMarkdownRenderer at 0x10798ca90>"
"<nbdev.showdoc.BasicMarkdownRenderer>"
]
},
"execution_count": null,
Expand Down Expand Up @@ -1135,7 +1136,7 @@
"Dict from value to index"
],
"text/plain": [
"<nbdev.showdoc.BasicMarkdownRenderer at 0x1268c9c40>"
"<nbdev.showdoc.BasicMarkdownRenderer>"
]
},
"execution_count": null,
Expand Down Expand Up @@ -1168,13 +1169,13 @@
"\n",
"#### L.filter\n",
"\n",
"> L.filter (f=<functionnoopat0x103aff820>, negate=False, gen=False,\n",
"> L.filter (f=<functionnoopat0x11542e1f0>, negate=False, gen=False,\n",
"> **kwargs)\n",
"\n",
"Create new `L` filtered by predicate `f`, passing `args` and `kwargs` to `f`"
],
"text/plain": [
"<nbdev.showdoc.BasicMarkdownRenderer at 0x1268c9fa0>"
"<nbdev.showdoc.BasicMarkdownRenderer>"
]
},
"execution_count": null,
Expand Down Expand Up @@ -1233,7 +1234,7 @@
"Like `filter`, but return indices for matching items"
],
"text/plain": [
"<nbdev.showdoc.BasicMarkdownRenderer at 0x1268d0c70>"
"<nbdev.showdoc.BasicMarkdownRenderer>"
]
},
"execution_count": null,
Expand Down Expand Up @@ -1271,7 +1272,7 @@
"Return index of first matching item"
],
"text/plain": [
"<nbdev.showdoc.BasicMarkdownRenderer at 0x1268d0310>"
"<nbdev.showdoc.BasicMarkdownRenderer>"
]
},
"execution_count": null,
Expand Down Expand Up @@ -1309,7 +1310,7 @@
"Create new `L` with `f` applied to all `items`, passing `args` and `kwargs` to `f`"
],
"text/plain": [
"<nbdev.showdoc.BasicMarkdownRenderer at 0x1268e82e0>"
"<nbdev.showdoc.BasicMarkdownRenderer>"
]
},
"execution_count": null,
Expand Down Expand Up @@ -1391,12 +1392,12 @@
"\n",
"#### L.map_dict\n",
"\n",
"> L.map_dict (f=<functionnoopat0x103aff820>, *args, gen=False, **kwargs)\n",
"> L.map_dict (f=<functionnoopat0x11542e1f0>, *args, gen=False, **kwargs)\n",
"\n",
"Like `map`, but creates a dict from `items` to function results"
],
"text/plain": [
"<nbdev.showdoc.BasicMarkdownRenderer at 0x1268f4af0>"
"<nbdev.showdoc.BasicMarkdownRenderer>"
]
},
"execution_count": null,
Expand Down Expand Up @@ -1435,7 +1436,7 @@
"Create new `L` with `zip(*items)`"
],
"text/plain": [
"<nbdev.showdoc.BasicMarkdownRenderer at 0x1268f4520>"
"<nbdev.showdoc.BasicMarkdownRenderer>"
]
},
"execution_count": null,
Expand Down Expand Up @@ -1485,7 +1486,7 @@
"Combine `zip` and `starmap`"
],
"text/plain": [
"<nbdev.showdoc.BasicMarkdownRenderer at 0x1268f6c70>"
"<nbdev.showdoc.BasicMarkdownRenderer>"
]
},
"execution_count": null,
Expand Down Expand Up @@ -1524,7 +1525,7 @@
"Create new `L` with `self` zip with each of `*rest`"
],
"text/plain": [
"<nbdev.showdoc.BasicMarkdownRenderer at 0x1268f69a0>"
"<nbdev.showdoc.BasicMarkdownRenderer>"
]
},
"execution_count": null,
Expand Down Expand Up @@ -1564,7 +1565,7 @@
"Combine `zipwith` and `starmap`"
],
"text/plain": [
"<nbdev.showdoc.BasicMarkdownRenderer at 0x1269028e0>"
"<nbdev.showdoc.BasicMarkdownRenderer>"
]
},
"execution_count": null,
Expand Down Expand Up @@ -1602,7 +1603,7 @@
"Create new `L` with item `idx` of all `items`"
],
"text/plain": [
"<nbdev.showdoc.BasicMarkdownRenderer at 0x1269025b0>"
"<nbdev.showdoc.BasicMarkdownRenderer>"
]
},
"execution_count": null,
Expand Down Expand Up @@ -1640,7 +1641,7 @@
"Create new `L` with attr `k` (or value `k` for dicts) of all `items`."
],
"text/plain": [
"<nbdev.showdoc.BasicMarkdownRenderer at 0x1079e73d0>"
"<nbdev.showdoc.BasicMarkdownRenderer>"
]
},
"execution_count": null,
Expand Down Expand Up @@ -1684,7 +1685,7 @@
"New `L` sorted by `key`. If key is str use `attrgetter`; if int use `itemgetter`"
],
"text/plain": [
"<nbdev.showdoc.BasicMarkdownRenderer at 0x10792bfd0>"
"<nbdev.showdoc.BasicMarkdownRenderer>"
]
},
"execution_count": null,
Expand Down Expand Up @@ -1722,7 +1723,7 @@
"Class Method: Same as `str.split`, but returns an `L`"
],
"text/plain": [
"<nbdev.showdoc.BasicMarkdownRenderer at 0x107958f70>"
"<nbdev.showdoc.BasicMarkdownRenderer>"
]
},
"execution_count": null,
Expand Down Expand Up @@ -1760,7 +1761,7 @@
"Class Method: Same as `range`, but returns `L`. Can pass collection for `a`, to use `len(a)`"
],
"text/plain": [
"<nbdev.showdoc.BasicMarkdownRenderer at 0x107958220>"
"<nbdev.showdoc.BasicMarkdownRenderer>"
]
},
"execution_count": null,
Expand Down Expand Up @@ -1799,7 +1800,7 @@
"Concatenate all elements of list"
],
"text/plain": [
"<nbdev.showdoc.BasicMarkdownRenderer at 0x107958f40>"
"<nbdev.showdoc.BasicMarkdownRenderer>"
]
},
"execution_count": null,
Expand Down Expand Up @@ -1837,7 +1838,7 @@
"Same as `list.copy`, but returns an `L`"
],
"text/plain": [
"<nbdev.showdoc.BasicMarkdownRenderer at 0x10795f670>"
"<nbdev.showdoc.BasicMarkdownRenderer>"
]
},
"execution_count": null,
Expand Down Expand Up @@ -1871,13 +1872,13 @@
"\n",
"#### L.map_first\n",
"\n",
"> L.map_first (f=<functionnoopat0x103aff820>,\n",
"> g=<functionnoopat0x103aff820>, *args, **kwargs)\n",
"> L.map_first (f=<functionnoopat0x11542e1f0>,\n",
"> g=<functionnoopat0x11542e1f0>, *args, **kwargs)\n",
"\n",
"First element of `map_filter`"
],
"text/plain": [
"<nbdev.showdoc.BasicMarkdownRenderer at 0x10791fe80>"
"<nbdev.showdoc.BasicMarkdownRenderer>"
]
},
"execution_count": null,
Expand Down Expand Up @@ -1916,7 +1917,7 @@
"Call `setattr` on all items"
],
"text/plain": [
"<nbdev.showdoc.BasicMarkdownRenderer at 0x10793e0a0>"
"<nbdev.showdoc.BasicMarkdownRenderer>"
]
},
"execution_count": null,
Expand Down Expand Up @@ -2003,16 +2004,15 @@
"#|export\n",
"class Config:\n",
" \"Reading and writing `ConfigParser` ini files\"\n",
" def __init__(self, cfg_path, cfg_name, create=None):\n",
" def __init__(self, cfg_path, cfg_name, create=None, save=True):\n",
" cfg_path = Path(cfg_path).expanduser().absolute()\n",
" self.config_path,self.config_file = cfg_path,cfg_path/cfg_name\n",
" if not self.config_file.exists():\n",
" if create:\n",
" self.d = create\n",
" if self.config_file.exists(): self.d = read_config_file(self.config_file)\n",
" elif create is not None:\n",
" self.d = ConfigParser(create)['DEFAULT']\n",
" if save:\n",
" cfg_path.mkdir(exist_ok=True, parents=True)\n",
" self.save()\n",
" else: raise FileNotFoundError(f\"Could not find {cfg_name}\")\n",
" self.d = read_config_file(self.config_file)\n",
"\n",
" def __setitem__(self,k,v): self.d[k] = str(v)\n",
" def __contains__(self,k): return k in self.d\n",
Expand All @@ -2030,7 +2030,7 @@
"cell_type": "markdown",
"metadata": {},
"source": [
"`Config` and provides direct access to the 'DEFAULT' section of a `ConfigParser` ini file."
"`Config` is a convenient wrapper around `ConfigParser` ini files with a single section (`DEFAULT`)."
]
},
{
Expand All @@ -2048,6 +2048,24 @@
"test_eq(cfg.get('foo','bar'),'bar')"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"You can also create an in-memory `Config` by passing `save=False`:"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"cfg = Config('..', 'tmp.ini', create=_d, save=False)\n",
"test_eq(cfg.user,'fastai')\n",
"assert not Path('../tmp.ini').exists()"
]
},
{
"cell_type": "markdown",
"metadata": {},
Expand All @@ -2059,27 +2077,7 @@
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Converted 00_test.ipynb.\n",
"Converted 01_basics.ipynb.\n",
"Converted 02_foundation.ipynb.\n",
"Converted 03_xtras.ipynb.\n",
"Converted 03a_parallel.ipynb.\n",
"Converted 03b_net.ipynb.\n",
"Converted 04_dispatch.ipynb.\n",
"Converted 05_transform.ipynb.\n",
"Converted 06_docments.ipynb.\n",
"Converted 07_meta.ipynb.\n",
"Converted 08_script.ipynb.\n",
"Converted index.ipynb.\n",
"Converted parallel_win.ipynb.\n"
]
}
],
"outputs": [],
"source": [
"#|hide\n",
"#|eval: false\n",
Expand Down

0 comments on commit 67c0f3f

Please sign in to comment.