Skip to content

Commit 59d0548

Browse files
committed
fixes #196
1 parent 0f1313e commit 59d0548

File tree

3 files changed

+96
-1
lines changed

3 files changed

+96
-1
lines changed

fastcore/_nbdev.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,7 @@
100100
"instantiate": "01_basics.ipynb",
101101
"using_attr": "01_basics.ipynb",
102102
"Self": "01_basics.ipynb",
103+
"Stateful": "01_basics.ipynb",
103104
"PrettyString": "01_basics.ipynb",
104105
"even_mults": "01_basics.ipynb",
105106
"num_cpus": "01_basics.ipynb",

fastcore/basics.py

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,8 @@
1010
'filter_values', 'cycle', 'zip_cycle', 'sorted_ex', 'negate_func', 'argwhere', 'filter_ex', 'range_of',
1111
'renumerate', 'first', 'nested_attr', 'nested_idx', 'num_methods', 'rnum_methods', 'inum_methods',
1212
'fastuple', 'arg0', 'arg1', 'arg2', 'arg3', 'arg4', 'bind', 'map_ex', 'compose', 'maps', 'partialler',
13-
'instantiate', 'using_attr', 'Self', 'Self', 'PrettyString', 'even_mults', 'num_cpus', 'add_props', 'typed']
13+
'instantiate', 'using_attr', 'Self', 'Self', 'Stateful', 'PrettyString', 'even_mults', 'num_cpus',
14+
'add_props', 'typed']
1415

1516
# Cell
1617
from .imports import *
@@ -660,6 +661,26 @@ def __call__(self,*args,**kwargs): return self.__getattr__('_call')(*args,**kwar
660661
# Cell
661662
#nbdev_comment _all_ = ['Self']
662663

664+
# Cell
665+
class Stateful:
666+
"A base class/mixin for objects that should not serialize all their state"
667+
_stateattrs=()
668+
def __init__(self,*args,**kwargs):
669+
self._init_state()
670+
super().__init__(*args,**kwargs)
671+
672+
def __getstate__(self):
673+
return {k:v for k,v in self.__dict__.items()
674+
if k not in self._stateattrs+('_state',)}
675+
676+
def __setstate__(self, state):
677+
self.__dict__.update(state)
678+
self._init_state()
679+
680+
def _init_state(self):
681+
"Override for custom deserialization logic"
682+
self._state = {}
683+
663684
# Cell
664685
class PrettyString(str):
665686
"Little hack to get strings to show properly in Jupyter."

nbs/01_basics.ipynb

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3531,6 +3531,79 @@
35313531
"## Other Helpers"
35323532
]
35333533
},
3534+
{
3535+
"cell_type": "code",
3536+
"execution_count": null,
3537+
"metadata": {},
3538+
"outputs": [],
3539+
"source": [
3540+
"#export\n",
3541+
"class Stateful:\n",
3542+
" \"A base class/mixin for objects that should not serialize all their state\"\n",
3543+
" _stateattrs=()\n",
3544+
" def __init__(self,*args,**kwargs):\n",
3545+
" self._init_state()\n",
3546+
" super().__init__(*args,**kwargs)\n",
3547+
" \n",
3548+
" def __getstate__(self):\n",
3549+
" return {k:v for k,v in self.__dict__.items()\n",
3550+
" if k not in self._stateattrs+('_state',)}\n",
3551+
" \n",
3552+
" def __setstate__(self, state):\n",
3553+
" self.__dict__.update(state)\n",
3554+
" self._init_state()\n",
3555+
"\n",
3556+
" def _init_state(self):\n",
3557+
" \"Override for custom deserialization logic\"\n",
3558+
" self._state = {}"
3559+
]
3560+
},
3561+
{
3562+
"cell_type": "code",
3563+
"execution_count": null,
3564+
"metadata": {},
3565+
"outputs": [
3566+
{
3567+
"data": {
3568+
"text/markdown": [
3569+
"<h4 id=\"Stateful\" class=\"doc_header\"><code>class</code> <code>Stateful</code><a href=\"\" class=\"source_link\" style=\"float:right\">[source]</a></h4>\n",
3570+
"\n",
3571+
"> <code>Stateful</code>(**\\*`args`**, **\\*\\*`kwargs`**)\n",
3572+
"\n",
3573+
"A base class/mixin for objects that should not serialize all their state"
3574+
],
3575+
"text/plain": [
3576+
"<IPython.core.display.Markdown object>"
3577+
]
3578+
},
3579+
"metadata": {},
3580+
"output_type": "display_data"
3581+
}
3582+
],
3583+
"source": [
3584+
"show_doc(Stateful, title_level=4)"
3585+
]
3586+
},
3587+
{
3588+
"cell_type": "code",
3589+
"execution_count": null,
3590+
"metadata": {},
3591+
"outputs": [],
3592+
"source": [
3593+
"class _T(Stateful):\n",
3594+
" def __init__(self):\n",
3595+
" super().__init__()\n",
3596+
" self.a=1\n",
3597+
" self._state['test']=2\n",
3598+
"\n",
3599+
"t = _T()\n",
3600+
"t2 = pickle.loads(pickle.dumps(t))\n",
3601+
"test_eq(t.a,1)\n",
3602+
"test_eq(t._state['test'],2)\n",
3603+
"test_eq(t2.a,1)\n",
3604+
"test_eq(t2._state,{})"
3605+
]
3606+
},
35343607
{
35353608
"cell_type": "code",
35363609
"execution_count": null,

0 commit comments

Comments
 (0)