Skip to content

Commit fd100fc

Browse files
author
Hamel Husain
authored
Merge pull request #243 from fastai/obj2dict
add obj2dict
2 parents a7d2984 + 6474bc9 commit fd100fc

File tree

4 files changed

+53
-7
lines changed

4 files changed

+53
-7
lines changed

fastcore/_nbdev.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,7 @@
134134
"read_config_file": "02_foundation.ipynb",
135135
"Config": "02_foundation.ipynb",
136136
"dict2obj": "03_xtras.ipynb",
137+
"obj2dict": "03_xtras.ipynb",
137138
"repr_dict": "03_xtras.ipynb",
138139
"AttrDict.__repr__": "03_xtras.ipynb",
139140
"is_listy": "03_xtras.ipynb",

fastcore/xtras.py

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
# AUTOGENERATED! DO NOT EDIT! File to edit: nbs/03_xtras.ipynb (unless otherwise specified).
22

3-
__all__ = ['dict2obj', 'repr_dict', 'is_listy', 'shufflish', 'mapped', 'IterLen', 'ReindexCollection', 'maybe_open',
4-
'image_size', 'bunzip', 'join_path_file', 'loads', 'untar_dir', 'repo_details', 'run', 'open_file',
5-
'save_pickle', 'load_pickle', 'stringfmt_names', 'PartialFormatter', 'partial_format', 'utc2local',
6-
'local2utc', 'trace', 'round_multiple', 'modified_env', 'ContextManagers', 'str2bool', 'sort_by_run']
3+
__all__ = ['dict2obj', 'obj2dict', 'repr_dict', 'is_listy', 'shufflish', 'mapped', 'IterLen', 'ReindexCollection',
4+
'maybe_open', 'image_size', 'bunzip', 'join_path_file', 'loads', 'untar_dir', 'repo_details', 'run',
5+
'open_file', 'save_pickle', 'load_pickle', 'stringfmt_names', 'PartialFormatter', 'partial_format',
6+
'utc2local', 'local2utc', 'trace', 'round_multiple', 'modified_env', 'ContextManagers', 'str2bool',
7+
'sort_by_run']
78

89
# Cell
910
from .imports import *
@@ -24,6 +25,13 @@ def dict2obj(d):
2425
if not isinstance(d, dict): return d
2526
return AttrDict(**{k:dict2obj(v) for k,v in d.items()})
2627

28+
# Cell
29+
def obj2dict(d):
30+
"Convert (possibly nested) AttrDicts (or lists of AttrDicts) to `dict`"
31+
if isinstance(d, (L,list)): return list(L(d).map(obj2dict))
32+
if not isinstance(d, dict): return d
33+
return dict(**{k:obj2dict(v) for k,v in d.items()})
34+
2735
# Cell
2836
def _repr_dict(d, lvl):
2937
if isinstance(d,dict):

nbs/02_foundation.ipynb

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -685,7 +685,7 @@
685685
{
686686
"data": {
687687
"text/plain": [
688-
"b'\\x80\\x03cinspect\\nSignature\\nq\\x00(cinspect\\nParameter\\nq\\x01X\\x05\\x00\\x00\\x00itemsq\\x02cinspect\\n_ParameterKind\\nq\\x03K\\x01\\x85q\\x04Rq\\x05\\x86q\\x06Rq\\x07}q\\x08(X\\x08\\x00\\x00\\x00_defaultq\\tNX\\x0b\\x00\\x00\\x00_annotationq\\ncinspect\\n_empty\\nq\\x0bubh\\x01X\\x04\\x00\\x00\\x00restq\\x0ch\\x03K\\x02\\x85q\\rRq\\x0e\\x86q\\x0fRq\\x10}q\\x11(h\\th\\x0bh\\nh\\x0bubh\\x01X\\x08\\x00\\x00\\x00use_listq\\x12h\\x03K\\x03\\x85q\\x13Rq\\x14\\x86q\\x15Rq\\x16}q\\x17(h\\t\\x89h\\nh\\x0bubh\\x01X\\x05\\x00\\x00\\x00matchq\\x18h\\x14\\x86q\\x19Rq\\x1a}q\\x1b(h\\tNh\\nh\\x0bubtq\\x1c\\x85q\\x1dRq\\x1e}q\\x1fX\\x12\\x00\\x00\\x00_return_annotationq h\\x0bsb.'"
688+
"b'\\x80\\x04\\x95\\x01\\x01\\x00\\x00\\x00\\x00\\x00\\x00\\x8c\\x07inspect\\x94\\x8c\\tSignature\\x94\\x93\\x94(h\\x00\\x8c\\tParameter\\x94\\x93\\x94\\x8c\\x05items\\x94h\\x00\\x8c\\x0e_ParameterKind\\x94\\x93\\x94K\\x01\\x85\\x94R\\x94\\x86\\x94R\\x94}\\x94(\\x8c\\x08_default\\x94N\\x8c\\x0b_annotation\\x94h\\x00\\x8c\\x06_empty\\x94\\x93\\x94ubh\\x04\\x8c\\x04rest\\x94h\\x07K\\x02\\x85\\x94R\\x94\\x86\\x94R\\x94}\\x94(h\\rh\\x10h\\x0eh\\x10ubh\\x04\\x8c\\x08use_list\\x94h\\x07K\\x03\\x85\\x94R\\x94\\x86\\x94R\\x94}\\x94(h\\r\\x89h\\x0eh\\x10ubh\\x04\\x8c\\x05match\\x94h\\x19\\x86\\x94R\\x94}\\x94(h\\rNh\\x0eh\\x10ubt\\x94\\x85\\x94R\\x94}\\x94\\x8c\\x12_return_annotation\\x94h\\x10sb.'"
689689
]
690690
},
691691
"execution_count": null,
@@ -818,7 +818,7 @@
818818
{
819819
"data": {
820820
"text/plain": [
821-
"['k', 1, 0]"
821+
"[10, 3, 7]"
822822
]
823823
},
824824
"execution_count": null,
@@ -860,6 +860,8 @@
860860
"t = L(arr, use_list=None)\n",
861861
"test_eq(t[1,2], arr[[1,2]])\n",
862862
"\n",
863+
"\n",
864+
"import pandas as pd\n",
863865
"df = pd.DataFrame({'a':[1,2,3]})\n",
864866
"t = L(df, use_list=None)\n",
865867
"test_eq(t[1,2], L(pd.DataFrame({'a':[2,3]}, index=[1,2]), use_list=None))"
@@ -1983,10 +1985,13 @@
19831985
"Converted 01_basics.ipynb.\n",
19841986
"Converted 02_foundation.ipynb.\n",
19851987
"Converted 03_xtras.ipynb.\n",
1988+
"Converted 03a_parallel.ipynb.\n",
1989+
"Converted 03b_net.ipynb.\n",
19861990
"Converted 04_dispatch.ipynb.\n",
19871991
"Converted 05_transform.ipynb.\n",
19881992
"Converted 07_meta.ipynb.\n",
19891993
"Converted 08_script.ipynb.\n",
1994+
"Converted Untitled1.ipynb.\n",
19901995
"Converted index.ipynb.\n"
19911996
]
19921997
}

nbs/03_xtras.ipynb

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,10 +101,42 @@
101101
"metadata": {},
102102
"outputs": [],
103103
"source": [
104-
"ds = dict2obj(L(d1, d1))\n",
104+
"_list_of_dicts = [d1, d1]\n",
105+
"ds = dict2obj(_list_of_dicts)\n",
105106
"test_eq(ds[0].b.c, 2)"
106107
]
107108
},
109+
{
110+
"cell_type": "code",
111+
"execution_count": null,
112+
"metadata": {},
113+
"outputs": [],
114+
"source": [
115+
"#export\n",
116+
"def obj2dict(d):\n",
117+
" \"Convert (possibly nested) AttrDicts (or lists of AttrDicts) to `dict`\"\n",
118+
" if isinstance(d, (L,list)): return list(L(d).map(obj2dict))\n",
119+
" if not isinstance(d, dict): return d\n",
120+
" return dict(**{k:obj2dict(v) for k,v in d.items()})"
121+
]
122+
},
123+
{
124+
"cell_type": "markdown",
125+
"metadata": {},
126+
"source": [
127+
"`obj2dict` can be used to reverse what is done by `dict2obj`:"
128+
]
129+
},
130+
{
131+
"cell_type": "code",
132+
"execution_count": null,
133+
"metadata": {},
134+
"outputs": [],
135+
"source": [
136+
"test_eq(obj2dict(d2), d1)\n",
137+
"test_eq(obj2dict(ds), _list_of_dicts) "
138+
]
139+
},
108140
{
109141
"cell_type": "code",
110142
"execution_count": null,

0 commit comments

Comments
 (0)