Skip to content

Commit

Permalink
fixes #633
Browse files Browse the repository at this point in the history
  • Loading branch information
jph00 committed Oct 4, 2024
1 parent 2164542 commit 49109db
Show file tree
Hide file tree
Showing 2 changed files with 73 additions and 23 deletions.
4 changes: 2 additions & 2 deletions fastcore/basics.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,8 @@ def basic_repr(flds=None):
flds = list(flds or [])
def _f(self):
res = f'{type(self).__module__}.{type(self).__name__}'
if not flds: return f'<{res}>'
sig = ', '.join(f'{o}={getattr(self,o)!r}' for o in flds)
fs = flds if flds else [o for o in vars(self) if not o.startswith('_')]
sig = ', '.join(f'{o}={getattr(self,o)!r}' for o in fs)
return f'{res}({sig})'
return _f

Expand Down
92 changes: 71 additions & 21 deletions nbs/01_basics.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
"cells": [
{
"cell_type": "code",
"execution_count": null,
"execution_count": 1,
"metadata": {},
"outputs": [],
"source": [
Expand All @@ -11,7 +11,7 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 2,
"metadata": {},
"outputs": [],
"source": [
Expand All @@ -26,7 +26,7 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 3,
"metadata": {},
"outputs": [],
"source": [
Expand Down Expand Up @@ -55,7 +55,7 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 4,
"metadata": {},
"outputs": [],
"source": [
Expand All @@ -65,7 +65,7 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 5,
"metadata": {},
"outputs": [],
"source": [
Expand All @@ -84,7 +84,7 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 6,
"metadata": {},
"outputs": [],
"source": [
Expand All @@ -94,7 +94,7 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 7,
"metadata": {},
"outputs": [],
"source": [
Expand All @@ -113,7 +113,7 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 8,
"metadata": {},
"outputs": [],
"source": [
Expand All @@ -125,7 +125,7 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 32,
"metadata": {},
"outputs": [],
"source": [
Expand All @@ -136,8 +136,8 @@
" flds = list(flds or [])\n",
" def _f(self):\n",
" res = f'{type(self).__module__}.{type(self).__name__}'\n",
" if not flds: return f'<{res}>'\n",
" sig = ', '.join(f'{o}={getattr(self,o)!r}' for o in flds)\n",
" fs = flds if flds else [o for o in vars(self) if not o.startswith('_')]\n",
" sig = ', '.join(f'{o}={getattr(self,o)!r}' for o in fs)\n",
" return f'{res}({sig})'\n",
" return _f"
]
Expand All @@ -151,16 +151,16 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 33,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"'<__main__.SomeClass>'"
"'__main__.SomeClass()'"
]
},
"execution_count": null,
"execution_count": 33,
"metadata": {},
"output_type": "execute_result"
}
Expand All @@ -179,7 +179,7 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 34,
"metadata": {},
"outputs": [
{
Expand All @@ -188,7 +188,7 @@
"\"__main__.SomeClass(a=1, b='foo')\""
]
},
"execution_count": null,
"execution_count": 34,
"metadata": {},
"output_type": "execute_result"
}
Expand All @@ -203,9 +203,16 @@
"repr(SomeClass())"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Nested objects work too:"
]
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 35,
"metadata": {},
"outputs": [
{
Expand All @@ -214,7 +221,7 @@
"\"__main__.AnotherClass(c=__main__.SomeClass(a=1, b='foo'), d='bar')\""
]
},
"execution_count": null,
"execution_count": 35,
"metadata": {},
"output_type": "execute_result"
}
Expand All @@ -228,9 +235,40 @@
"repr(AnotherClass())"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Instance variables (but not class variables) are shown if `basic_repr` is called with no arguments:"
]
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 36,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"\"__main__.SomeClass(a=1, b='foo')\""
]
},
"execution_count": 36,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"class SomeClass:\n",
" def __init__(self, a=1, b='foo'): self.a,self.b = a,b\n",
" __repr__=basic_repr()\n",
"\n",
"repr(SomeClass())"
]
},
{
"cell_type": "code",
"execution_count": 37,
"metadata": {},
"outputs": [],
"source": [
Expand Down Expand Up @@ -6661,7 +6699,7 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 38,
"metadata": {},
"outputs": [],
"source": [
Expand All @@ -6682,9 +6720,21 @@
"split_at_heading": true
},
"kernelspec": {
"display_name": "python3",
"display_name": "Python 3 (ipykernel)",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.11.8"
}
},
"nbformat": 4,
Expand Down

0 comments on commit 49109db

Please sign in to comment.