Skip to content

Commit

Permalink
add docs for pickle
Browse files Browse the repository at this point in the history
  • Loading branch information
samuelcolvin committed Jun 21, 2017
1 parent 02fef65 commit d9c31c7
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 0 deletions.
1 change: 1 addition & 0 deletions HISTORY.rst
Expand Up @@ -7,6 +7,7 @@ v0.3.0 (TBC)
............
* immutable models via ``config.allow_mutation = False``, associated cleanup and performance improvement #44
* immutable helper methods ``construct()`` and ``copy()`` #53
* allow pickling of models #53
* ``setattr`` is removed as ``__setattr__`` is now intelligent #44
* ``raise_exception`` removed, Models now always raise exceptions #44
* instance method validators removed TODO
Expand Down
20 changes: 20 additions & 0 deletions docs/examples/ex_pickle.py
@@ -0,0 +1,20 @@
import pickle
from pydantic import BaseModel


class FooBarModel(BaseModel):
a: str
b: int


m = FooBarModel(a='hello', b=123)
print(m)
# > FooBarModel a='hello' b=123

data = pickle.dumps(m)
print(data)
# > b'\x80\x03c...'

m2 = pickle.loads(data)
print(m2)
# > FooBarModel a='hello' b=123
7 changes: 7 additions & 0 deletions docs/index.rst
Expand Up @@ -231,6 +231,13 @@ is duplicated.

.. literalinclude:: examples/copy_values.py

Pickle
......

Using the same plumbing as ``copy()`` pydantic models support efficient pickling and unpicking.

.. literalinclude:: examples/ex_pickle.py

.. include:: ../HISTORY.rst


Expand Down
2 changes: 2 additions & 0 deletions docs/spelling_wordlist.txt
Expand Up @@ -5,6 +5,8 @@ config
doesn
dicts
django
faux
Faux
ints
jsonmodels
pydantic
Expand Down

0 comments on commit d9c31c7

Please sign in to comment.