Skip to content

Commit 7f909e2

Browse files
committed
Stateful example
1 parent de83082 commit 7f909e2

File tree

4 files changed

+60
-6
lines changed

4 files changed

+60
-6
lines changed

fastcore/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
__version__ = "1.3.5"
1+
__version__ = "1.3.6"

fastcore/basics.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -667,7 +667,7 @@ class Stateful:
667667
_stateattrs=()
668668
def __init__(self,*args,**kwargs):
669669
self._init_state()
670-
super().__init__(*args,**kwargs)
670+
super().__init__(*args,**kwargs) # required for mixin usage
671671

672672
def __getstate__(self):
673673
return {k:v for k,v in self.__dict__.items()
@@ -678,7 +678,7 @@ def __setstate__(self, state):
678678
self._init_state()
679679

680680
def _init_state(self):
681-
"Override for custom deserialization logic"
681+
"Override for custom init and deserialization logic"
682682
self._state = {}
683683

684684
# Cell

nbs/01_basics.ipynb

Lines changed: 56 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3543,7 +3543,7 @@
35433543
" _stateattrs=()\n",
35443544
" def __init__(self,*args,**kwargs):\n",
35453545
" self._init_state()\n",
3546-
" super().__init__(*args,**kwargs)\n",
3546+
" super().__init__(*args,**kwargs) # required for mixin usage\n",
35473547
" \n",
35483548
" def __getstate__(self):\n",
35493549
" return {k:v for k,v in self.__dict__.items()\n",
@@ -3554,7 +3554,7 @@
35543554
" self._init_state()\n",
35553555
"\n",
35563556
" def _init_state(self):\n",
3557-
" \"Override for custom deserialization logic\"\n",
3557+
" \"Override for custom init and deserialization logic\"\n",
35583558
" self._state = {}"
35593559
]
35603560
},
@@ -3604,6 +3604,60 @@
36043604
"test_eq(t2._state,{})"
36053605
]
36063606
},
3607+
{
3608+
"cell_type": "markdown",
3609+
"metadata": {},
3610+
"source": [
3611+
"Override `_init_state` to do any necessary setup steps that are required during `__init__` or during deserialization (e.g. `pickle.load`). Here's an example of how `Stateful` simplifies the official Python example for [Handling Stateful Objects](https://docs.python.org/3/library/pickle.html#handling-stateful-objects)."
3612+
]
3613+
},
3614+
{
3615+
"cell_type": "code",
3616+
"execution_count": null,
3617+
"metadata": {},
3618+
"outputs": [],
3619+
"source": [
3620+
"class TextReader(Stateful):\n",
3621+
" \"\"\"Print and number lines in a text file.\"\"\"\n",
3622+
" _stateattrs=('file',)\n",
3623+
" def __init__(self, filename):\n",
3624+
" self.filename,self.lineno = filename,0\n",
3625+
" super().__init__()\n",
3626+
"\n",
3627+
" def readline(self):\n",
3628+
" self.lineno += 1\n",
3629+
" line = self.file.readline()\n",
3630+
" if line: return f\"{self.lineno}: {line.strip()}\"\n",
3631+
"\n",
3632+
" def _init_state(self):\n",
3633+
" self.file = open(self.filename)\n",
3634+
" for _ in range(self.lineno): self.file.readline()"
3635+
]
3636+
},
3637+
{
3638+
"cell_type": "code",
3639+
"execution_count": null,
3640+
"metadata": {},
3641+
"outputs": [
3642+
{
3643+
"name": "stdout",
3644+
"output_type": "stream",
3645+
"text": [
3646+
"1: {\n",
3647+
"2: \"cells\": [\n",
3648+
"3: {\n"
3649+
]
3650+
}
3651+
],
3652+
"source": [
3653+
"reader = TextReader(\"00_test.ipynb\")\n",
3654+
"print(reader.readline())\n",
3655+
"print(reader.readline())\n",
3656+
"\n",
3657+
"new_reader = pickle.loads(pickle.dumps(reader))\n",
3658+
"print(reader.readline())"
3659+
]
3660+
},
36073661
{
36083662
"cell_type": "code",
36093663
"execution_count": null,

settings.ini

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ author = Jeremy Howard and Sylvain Gugger
77
author_email = [email protected]
88
copyright = fast.ai
99
branch = master
10-
version = 1.3.5
10+
version = 1.3.6
1111
min_python = 3.6
1212
audience = Developers
1313
language = English

0 commit comments

Comments
 (0)