Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions fastcore/_modidx.py
Original file line number Diff line number Diff line change
Expand Up @@ -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'),
Expand Down
16 changes: 16 additions & 0 deletions fastcore/xtras.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand All @@ -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):
Expand Down
32 changes: 32 additions & 0 deletions nbs/03_xtras.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down