Skip to content

Commit 7b63121

Browse files
authored
Merge pull request #702 from AnswerDotAI/write_json
add write_json
2 parents a7fef51 + 24ae321 commit 7b63121

File tree

3 files changed

+20
-0
lines changed

3 files changed

+20
-0
lines changed

fastcore/_modidx.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -577,6 +577,7 @@
577577
'fastcore.xtras.Path.read_json': ('xtras.html#path.read_json', 'fastcore/xtras.py'),
578578
'fastcore.xtras.Path.readlines': ('xtras.html#path.readlines', 'fastcore/xtras.py'),
579579
'fastcore.xtras.Path.relpath': ('xtras.html#path.relpath', 'fastcore/xtras.py'),
580+
'fastcore.xtras.Path.write_json': ('xtras.html#path.write_json', 'fastcore/xtras.py'),
580581
'fastcore.xtras.ReindexCollection': ('xtras.html#reindexcollection', 'fastcore/xtras.py'),
581582
'fastcore.xtras.ReindexCollection.__getitem__': ( 'xtras.html#reindexcollection.__getitem__',
582583
'fastcore/xtras.py'),

fastcore/xtras.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -366,6 +366,12 @@ def mk_write(self:Path, data, encoding=None, errors=None, mode=511, uid=-1, gid=
366366
self.write_text(data, encoding=encoding, errors=errors)
367367
if uid!=-1 or gid!=-1: os.chown(self, uid, gid)
368368

369+
# %% ../nbs/03_xtras.ipynb
370+
@patch
371+
def write_json(self:Path, data, encoding=None, errors=None, mode=511, uid=-1, gid=-1, **kw):
372+
"Same as `dumps`followed by `mk_write`"
373+
self.mk_write(dumps(data,**kw),encoding,errors,mode,uid,gid)
374+
369375
# %% ../nbs/03_xtras.ipynb
370376
@patch
371377
def relpath(self:Path, start=None):

nbs/03_xtras.ipynb

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1237,6 +1237,19 @@
12371237
" if uid!=-1 or gid!=-1: os.chown(self, uid, gid)"
12381238
]
12391239
},
1240+
{
1241+
"cell_type": "code",
1242+
"execution_count": null,
1243+
"metadata": {},
1244+
"outputs": [],
1245+
"source": [
1246+
"#|export\n",
1247+
"@patch\n",
1248+
"def write_json(self:Path, data, encoding=None, errors=None, mode=511, uid=-1, gid=-1, **kw):\n",
1249+
" \"Same as `dumps`followed by `mk_write`\"\n",
1250+
" self.mk_write(dumps(data,**kw),encoding,errors,mode,uid,gid)"
1251+
]
1252+
},
12401253
{
12411254
"cell_type": "code",
12421255
"execution_count": null,

0 commit comments

Comments
 (0)