Skip to content

Commit

Permalink
Address integration test failures
Browse files Browse the repository at this point in the history
  • Loading branch information
Isaac-Flath committed Oct 2, 2024
1 parent cd0f097 commit 563cfcf
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 2 deletions.
4 changes: 3 additions & 1 deletion fastcore/basics.py
Original file line number Diff line number Diff line change
Expand Up @@ -716,9 +716,11 @@ def nested_attr(o, attr, default=None):
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): return default
except (AttributeError, KeyError, IndexError, TypeError):
return default
return o

# %% ../nbs/01_basics.ipynb
Expand Down
25 changes: 24 additions & 1 deletion nbs/01_basics.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -3903,6 +3903,13 @@
"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 @@ -3915,12 +3922,28 @@
" 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): return default\n",
" except (AttributeError, KeyError, IndexError, TypeError):\n",
" return default\n",
" return o"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"class TestObj:\n",
" def __init__(self): self.nested = {'key': [1, 2, {'inner': 'value'}]}\n",
"test_obj = TestObj()\n",
"\n",
"test_eq(nested_attr(test_obj, 'nested.key.2.inner'),'value')\n",
"test_eq(nested_attr([1, 2, 3], '1'),2)"
]
},
{
"cell_type": "code",
"execution_count": null,
Expand Down

0 comments on commit 563cfcf

Please sign in to comment.