Skip to content

Commit 63fff2e

Browse files
committed
fixes #432
1 parent 8aef614 commit 63fff2e

File tree

3 files changed

+36
-14
lines changed

3 files changed

+36
-14
lines changed

fastcore/_nbdev.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -157,6 +157,7 @@
157157
"bunzip": "03_xtras.ipynb",
158158
"loads": "03_xtras.ipynb",
159159
"loads_multi": "03_xtras.ipynb",
160+
"dumps": "03_xtras.ipynb",
160161
"untar_dir": "03_xtras.ipynb",
161162
"repo_details": "03_xtras.ipynb",
162163
"run": "03_xtras.ipynb",

fastcore/xtras.py

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
from __future__ import annotations
55

66

7-
__all__ = ['walk', 'globtastic', 'maybe_open', 'image_size', 'bunzip', 'loads', 'loads_multi', 'untar_dir',
7+
__all__ = ['walk', 'globtastic', 'maybe_open', 'image_size', 'bunzip', 'loads', 'loads_multi', 'dumps', 'untar_dir',
88
'repo_details', 'run', 'open_file', 'save_pickle', 'load_pickle', 'dict2obj', 'obj2dict', 'repr_dict',
99
'is_listy', 'mapped', 'IterLen', 'ReindexCollection', 'get_source_link', 'truncstr', 'spark_chars',
1010
'sparkline', 'modify_exception', 'round_multiple', 'str2bool', 'set_num_threads', 'join_path_file',
@@ -113,13 +113,12 @@ def bunzip(fn):
113113
for d in iter(lambda: src.read(1024*1024), b''): dst.write(d)
114114

115115
# Cell
116-
def loads(s, cls=None, object_hook=None, parse_float=None,
117-
parse_int=None, parse_constant=None, object_pairs_hook=None, **kw):
116+
def loads(s, **kw):
118117
"Same as `json.loads`, but handles `None`"
119118
if not s: return {}
120-
import json
121-
return json.loads(s, cls=cls, object_hook=object_hook, parse_float=parse_float,
122-
parse_int=parse_int, parse_constant=parse_constant, object_pairs_hook=object_pairs_hook, **kw)
119+
try: import ujson as json
120+
except ModuleNotFoundError: import json
121+
return json.loads(s, **kw)
123122

124123
# Cell
125124
def loads_multi(s:str):
@@ -133,6 +132,14 @@ def loads_multi(s:str):
133132
yield obj
134133
s = s[pos:]
135134

135+
# Cell
136+
def dumps(obj, **kw):
137+
"Same as `json.dumps`, but uses `ujson` if available"
138+
try: import ujson as json
139+
except ModuleNotFoundError: import json
140+
else: kw['escape_forward_slashes']=False
141+
return json.dumps(obj, **kw)
142+
136143
# Cell
137144
def _unpack(fname, out):
138145
import shutil

nbs/03_xtras.ipynb

Lines changed: 22 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -398,13 +398,12 @@
398398
"outputs": [],
399399
"source": [
400400
"#export\n",
401-
"def loads(s, cls=None, object_hook=None, parse_float=None,\n",
402-
" parse_int=None, parse_constant=None, object_pairs_hook=None, **kw):\n",
401+
"def loads(s, **kw):\n",
403402
" \"Same as `json.loads`, but handles `None`\"\n",
404403
" if not s: return {}\n",
405-
" import json\n",
406-
" return json.loads(s, cls=cls, object_hook=object_hook, parse_float=parse_float,\n",
407-
" parse_int=parse_int, parse_constant=parse_constant, object_pairs_hook=object_pairs_hook, **kw)"
404+
" try: import ujson as json\n",
405+
" except ModuleNotFoundError: import json\n",
406+
" return json.loads(s, **kw)"
408407
]
409408
},
410409
{
@@ -444,6 +443,21 @@
444443
"test_eq(list(loads_multi(tst)), [{'a': 1}, {'b': 2}])"
445444
]
446445
},
446+
{
447+
"cell_type": "code",
448+
"execution_count": null,
449+
"metadata": {},
450+
"outputs": [],
451+
"source": [
452+
"#export\n",
453+
"def dumps(obj, **kw):\n",
454+
" \"Same as `json.dumps`, but uses `ujson` if available\"\n",
455+
" try: import ujson as json\n",
456+
" except ModuleNotFoundError: import json\n",
457+
" else: kw['escape_forward_slashes']=False\n",
458+
" return json.dumps(obj, **kw)"
459+
]
460+
},
447461
{
448462
"cell_type": "code",
449463
"execution_count": null,
@@ -1496,7 +1510,7 @@
14961510
{
14971511
"data": {
14981512
"text/plain": [
1499-
"['e', 'c', 'f', 'h', 'a', 'g', 'd', 'b']"
1513+
"['h', 'b', 'e', 'g', 'a', 'd', 'f', 'c']"
15001514
]
15011515
},
15021516
"execution_count": null,
@@ -2006,8 +2020,8 @@
20062020
"name": "stdout",
20072021
"output_type": "stream",
20082022
"text": [
2009-
"Num Events: 7, Freq/sec: 333.8\n",
2010-
"Most recent: ▅▂▅▇▁ 318.4 288.8 312.8 352.0 246.1\n"
2023+
"Num Events: 7, Freq/sec: 315.8\n",
2024+
"Most recent: ▇▁▁▂▅ 423.6 331.5 332.6 361.3 399.7\n"
20112025
]
20122026
}
20132027
],

0 commit comments

Comments
 (0)