From 8d76bc23c954136c8e5f8d5489eab132f1ef9c1a Mon Sep 17 00:00:00 2001 From: motdam Date: Mon, 2 Mar 2026 17:24:27 +0000 Subject: [PATCH 1/2] Add read_yml and write_yml path extensions --- nbs/03_xtras.ipynb | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) diff --git a/nbs/03_xtras.ipynb b/nbs/03_xtras.ipynb index 745b832a..145ff4b0 100644 --- a/nbs/03_xtras.ipynb +++ b/nbs/03_xtras.ipynb @@ -1589,6 +1589,22 @@ " return loads(self.read_text(encoding=encoding, errors=errors))" ] }, + { + "cell_type": "code", + "execution_count": null, + "id": "6c5f7de3", + "metadata": {}, + "outputs": [], + "source": [ + "#| export\n", + "@patch\n", + "def read_yml(self:Path, encoding=None, errors=None):\n", + " \"Same as `read_text` followed by `yaml.safe_load`\"\n", + " try: import yaml\n", + " except ImportError: raise ImportError(\"pip install pyyaml\")\n", + " return yaml.safe_load(self.read_text(encoding=encoding, errors=errors))" + ] + }, { "cell_type": "code", "execution_count": null, @@ -1619,6 +1635,22 @@ " self.mk_write(dumps(data,**kw),encoding,errors,mode,uid,gid)" ] }, + { + "cell_type": "code", + "execution_count": null, + "id": "0290ae06", + "metadata": {}, + "outputs": [], + "source": [ + "#| export\n", + "@patch\n", + "def write_yml(self:Path, data, encoding=None, errors=None, mode=511, uid=-1, gid=-1, **kw):\n", + " \"Same as `yaml.dump` followed by `mk_write`\"\n", + " try: import yaml\n", + " except ImportError: raise ImportError(\"pip install pyyaml\")\n", + " self.mk_write(yaml.dump(data, **kw), encoding, errors, mode, uid, gid)" + ] + }, { "cell_type": "code", "execution_count": null, From c0199f66e1a4101c9baa5b0812361fe861682747 Mon Sep 17 00:00:00 2001 From: motdam Date: Mon, 2 Mar 2026 17:25:24 +0000 Subject: [PATCH 2/2] nbdev-export python files --- fastcore/_modidx.py | 2 ++ fastcore/xtras.py | 16 ++++++++++++++++ 2 files changed, 18 insertions(+) diff --git a/fastcore/_modidx.py b/fastcore/_modidx.py index 2fd621fc..3cce5b6d 100644 --- a/fastcore/_modidx.py +++ b/fastcore/_modidx.py @@ -704,9 +704,11 @@ 'fastcore.xtras.Path.mk_write': ('xtras.html#path.mk_write', 'fastcore/xtras.py'), 'fastcore.xtras.Path.normpath': ('xtras.html#path.normpath', 'fastcore/xtras.py'), 'fastcore.xtras.Path.read_json': ('xtras.html#path.read_json', 'fastcore/xtras.py'), + 'fastcore.xtras.Path.read_yml': ('xtras.html#path.read_yml', 'fastcore/xtras.py'), 'fastcore.xtras.Path.readlines': ('xtras.html#path.readlines', 'fastcore/xtras.py'), 'fastcore.xtras.Path.relpath': ('xtras.html#path.relpath', 'fastcore/xtras.py'), 'fastcore.xtras.Path.write_json': ('xtras.html#path.write_json', 'fastcore/xtras.py'), + 'fastcore.xtras.Path.write_yml': ('xtras.html#path.write_yml', 'fastcore/xtras.py'), 'fastcore.xtras.ReindexCollection': ('xtras.html#reindexcollection', 'fastcore/xtras.py'), 'fastcore.xtras.ReindexCollection.__getitem__': ( 'xtras.html#reindexcollection.__getitem__', 'fastcore/xtras.py'), diff --git a/fastcore/xtras.py b/fastcore/xtras.py index 8ae1db07..79771b6a 100644 --- a/fastcore/xtras.py +++ b/fastcore/xtras.py @@ -433,6 +433,14 @@ def read_json(self:Path, encoding=None, errors=None): "Same as `read_text` followed by `loads`" return loads(self.read_text(encoding=encoding, errors=errors)) +# %% ../nbs/03_xtras.ipynb #6c5f7de3 +@patch +def read_yml(self:Path, encoding=None, errors=None): + "Same as `read_text` followed by `yaml.safe_load`" + try: import yaml + except ImportError: raise ImportError("pip install pyyaml") + return yaml.safe_load(self.read_text(encoding=encoding, errors=errors)) + # %% ../nbs/03_xtras.ipynb #d6d8d893 @patch def mk_write(self:Path, data, encoding=None, errors=None, mode=511, uid=-1, gid=-1): @@ -447,6 +455,14 @@ def write_json(self:Path, data, encoding=None, errors=None, mode=511, uid=-1, gi "Same as `dumps`followed by `mk_write`" self.mk_write(dumps(data,**kw),encoding,errors,mode,uid,gid) +# %% ../nbs/03_xtras.ipynb #0290ae06 +@patch +def write_yml(self:Path, data, encoding=None, errors=None, mode=511, uid=-1, gid=-1, **kw): + "Same as `yaml.dump` followed by `mk_write`" + try: import yaml + except ImportError: raise ImportError("pip install pyyaml") + self.mk_write(yaml.dump(data, **kw), encoding, errors, mode, uid, gid) + # %% ../nbs/03_xtras.ipynb #9fc9965f @patch def relpath(self:Path, start=None):