Skip to content

Commit

Permalink
Bug fix for keys
Browse files Browse the repository at this point in the history
  • Loading branch information
Isaac-Flath committed Oct 2, 2024
1 parent 563cfcf commit dab92e3
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 31 deletions.
10 changes: 4 additions & 6 deletions fastcore/basics.py
Original file line number Diff line number Diff line change
Expand Up @@ -714,12 +714,10 @@ def only(o):
def nested_attr(o, attr, default=None):
"Same as `getattr`, but if `attr` includes a `.`, then looks inside nested objects"
try:
for a in attr.split("."):
if hasattr(o, a): o = getattr(o, a)
elif isinstance(o, (list, tuple, dict)) and a.isdigit():o = o[int(a)]
elif hasattr(o, '__getitem__'): o = o[a]
else: return default
except (AttributeError, KeyError, IndexError, TypeError):
for a in attr.split("."):
try: o = getattr(o, a)
except AttributeError: o = o[a]
except (KeyError,IndexError, TypeError, ValueError):
return default
return o

Expand Down
44 changes: 19 additions & 25 deletions nbs/01_basics.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -3903,13 +3903,6 @@
"test_fail(lambda: only([0,1]), contains='iterable has more than 1 item')"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
},
{
"cell_type": "code",
"execution_count": null,
Expand All @@ -3920,12 +3913,10 @@
"def nested_attr(o, attr, default=None):\n",
" \"Same as `getattr`, but if `attr` includes a `.`, then looks inside nested objects\"\n",
" try:\n",
" for a in attr.split(\".\"):\n",
" if hasattr(o, a): o = getattr(o, a)\n",
" elif isinstance(o, (list, tuple, dict)) and a.isdigit():o = o[int(a)]\n",
" elif hasattr(o, '__getitem__'): o = o[a]\n",
" else: return default\n",
" except (AttributeError, KeyError, IndexError, TypeError):\n",
" for a in attr.split(\".\"): \n",
" try: o = getattr(o, a)\n",
" except AttributeError: o = o[a]\n",
" except (KeyError,IndexError, TypeError, ValueError):\n",
" return default\n",
" return o"
]
Expand All @@ -3935,6 +3926,20 @@
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"class CustomIndexable:\n",
" def __init__(self): self.data = {'a':1,'b':'v','c':{'d':5}}\n",
" def __getitem__(self, key): return self.data[key]\n",
"\n",
"custom_indexable = CustomIndexable()\n",
"test_eq(nested_attr(custom_indexable,'a'),1)\n",
"test_eq(nested_attr(custom_indexable,'c.d'),5)\n",
"test_eq(nested_attr(custom_indexable,'e'),None)"
]
},
{
"cell_type": "raw",
"metadata": {},
"source": [
"class TestObj:\n",
" def __init__(self): self.nested = {'key': [1, 2, {'inner': 'value'}]}\n",
Expand Down Expand Up @@ -3973,6 +3978,7 @@
"metadata": {},
"outputs": [],
"source": [
"#|hide\n",
"class CustomIndexable:\n",
" def __init__(self): self.data = {'a':1,'b':'v','c':{'d':5}}\n",
" def __getitem__(self, key): return self.data[key]\n",
Expand Down Expand Up @@ -4009,18 +4015,6 @@
"test_eq(o, {'a':{'b':{'c':'d'}},'e':'f'})"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"#|hide\n",
"o = {'a':'b'}\n",
"test_eq(nested_setdefault(o, 'a', 'c'), 'b')\n",
"test_eq(o, {'a':'b'})"
]
},
{
"cell_type": "code",
"execution_count": null,
Expand Down

0 comments on commit dab92e3

Please sign in to comment.