SlideShare a Scribd company logo
PyCon Korea 2019
Pickle & Custom Binary Serializer
Young Seok Tony Kim
(김영석)
About the speaker
www.aitrics.com
contact@aitrics.com
Soyoung Yoon <lovelife@kaist.ac.kr>
● Software Engineer
● Machine Learning Research Engineer
Master’s degree in Data Science
!3
Motivation
• Understand the general idea how pickle works internally

• Know when is the best to use pickle, when to use other serialization methods

• Build a simple custom serializer
!4
Objective of this talk
!5
What is Pickle? What is serialization?
0110000101101100 …
Serialization De-serialization
Python object Python object
!6
What is Pickle? What is serialization?
0110000101101100 …
Serialization De-serialization
Python object Python object
pickle.dump()

pickle.dumps()
pickle.load()

pickle.loads()
!7
What is Pickle? What is serialization?
0110000101101100 …
Serialization De-serialization
Python object Python object
pickle.dump()

pickle.dumps()
pickle.load()

pickle.loads()
Pickle module
!8
Other serialization methods
JSON example from Wikipedia
XML example from W3schools
Protobuf example from Google
!9
Other serialization methods
Pickle JSON Protobuf MessagePack
Human-readable
No

(except protocol 0)
Yes No No
Python-specific Yes No No No
User-defined class Yes No No No
!10
Pickle API
pickle.dump(obj, file, protocol=None, *, fix_imports=True)
pickle.dumps(obj, protocol=None, *, fix_imports=True)
pickle.load(file, *, fix_imports=True, encoding=“ASCII”, errors=“strict”)
pickle.loads(bytes_object, *, fix_imports=True, encoding="ASCII", errors="strict")
!11
Pickle API
pickle.dump(obj, file, protocol=None, *, fix_imports=True)
pickle.dumps(obj, protocol=None, *, fix_imports=True)
pickle.load(file, *, fix_imports=True, encoding=“ASCII”, errors=“strict”)
pickle.loads(bytes_object, *, fix_imports=True, encoding="ASCII", errors="strict")
Python object
10110001
01110111
010111…
File
Writes
!12
Pickle API
pickle.dump(obj, file, protocol=None, *, fix_imports=True)
pickle.dumps(obj, protocol=None, *, fix_imports=True)
pickle.load(file, *, fix_imports=True, encoding=“ASCII”, errors=“strict”)
pickle.loads(bytes_object, *, fix_imports=True, encoding="ASCII", errors="strict")
Python object
Returns
b'x80x03Kn.'
Bytes
!13
Pickle API
pickle.dump(obj, file, protocol=None, *, fix_imports=True)
pickle.dumps(obj, protocol=None, *, fix_imports=True)
pickle.load(file, *, fix_imports=True, encoding=“ASCII”, errors=“strict”)
pickle.loads(bytes_object, *, fix_imports=True, encoding="ASCII", errors="strict")
Python object
10110001
01110111
010111…
File
Reads
!14
Pickle API
pickle.dump(obj, file, protocol=None, *, fix_imports=True)
pickle.dumps(obj, protocol=None, *, fix_imports=True)
pickle.load(file, *, fix_imports=True, encoding=“ASCII”, errors=“strict”)
pickle.loads(bytes_object, *, fix_imports=True, encoding="ASCII", errors="strict")
Python object
Returns
b'x80x03Kn.'
Bytes
!15
Pickle API
pickle.dump(obj, file, protocol=None, *, fix_imports=True)
pickle.dumps(obj, protocol=None, *, fix_imports=True)
pickle.load(file, *, fix_imports=True, encoding=“ASCII”, errors=“strict”)
pickle.loads(bytes_object, *, fix_imports=True, encoding="ASCII", errors="strict")
!16
Pickle API
pickle.dump(obj, file)
pickle.load(file)
!17
Pickle Protocols
Protocol #
#number
0 1 2 3 4 5
Introduced in - - 2.3 3.0 3.4 3.8
Relevant PEP - - PEP 307
What’s new in
Python 3.0
PEP 3154 PEP 574
What’s 

added
human-readable,
original protocol old binary format
provides 

much more
efficient pickling
of new-style
classes.
It has explicit
support for bytes
objects
adds support for
very large
objects, pickling
more kinds of
objects, and
some data format
optimizations
supports

out-of-band 

data
Note
was called

“text mode”
was called 

“binary mode”
Cannot be
unpickled by
Python 2.x
Support for
Unicode
• Pickle protocol opcodes never changes. Only the new ones are introduced.

• This make sure old pickles continue to be readable forever

• If older unpickler tries to read a pickle generated by newer protocol, it will
either

• Work well, if the newer protocol does not use higher protocol opcode.

• Explicitly give you an error, by raising PicklingError exception
!18
Pickle Protocols
Opcode examples
Opcode Name Opcode (Byte)
INT I
LONG L
LONG1 x8a
BININT J
BININT1 K
STRING S
NONE N
NEWTRUE x88
NEWFALSE x89
… …
!20
Protocol 0
!21
LONG (0)
‘L’ Before and After the decimal value
!22
LONG (0)
HEX 4C 33 4C 0A 2E
ASCII L 3 L n .
INT 76 51 76 10 46
‘L’ Before and After the decimal value
!23
LONG (0)
HEX 4C 33 4C 0A 2E
ASCII L 3 L n .
INT 76 51 76 10 46
‘L’ Before and After the decimal value
!24
LONG (0)
HEX 4C 33 4C 0A 2E
ASCII L 3 L n .
INT 76 51 76 10 46
‘L’ Before and After the decimal value
!25
LONG (0)
HEX 4C 33 4C 0A 2E
ASCII L 3 L n .
INT 76 51 76 10 46
‘L’ Before and After the decimal value
!26
LONG (0)
!27
LONG (0)
HEX 4C 31 32 33 34 35 36 37 38 39 30 … 4C 0A 2E
ASCII L 1 2 3 4 5 6 7 8 9 0 … L n .
INT 76 49 50 51 52 53 54 55 56 57 48 … 76 10 46
Takes quadratic in the number of digits when unpickling
!28
LONG (0)
HEX 4C 31 32 33 34 35 36 37 38 39 30 … 4C 0A 2E
ASCII L 1 2 3 4 5 6 7 8 9 0 … L n .
INT 76 49 50 51 52 53 54 55 56 57 48 … 76 10 46
!29
FLOAT (0)
HEX 46 33 2E 31 34 0A 2E
ASCII F 3 . 1 4 n .
INT 70 51 46 49 52 10 46
!30
FLOAT (0)
HEX 46 33 2E 31 34 0A 2E
ASCII F 3 . 1 4 n .
INT 70 51 46 49 52 10 46
tuple
!32
TUPLE (0)
!33
TUPLE (0)
HEX 28 4C 30 4C 0A 4C 31 4C 0A 4C 32 4C 0A 74 70 30 0A 2E
ASCII ( L 0 L n L 1 L n L 2 L n t p 0 n .
INT 40 76 48 76 10 76 49 76 10 76 50 76 10 116 112 48 10 46
!34
TUPLE (0)
HEX 28 4C 30 4C 0A 4C 31 4C 0A 4C 32 4C 0A 74 70 30 0A 2E
ASCII ( L 0 L n L 1 L n L 2 L n t p 0 n .
INT 40 76 48 76 10 76 49 76 10 76 50 76 10 116 112 48 10 46
MARK
MARK
Stack
!35
TUPLE (0)
HEX 28 4C 30 4C 0A 4C 31 4C 0A 4C 32 4C 0A 74 70 30 0A 2E
ASCII ( L 0 L n L 1 L n L 2 L n t p 0 n .
INT 40 76 48 76 10 76 49 76 10 76 50 76 10 116 112 48 10 46
0
0
MARK
Stack
!36
TUPLE (0)
HEX 28 4C 30 4C 0A 4C 31 4C 0A 4C 32 4C 0A 74 70 30 0A 2E
ASCII ( L 0 L n L 1 L n L 2 L n t p 0 n .
INT 40 76 48 76 10 76 49 76 10 76 50 76 10 116 112 48 10 46
1
1
0
MARK
Stack
!37
TUPLE (0)
HEX 28 4C 30 4C 0A 4C 31 4C 0A 4C 32 4C 0A 74 70 30 0A 2E
ASCII ( L 0 L n L 1 L n L 2 L n t p 0 n .
INT 40 76 48 76 10 76 49 76 10 76 50 76 10 116 112 48 10 46
2 2
1
0
MARK
Stack
!38
TUPLE (0)
HEX 28 4C 30 4C 0A 4C 31 4C 0A 4C 32 4C 0A 74 70 30 0A 2E
ASCII ( L 0 L n L 1 L n L 2 L n t p 0 n .
INT 40 76 48 76 10 76 49 76 10 76 50 76 10 116 112 48 10 46
TUPLE
(0,1,2)
Stack
!39
TUPLE (0)
HEX 28 4C 30 4C 0A 4C 31 4C 0A 4C 32 4C 0A 74 70 30 0A 2E
ASCII ( L 0 L n L 1 L n L 2 L n t p 0 n .
INT 40 76 48 76 10 76 49 76 10 76 50 76 10 116 112 48 10 46
PUT
(0,1,2)
Stack
Index 0 1 2 3 4 5
Value (0,1,2)
Memo
list
!41
LIST (0)
!42
LIST (0)
HEX 28 6C 70 30 0A 4C 30 4C 0A 61 4C 31 4C 0A 61 67 30 0A 61 2E
ASCII ( l p 0 n L 0 L n a L 1 L n a g 0 n a .
INT 40 108 112 48 10 76 48 76 10 97 76 49 76 10 97 103 48 10 97 46
!43
LIST (0)
HEX 28 6C 70 30 0A 4C 30 4C 0A 61 4C 31 4C 0A 61 67 30 0A 61 2E
ASCII ( l p 0 n L 0 L n a L 1 L n a g 0 n a .
INT 40 108 112 48 10 76 48 76 10 97 76 49 76 10 97 103 48 10 97 46
Index 0 1 2 3 4 5
Value
Memo
MARK
Stack
MARK
!44
LIST (0)
HEX 28 6C 70 30 0A 4C 30 4C 0A 61 4C 31 4C 0A 61 67 30 0A 61 2E
ASCII ( l p 0 n L 0 L n a L 1 L n a g 0 n a .
INT 40 108 112 48 10 76 48 76 10 97 76 49 76 10 97 103 48 10 97 46
Index 0 1 2 3 4 5
Value
Memo
list1
Stack
LIST
list1
!45
LIST (0)
HEX 28 6C 70 30 0A 4C 30 4C 0A 61 4C 31 4C 0A 61 67 30 0A 61 2E
ASCII ( l p 0 n L 0 L n a L 1 L n a g 0 n a .
INT 40 108 112 48 10 76 48 76 10 97 76 49 76 10 97 103 48 10 97 46
Index 0 1 2 3 4 5
Value list1
Memo
list1
Stack
PUT
list1
!46
LIST (0)
HEX 28 6C 70 30 0A 4C 30 4C 0A 61 4C 31 4C 0A 61 67 30 0A 61 2E
ASCII ( l p 0 n L 0 L n a L 1 L n a g 0 n a .
INT 40 108 112 48 10 76 48 76 10 97 76 49 76 10 97 103 48 10 97 46
Index 0 1 2 3 4 5
Value list1
Memo
0
list1
Stack
list1
!47
LIST (0)
HEX 28 6C 70 30 0A 4C 30 4C 0A 61 4C 31 4C 0A 61 67 30 0A 61 2E
ASCII ( l p 0 n L 0 L n a L 1 L n a g 0 n a .
INT 40 108 112 48 10 76 48 76 10 97 76 49 76 10 97 103 48 10 97 46
Index 0 1 2 3 4 5
Value list1
Memo
list1
Stack
APPEND
0list1
!48
LIST (0)
HEX 28 6C 70 30 0A 4C 30 4C 0A 61 4C 31 4C 0A 61 67 30 0A 61 2E
ASCII ( l p 0 n L 0 L n a L 1 L n a g 0 n a .
INT 40 108 112 48 10 76 48 76 10 97 76 49 76 10 97 103 48 10 97 46
Index 0 1 2 3 4 5
Value list1
Memo
1
list1
Stack
0list1
!49
LIST (0)
HEX 28 6C 70 30 0A 4C 30 4C 0A 61 4C 31 4C 0A 61 67 30 0A 61 2E
ASCII ( l p 0 n L 0 L n a L 1 L n a g 0 n a .
INT 40 108 112 48 10 76 48 76 10 97 76 49 76 10 97 103 48 10 97 46
Index 0 1 2 3 4 5
Value list1
Memo
list1
Stack
APPEND
0 1list1
!50
LIST (0)
HEX 28 6C 70 30 0A 4C 30 4C 0A 61 4C 31 4C 0A 61 67 30 0A 61 2E
ASCII ( l p 0 n L 0 L n a L 1 L n a g 0 n a .
INT 40 108 112 48 10 76 48 76 10 97 76 49 76 10 97 103 48 10 97 46
Index 0 1 2 3 4 5
Value list1
Memo
list1
list1
Stack
GET
0 1list1
!51
LIST (0)
HEX 28 6C 70 30 0A 4C 30 4C 0A 61 4C 31 4C 0A 61 67 30 0A 61 2E
ASCII ( l p 0 n L 0 L n a L 1 L n a g 0 n a .
INT 40 108 112 48 10 76 48 76 10 97 76 49 76 10 97 103 48 10 97 46
Index 0 1 2 3 4 5
Value list1
Memo
list1
Stack
APPEND
0 1 list1list1
Protocol 1
!53
BININT2 (1)
!54
BININT2 (1)
HEX 4D 00 01 2E
ASCII M x00 x01 .
INT 76 49 50 51
BININT2
Protocol 2
!56
NEWOBJ (2)
!57
NEWOBJ (2)
HEX
ASCII
INT
!58
PyCon
HEX
ASCII
INT
Picture from http://rahmonov.me/posts/python-decorators/
• Official doc: https://docs.python.org/3/library/pickletools.html

• Usage
!59
pickletools
!60
pickletools
!61
NEWOBJ (2)
PROTO NEWOBJ
Protocol 3
SHORT_BINBYTES (3)
Protocol 4
!65
SHORT_BINUNICODE (4)
Protocol 5
• New pickle protocol 5 can send extra metadata needed for out-of-band data
buffers.

• There is a new PickleBuffer to return out-of-band data buffers.

• Reduces unnecessary memory copies.
!67
PEP 574 -- Pickle protocol 5 with 

out-of-band data
!68
PEP 574 -- Pickle protocol 5 with 

out-of-band data
https://github.com/numpy/numpy/issues/11161
• I was implementing an ICLR 2016 paper - 

“Deep Compression: Compressing Deep Neural Networks with Pruning,
Trained Quantization and Huffman Coding” 

by Song Han, Huizi Mao, William J. Dally

• This paper was about compressing Neural Network.

• For example, compressing AlexNet from 233MB to 8.9MB 

without loss of accuracy

• The official repository did not contain Huffman Coding
!69
Custom Binary Serializer
!70
Custom Binary Serializer
Your custom object
Custom serialization logic
bytearray object
1A FF 8B 9C
!71
Custom Binary Serializer
Quantized
sparse

numpy array
Your custom object
Custom serialization logic
bytearray object
1A FF 8B 9C
!72
Custom Binary Serializer
Quantized
sparse

numpy array
Your custom object
Custom
serialization
logic
bytearray object
1A FF 8B 9C
Binary string
to
“0110110101010101000…”
!73
Custom Binary Serializer
bytearray object
1A FF 8B 9C
Binary string
to
“0110110101010101000…”
• struct module performs conversions between Python values and C structs
represented as Python bytes objects.
!74
struct module
Python object
bytes object
1A FF 8B 9C
struct.pack()
struct.unpack()
!75
struct module
https://docs.python.org/3/library/struct.html
• What is difference between cPickle and pickle?

• What can be pickled? What cannot pickled?

• When should we use pickle?
!76
FAQ
What is the difference between cPickle and pickle?
• _pickle is a pickle module implemented in C language. 

It is faster than Python implementation.

• _pickle was known as cPickle in Python 2. 

• In Python 2, cPickle and pickle modules were separate.

• In Python 3, pickle module imports and uses _pickle, if available. 

Otherwise, it uses python implementation.

• TLDR; Use Python 3, you don’t need to worry about anything.
• None, True, and False

• integers, floating point numbers,
complex numbers

• strings, bytes, bytearrays

• tuples, lists, sets, and dictionaries
containing only picklable objects

• Classes, functions 

(both built-in and user-defined)
defined at the top level of a module
(using def, not lambda)

• Instances of such classes whose
__dict__ or the result of calling
__getstate__() is picklable (see
section Pickling Class Instances for
details).
!78
What can be pickled and unpickled?
!79
Can a function be pickled?
10110001
01110111
010111…
function.pickle
File
!80
Can a function be pickled?
10110001
01110111
010111…
function.pickle
File
!81
Can a function be pickled?
10110001
01110111
010111…
function.pickle
File
• Only the function’s name is pickled, along with the name of the module the
function is defined in.

• Functions are pickled by name reference, not by value. This means you
cannot pickle lambda functions.

• Similarly, classes, methods, decorators (which is also a function) are pickled
by reference, not by the value
!82
Function content is not pickled
• Pickle is probably most useful when you want to quickly store python objects to
restore later.

• JSON is more readable, simple, and cross-platform

• Protobuf is good for its performance, especially for network communication

• messagepack is an alternative for JSON, when you want faster and smaller
serialization

• Only the function’s name is pickled, along with the name of the module the function is
defined in. A function’s content is NOT pickled.

• When building a custom serializer, I recommend looking struct module in Python
!83
Summary / Takeaways
References
• Official Python document

• https://docs.python.org/3/library/pickle.html

• CPython Source code 

(Great content. Extensive comments here are quite easy to understand)

• https://github.com/python/cpython/blob/master/Lib/pickle.py

• https://github.com/python/cpython/blob/master/Lib/pickletools.py
!85
pickle.py
(from cPython implementation)
• 파이썬 웹서버 REST API 문서 쉽고 빠르게 작성하기 (Yongseon Lee) 

18th (Sun) 11:55 ~ 12:35
• Advanced Python testing techniques (Jaeman An)

18th (Sun) 11:55 ~ 12:35
• 실시간 의료 인공지능 데이터 처리를 위한 Django Query Optimization (Soyoung Yoon)

18th (Sun) 13:55 ~ 14:35
• Pickle & Custom Binary Serializer (Young Seok Kim)

18th (Sun) 14:55 ~ 15:35
Talks from AITRICS
www.aitrics.com
contact@aitrics.com
Thank
Soyoung Yoon <lovelife@kaist.ac.kr>
We are Hiring!
● Software Engineer
● Machine Learning Researc

More Related Content

Recently uploaded

Uni Systems Copilot event_05062024_C.Vlachos.pdf
Uni Systems Copilot event_05062024_C.Vlachos.pdfUni Systems Copilot event_05062024_C.Vlachos.pdf
Uni Systems Copilot event_05062024_C.Vlachos.pdf
Uni Systems S.M.S.A.
 
20240605 QFM017 Machine Intelligence Reading List May 2024
20240605 QFM017 Machine Intelligence Reading List May 202420240605 QFM017 Machine Intelligence Reading List May 2024
20240605 QFM017 Machine Intelligence Reading List May 2024
Matthew Sinclair
 
GraphSummit Singapore | The Art of the Possible with Graph - Q2 2024
GraphSummit Singapore | The Art of the  Possible with Graph - Q2 2024GraphSummit Singapore | The Art of the  Possible with Graph - Q2 2024
GraphSummit Singapore | The Art of the Possible with Graph - Q2 2024
Neo4j
 
20240607 QFM018 Elixir Reading List May 2024
20240607 QFM018 Elixir Reading List May 202420240607 QFM018 Elixir Reading List May 2024
20240607 QFM018 Elixir Reading List May 2024
Matthew Sinclair
 
HCL Notes and Domino License Cost Reduction in the World of DLAU
HCL Notes and Domino License Cost Reduction in the World of DLAUHCL Notes and Domino License Cost Reduction in the World of DLAU
HCL Notes and Domino License Cost Reduction in the World of DLAU
panagenda
 
RESUME BUILDER APPLICATION Project for students
RESUME BUILDER APPLICATION Project for studentsRESUME BUILDER APPLICATION Project for students
RESUME BUILDER APPLICATION Project for students
KAMESHS29
 
Full-RAG: A modern architecture for hyper-personalization
Full-RAG: A modern architecture for hyper-personalizationFull-RAG: A modern architecture for hyper-personalization
Full-RAG: A modern architecture for hyper-personalization
Zilliz
 
GraphRAG for Life Science to increase LLM accuracy
GraphRAG for Life Science to increase LLM accuracyGraphRAG for Life Science to increase LLM accuracy
GraphRAG for Life Science to increase LLM accuracy
Tomaz Bratanic
 
Pushing the limits of ePRTC: 100ns holdover for 100 days
Pushing the limits of ePRTC: 100ns holdover for 100 daysPushing the limits of ePRTC: 100ns holdover for 100 days
Pushing the limits of ePRTC: 100ns holdover for 100 days
Adtran
 
How to use Firebase Data Connect For Flutter
How to use Firebase Data Connect For FlutterHow to use Firebase Data Connect For Flutter
How to use Firebase Data Connect For Flutter
Daiki Mogmet Ito
 
How to Get CNIC Information System with Paksim Ga.pptx
How to Get CNIC Information System with Paksim Ga.pptxHow to Get CNIC Information System with Paksim Ga.pptx
How to Get CNIC Information System with Paksim Ga.pptx
danishmna97
 
HCL Notes und Domino Lizenzkostenreduzierung in der Welt von DLAU
HCL Notes und Domino Lizenzkostenreduzierung in der Welt von DLAUHCL Notes und Domino Lizenzkostenreduzierung in der Welt von DLAU
HCL Notes und Domino Lizenzkostenreduzierung in der Welt von DLAU
panagenda
 
Why You Should Replace Windows 11 with Nitrux Linux 3.5.0 for enhanced perfor...
Why You Should Replace Windows 11 with Nitrux Linux 3.5.0 for enhanced perfor...Why You Should Replace Windows 11 with Nitrux Linux 3.5.0 for enhanced perfor...
Why You Should Replace Windows 11 with Nitrux Linux 3.5.0 for enhanced perfor...
SOFTTECHHUB
 
Artificial Intelligence for XMLDevelopment
Artificial Intelligence for XMLDevelopmentArtificial Intelligence for XMLDevelopment
Artificial Intelligence for XMLDevelopment
Octavian Nadolu
 
Microsoft - Power Platform_G.Aspiotis.pdf
Microsoft - Power Platform_G.Aspiotis.pdfMicrosoft - Power Platform_G.Aspiotis.pdf
Microsoft - Power Platform_G.Aspiotis.pdf
Uni Systems S.M.S.A.
 
Observability Concepts EVERY Developer Should Know -- DeveloperWeek Europe.pdf
Observability Concepts EVERY Developer Should Know -- DeveloperWeek Europe.pdfObservability Concepts EVERY Developer Should Know -- DeveloperWeek Europe.pdf
Observability Concepts EVERY Developer Should Know -- DeveloperWeek Europe.pdf
Paige Cruz
 
GraphSummit Singapore | Graphing Success: Revolutionising Organisational Stru...
GraphSummit Singapore | Graphing Success: Revolutionising Organisational Stru...GraphSummit Singapore | Graphing Success: Revolutionising Organisational Stru...
GraphSummit Singapore | Graphing Success: Revolutionising Organisational Stru...
Neo4j
 
GraphSummit Singapore | The Future of Agility: Supercharging Digital Transfor...
GraphSummit Singapore | The Future of Agility: Supercharging Digital Transfor...GraphSummit Singapore | The Future of Agility: Supercharging Digital Transfor...
GraphSummit Singapore | The Future of Agility: Supercharging Digital Transfor...
Neo4j
 
Building Production Ready Search Pipelines with Spark and Milvus
Building Production Ready Search Pipelines with Spark and MilvusBuilding Production Ready Search Pipelines with Spark and Milvus
Building Production Ready Search Pipelines with Spark and Milvus
Zilliz
 
Programming Foundation Models with DSPy - Meetup Slides
Programming Foundation Models with DSPy - Meetup SlidesProgramming Foundation Models with DSPy - Meetup Slides
Programming Foundation Models with DSPy - Meetup Slides
Zilliz
 

Recently uploaded (20)

Uni Systems Copilot event_05062024_C.Vlachos.pdf
Uni Systems Copilot event_05062024_C.Vlachos.pdfUni Systems Copilot event_05062024_C.Vlachos.pdf
Uni Systems Copilot event_05062024_C.Vlachos.pdf
 
20240605 QFM017 Machine Intelligence Reading List May 2024
20240605 QFM017 Machine Intelligence Reading List May 202420240605 QFM017 Machine Intelligence Reading List May 2024
20240605 QFM017 Machine Intelligence Reading List May 2024
 
GraphSummit Singapore | The Art of the Possible with Graph - Q2 2024
GraphSummit Singapore | The Art of the  Possible with Graph - Q2 2024GraphSummit Singapore | The Art of the  Possible with Graph - Q2 2024
GraphSummit Singapore | The Art of the Possible with Graph - Q2 2024
 
20240607 QFM018 Elixir Reading List May 2024
20240607 QFM018 Elixir Reading List May 202420240607 QFM018 Elixir Reading List May 2024
20240607 QFM018 Elixir Reading List May 2024
 
HCL Notes and Domino License Cost Reduction in the World of DLAU
HCL Notes and Domino License Cost Reduction in the World of DLAUHCL Notes and Domino License Cost Reduction in the World of DLAU
HCL Notes and Domino License Cost Reduction in the World of DLAU
 
RESUME BUILDER APPLICATION Project for students
RESUME BUILDER APPLICATION Project for studentsRESUME BUILDER APPLICATION Project for students
RESUME BUILDER APPLICATION Project for students
 
Full-RAG: A modern architecture for hyper-personalization
Full-RAG: A modern architecture for hyper-personalizationFull-RAG: A modern architecture for hyper-personalization
Full-RAG: A modern architecture for hyper-personalization
 
GraphRAG for Life Science to increase LLM accuracy
GraphRAG for Life Science to increase LLM accuracyGraphRAG for Life Science to increase LLM accuracy
GraphRAG for Life Science to increase LLM accuracy
 
Pushing the limits of ePRTC: 100ns holdover for 100 days
Pushing the limits of ePRTC: 100ns holdover for 100 daysPushing the limits of ePRTC: 100ns holdover for 100 days
Pushing the limits of ePRTC: 100ns holdover for 100 days
 
How to use Firebase Data Connect For Flutter
How to use Firebase Data Connect For FlutterHow to use Firebase Data Connect For Flutter
How to use Firebase Data Connect For Flutter
 
How to Get CNIC Information System with Paksim Ga.pptx
How to Get CNIC Information System with Paksim Ga.pptxHow to Get CNIC Information System with Paksim Ga.pptx
How to Get CNIC Information System with Paksim Ga.pptx
 
HCL Notes und Domino Lizenzkostenreduzierung in der Welt von DLAU
HCL Notes und Domino Lizenzkostenreduzierung in der Welt von DLAUHCL Notes und Domino Lizenzkostenreduzierung in der Welt von DLAU
HCL Notes und Domino Lizenzkostenreduzierung in der Welt von DLAU
 
Why You Should Replace Windows 11 with Nitrux Linux 3.5.0 for enhanced perfor...
Why You Should Replace Windows 11 with Nitrux Linux 3.5.0 for enhanced perfor...Why You Should Replace Windows 11 with Nitrux Linux 3.5.0 for enhanced perfor...
Why You Should Replace Windows 11 with Nitrux Linux 3.5.0 for enhanced perfor...
 
Artificial Intelligence for XMLDevelopment
Artificial Intelligence for XMLDevelopmentArtificial Intelligence for XMLDevelopment
Artificial Intelligence for XMLDevelopment
 
Microsoft - Power Platform_G.Aspiotis.pdf
Microsoft - Power Platform_G.Aspiotis.pdfMicrosoft - Power Platform_G.Aspiotis.pdf
Microsoft - Power Platform_G.Aspiotis.pdf
 
Observability Concepts EVERY Developer Should Know -- DeveloperWeek Europe.pdf
Observability Concepts EVERY Developer Should Know -- DeveloperWeek Europe.pdfObservability Concepts EVERY Developer Should Know -- DeveloperWeek Europe.pdf
Observability Concepts EVERY Developer Should Know -- DeveloperWeek Europe.pdf
 
GraphSummit Singapore | Graphing Success: Revolutionising Organisational Stru...
GraphSummit Singapore | Graphing Success: Revolutionising Organisational Stru...GraphSummit Singapore | Graphing Success: Revolutionising Organisational Stru...
GraphSummit Singapore | Graphing Success: Revolutionising Organisational Stru...
 
GraphSummit Singapore | The Future of Agility: Supercharging Digital Transfor...
GraphSummit Singapore | The Future of Agility: Supercharging Digital Transfor...GraphSummit Singapore | The Future of Agility: Supercharging Digital Transfor...
GraphSummit Singapore | The Future of Agility: Supercharging Digital Transfor...
 
Building Production Ready Search Pipelines with Spark and Milvus
Building Production Ready Search Pipelines with Spark and MilvusBuilding Production Ready Search Pipelines with Spark and Milvus
Building Production Ready Search Pipelines with Spark and Milvus
 
Programming Foundation Models with DSPy - Meetup Slides
Programming Foundation Models with DSPy - Meetup SlidesProgramming Foundation Models with DSPy - Meetup Slides
Programming Foundation Models with DSPy - Meetup Slides
 

Featured

2024 State of Marketing Report – by Hubspot
2024 State of Marketing Report – by Hubspot2024 State of Marketing Report – by Hubspot
2024 State of Marketing Report – by Hubspot
Marius Sescu
 
Everything You Need To Know About ChatGPT
Everything You Need To Know About ChatGPTEverything You Need To Know About ChatGPT
Everything You Need To Know About ChatGPT
Expeed Software
 
Product Design Trends in 2024 | Teenage Engineerings
Product Design Trends in 2024 | Teenage EngineeringsProduct Design Trends in 2024 | Teenage Engineerings
Product Design Trends in 2024 | Teenage Engineerings
Pixeldarts
 
How Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental HealthHow Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental Health
ThinkNow
 
AI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdfAI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdf
marketingartwork
 
Skeleton Culture Code
Skeleton Culture CodeSkeleton Culture Code
Skeleton Culture Code
Skeleton Technologies
 
PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024
Neil Kimberley
 
Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)
contently
 
How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024
Albert Qian
 
Social Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsSocial Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie Insights
Kurio // The Social Media Age(ncy)
 
Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024
Search Engine Journal
 
5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary
SpeakerHub
 
ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd
Clark Boyd
 
Getting into the tech field. what next
Getting into the tech field. what next Getting into the tech field. what next
Getting into the tech field. what next
Tessa Mero
 
Google's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search IntentGoogle's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search Intent
Lily Ray
 
How to have difficult conversations
How to have difficult conversations How to have difficult conversations
How to have difficult conversations
Rajiv Jayarajah, MAppComm, ACC
 
Introduction to Data Science
Introduction to Data ScienceIntroduction to Data Science
Introduction to Data Science
Christy Abraham Joy
 
Time Management & Productivity - Best Practices
Time Management & Productivity -  Best PracticesTime Management & Productivity -  Best Practices
Time Management & Productivity - Best Practices
Vit Horky
 
The six step guide to practical project management
The six step guide to practical project managementThe six step guide to practical project management
The six step guide to practical project management
MindGenius
 
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
RachelPearson36
 

Featured (20)

2024 State of Marketing Report – by Hubspot
2024 State of Marketing Report – by Hubspot2024 State of Marketing Report – by Hubspot
2024 State of Marketing Report – by Hubspot
 
Everything You Need To Know About ChatGPT
Everything You Need To Know About ChatGPTEverything You Need To Know About ChatGPT
Everything You Need To Know About ChatGPT
 
Product Design Trends in 2024 | Teenage Engineerings
Product Design Trends in 2024 | Teenage EngineeringsProduct Design Trends in 2024 | Teenage Engineerings
Product Design Trends in 2024 | Teenage Engineerings
 
How Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental HealthHow Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental Health
 
AI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdfAI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdf
 
Skeleton Culture Code
Skeleton Culture CodeSkeleton Culture Code
Skeleton Culture Code
 
PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024
 
Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)
 
How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024
 
Social Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsSocial Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie Insights
 
Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024
 
5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary
 
ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd
 
Getting into the tech field. what next
Getting into the tech field. what next Getting into the tech field. what next
Getting into the tech field. what next
 
Google's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search IntentGoogle's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search Intent
 
How to have difficult conversations
How to have difficult conversations How to have difficult conversations
How to have difficult conversations
 
Introduction to Data Science
Introduction to Data ScienceIntroduction to Data Science
Introduction to Data Science
 
Time Management & Productivity - Best Practices
Time Management & Productivity -  Best PracticesTime Management & Productivity -  Best Practices
Time Management & Productivity - Best Practices
 
The six step guide to practical project management
The six step guide to practical project managementThe six step guide to practical project management
The six step guide to practical project management
 
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
 

[PyCon KR 2019] Pickle & Custom Binary Serializer

  • 1. PyCon Korea 2019 Pickle & Custom Binary Serializer Young Seok Tony Kim (김영석)
  • 2. About the speaker www.aitrics.com contact@aitrics.com Soyoung Yoon <lovelife@kaist.ac.kr> ● Software Engineer ● Machine Learning Research Engineer Master’s degree in Data Science
  • 4. • Understand the general idea how pickle works internally • Know when is the best to use pickle, when to use other serialization methods • Build a simple custom serializer !4 Objective of this talk
  • 5. !5 What is Pickle? What is serialization? 0110000101101100 … Serialization De-serialization Python object Python object
  • 6. !6 What is Pickle? What is serialization? 0110000101101100 … Serialization De-serialization Python object Python object pickle.dump()
 pickle.dumps() pickle.load()
 pickle.loads()
  • 7. !7 What is Pickle? What is serialization? 0110000101101100 … Serialization De-serialization Python object Python object pickle.dump()
 pickle.dumps() pickle.load()
 pickle.loads() Pickle module
  • 8. !8 Other serialization methods JSON example from Wikipedia XML example from W3schools Protobuf example from Google
  • 9. !9 Other serialization methods Pickle JSON Protobuf MessagePack Human-readable No
 (except protocol 0) Yes No No Python-specific Yes No No No User-defined class Yes No No No
  • 10. !10 Pickle API pickle.dump(obj, file, protocol=None, *, fix_imports=True) pickle.dumps(obj, protocol=None, *, fix_imports=True) pickle.load(file, *, fix_imports=True, encoding=“ASCII”, errors=“strict”) pickle.loads(bytes_object, *, fix_imports=True, encoding="ASCII", errors="strict")
  • 11. !11 Pickle API pickle.dump(obj, file, protocol=None, *, fix_imports=True) pickle.dumps(obj, protocol=None, *, fix_imports=True) pickle.load(file, *, fix_imports=True, encoding=“ASCII”, errors=“strict”) pickle.loads(bytes_object, *, fix_imports=True, encoding="ASCII", errors="strict") Python object 10110001 01110111 010111… File Writes
  • 12. !12 Pickle API pickle.dump(obj, file, protocol=None, *, fix_imports=True) pickle.dumps(obj, protocol=None, *, fix_imports=True) pickle.load(file, *, fix_imports=True, encoding=“ASCII”, errors=“strict”) pickle.loads(bytes_object, *, fix_imports=True, encoding="ASCII", errors="strict") Python object Returns b'x80x03Kn.' Bytes
  • 13. !13 Pickle API pickle.dump(obj, file, protocol=None, *, fix_imports=True) pickle.dumps(obj, protocol=None, *, fix_imports=True) pickle.load(file, *, fix_imports=True, encoding=“ASCII”, errors=“strict”) pickle.loads(bytes_object, *, fix_imports=True, encoding="ASCII", errors="strict") Python object 10110001 01110111 010111… File Reads
  • 14. !14 Pickle API pickle.dump(obj, file, protocol=None, *, fix_imports=True) pickle.dumps(obj, protocol=None, *, fix_imports=True) pickle.load(file, *, fix_imports=True, encoding=“ASCII”, errors=“strict”) pickle.loads(bytes_object, *, fix_imports=True, encoding="ASCII", errors="strict") Python object Returns b'x80x03Kn.' Bytes
  • 15. !15 Pickle API pickle.dump(obj, file, protocol=None, *, fix_imports=True) pickle.dumps(obj, protocol=None, *, fix_imports=True) pickle.load(file, *, fix_imports=True, encoding=“ASCII”, errors=“strict”) pickle.loads(bytes_object, *, fix_imports=True, encoding="ASCII", errors="strict")
  • 17. !17 Pickle Protocols Protocol # #number 0 1 2 3 4 5 Introduced in - - 2.3 3.0 3.4 3.8 Relevant PEP - - PEP 307 What’s new in Python 3.0 PEP 3154 PEP 574 What’s 
 added human-readable, original protocol old binary format provides 
 much more efficient pickling of new-style classes. It has explicit support for bytes objects adds support for very large objects, pickling more kinds of objects, and some data format optimizations supports
 out-of-band 
 data Note was called
 “text mode” was called 
 “binary mode” Cannot be unpickled by Python 2.x Support for Unicode
  • 18. • Pickle protocol opcodes never changes. Only the new ones are introduced. • This make sure old pickles continue to be readable forever • If older unpickler tries to read a pickle generated by newer protocol, it will either • Work well, if the newer protocol does not use higher protocol opcode. • Explicitly give you an error, by raising PicklingError exception !18 Pickle Protocols
  • 19. Opcode examples Opcode Name Opcode (Byte) INT I LONG L LONG1 x8a BININT J BININT1 K STRING S NONE N NEWTRUE x88 NEWFALSE x89 … …
  • 21. !21 LONG (0) ‘L’ Before and After the decimal value
  • 22. !22 LONG (0) HEX 4C 33 4C 0A 2E ASCII L 3 L n . INT 76 51 76 10 46 ‘L’ Before and After the decimal value
  • 23. !23 LONG (0) HEX 4C 33 4C 0A 2E ASCII L 3 L n . INT 76 51 76 10 46 ‘L’ Before and After the decimal value
  • 24. !24 LONG (0) HEX 4C 33 4C 0A 2E ASCII L 3 L n . INT 76 51 76 10 46 ‘L’ Before and After the decimal value
  • 25. !25 LONG (0) HEX 4C 33 4C 0A 2E ASCII L 3 L n . INT 76 51 76 10 46 ‘L’ Before and After the decimal value
  • 27. !27 LONG (0) HEX 4C 31 32 33 34 35 36 37 38 39 30 … 4C 0A 2E ASCII L 1 2 3 4 5 6 7 8 9 0 … L n . INT 76 49 50 51 52 53 54 55 56 57 48 … 76 10 46
  • 28. Takes quadratic in the number of digits when unpickling !28 LONG (0) HEX 4C 31 32 33 34 35 36 37 38 39 30 … 4C 0A 2E ASCII L 1 2 3 4 5 6 7 8 9 0 … L n . INT 76 49 50 51 52 53 54 55 56 57 48 … 76 10 46
  • 29. !29 FLOAT (0) HEX 46 33 2E 31 34 0A 2E ASCII F 3 . 1 4 n . INT 70 51 46 49 52 10 46
  • 30. !30 FLOAT (0) HEX 46 33 2E 31 34 0A 2E ASCII F 3 . 1 4 n . INT 70 51 46 49 52 10 46
  • 31. tuple
  • 33. !33 TUPLE (0) HEX 28 4C 30 4C 0A 4C 31 4C 0A 4C 32 4C 0A 74 70 30 0A 2E ASCII ( L 0 L n L 1 L n L 2 L n t p 0 n . INT 40 76 48 76 10 76 49 76 10 76 50 76 10 116 112 48 10 46
  • 34. !34 TUPLE (0) HEX 28 4C 30 4C 0A 4C 31 4C 0A 4C 32 4C 0A 74 70 30 0A 2E ASCII ( L 0 L n L 1 L n L 2 L n t p 0 n . INT 40 76 48 76 10 76 49 76 10 76 50 76 10 116 112 48 10 46 MARK MARK Stack
  • 35. !35 TUPLE (0) HEX 28 4C 30 4C 0A 4C 31 4C 0A 4C 32 4C 0A 74 70 30 0A 2E ASCII ( L 0 L n L 1 L n L 2 L n t p 0 n . INT 40 76 48 76 10 76 49 76 10 76 50 76 10 116 112 48 10 46 0 0 MARK Stack
  • 36. !36 TUPLE (0) HEX 28 4C 30 4C 0A 4C 31 4C 0A 4C 32 4C 0A 74 70 30 0A 2E ASCII ( L 0 L n L 1 L n L 2 L n t p 0 n . INT 40 76 48 76 10 76 49 76 10 76 50 76 10 116 112 48 10 46 1 1 0 MARK Stack
  • 37. !37 TUPLE (0) HEX 28 4C 30 4C 0A 4C 31 4C 0A 4C 32 4C 0A 74 70 30 0A 2E ASCII ( L 0 L n L 1 L n L 2 L n t p 0 n . INT 40 76 48 76 10 76 49 76 10 76 50 76 10 116 112 48 10 46 2 2 1 0 MARK Stack
  • 38. !38 TUPLE (0) HEX 28 4C 30 4C 0A 4C 31 4C 0A 4C 32 4C 0A 74 70 30 0A 2E ASCII ( L 0 L n L 1 L n L 2 L n t p 0 n . INT 40 76 48 76 10 76 49 76 10 76 50 76 10 116 112 48 10 46 TUPLE (0,1,2) Stack
  • 39. !39 TUPLE (0) HEX 28 4C 30 4C 0A 4C 31 4C 0A 4C 32 4C 0A 74 70 30 0A 2E ASCII ( L 0 L n L 1 L n L 2 L n t p 0 n . INT 40 76 48 76 10 76 49 76 10 76 50 76 10 116 112 48 10 46 PUT (0,1,2) Stack Index 0 1 2 3 4 5 Value (0,1,2) Memo
  • 40. list
  • 42. !42 LIST (0) HEX 28 6C 70 30 0A 4C 30 4C 0A 61 4C 31 4C 0A 61 67 30 0A 61 2E ASCII ( l p 0 n L 0 L n a L 1 L n a g 0 n a . INT 40 108 112 48 10 76 48 76 10 97 76 49 76 10 97 103 48 10 97 46
  • 43. !43 LIST (0) HEX 28 6C 70 30 0A 4C 30 4C 0A 61 4C 31 4C 0A 61 67 30 0A 61 2E ASCII ( l p 0 n L 0 L n a L 1 L n a g 0 n a . INT 40 108 112 48 10 76 48 76 10 97 76 49 76 10 97 103 48 10 97 46 Index 0 1 2 3 4 5 Value Memo MARK Stack MARK
  • 44. !44 LIST (0) HEX 28 6C 70 30 0A 4C 30 4C 0A 61 4C 31 4C 0A 61 67 30 0A 61 2E ASCII ( l p 0 n L 0 L n a L 1 L n a g 0 n a . INT 40 108 112 48 10 76 48 76 10 97 76 49 76 10 97 103 48 10 97 46 Index 0 1 2 3 4 5 Value Memo list1 Stack LIST list1
  • 45. !45 LIST (0) HEX 28 6C 70 30 0A 4C 30 4C 0A 61 4C 31 4C 0A 61 67 30 0A 61 2E ASCII ( l p 0 n L 0 L n a L 1 L n a g 0 n a . INT 40 108 112 48 10 76 48 76 10 97 76 49 76 10 97 103 48 10 97 46 Index 0 1 2 3 4 5 Value list1 Memo list1 Stack PUT list1
  • 46. !46 LIST (0) HEX 28 6C 70 30 0A 4C 30 4C 0A 61 4C 31 4C 0A 61 67 30 0A 61 2E ASCII ( l p 0 n L 0 L n a L 1 L n a g 0 n a . INT 40 108 112 48 10 76 48 76 10 97 76 49 76 10 97 103 48 10 97 46 Index 0 1 2 3 4 5 Value list1 Memo 0 list1 Stack list1
  • 47. !47 LIST (0) HEX 28 6C 70 30 0A 4C 30 4C 0A 61 4C 31 4C 0A 61 67 30 0A 61 2E ASCII ( l p 0 n L 0 L n a L 1 L n a g 0 n a . INT 40 108 112 48 10 76 48 76 10 97 76 49 76 10 97 103 48 10 97 46 Index 0 1 2 3 4 5 Value list1 Memo list1 Stack APPEND 0list1
  • 48. !48 LIST (0) HEX 28 6C 70 30 0A 4C 30 4C 0A 61 4C 31 4C 0A 61 67 30 0A 61 2E ASCII ( l p 0 n L 0 L n a L 1 L n a g 0 n a . INT 40 108 112 48 10 76 48 76 10 97 76 49 76 10 97 103 48 10 97 46 Index 0 1 2 3 4 5 Value list1 Memo 1 list1 Stack 0list1
  • 49. !49 LIST (0) HEX 28 6C 70 30 0A 4C 30 4C 0A 61 4C 31 4C 0A 61 67 30 0A 61 2E ASCII ( l p 0 n L 0 L n a L 1 L n a g 0 n a . INT 40 108 112 48 10 76 48 76 10 97 76 49 76 10 97 103 48 10 97 46 Index 0 1 2 3 4 5 Value list1 Memo list1 Stack APPEND 0 1list1
  • 50. !50 LIST (0) HEX 28 6C 70 30 0A 4C 30 4C 0A 61 4C 31 4C 0A 61 67 30 0A 61 2E ASCII ( l p 0 n L 0 L n a L 1 L n a g 0 n a . INT 40 108 112 48 10 76 48 76 10 97 76 49 76 10 97 103 48 10 97 46 Index 0 1 2 3 4 5 Value list1 Memo list1 list1 Stack GET 0 1list1
  • 51. !51 LIST (0) HEX 28 6C 70 30 0A 4C 30 4C 0A 61 4C 31 4C 0A 61 67 30 0A 61 2E ASCII ( l p 0 n L 0 L n a L 1 L n a g 0 n a . INT 40 108 112 48 10 76 48 76 10 97 76 49 76 10 97 103 48 10 97 46 Index 0 1 2 3 4 5 Value list1 Memo list1 Stack APPEND 0 1 list1list1
  • 54. !54 BININT2 (1) HEX 4D 00 01 2E ASCII M x00 x01 . INT 76 49 50 51 BININT2
  • 59. • Official doc: https://docs.python.org/3/library/pickletools.html • Usage !59 pickletools
  • 67. • New pickle protocol 5 can send extra metadata needed for out-of-band data buffers. • There is a new PickleBuffer to return out-of-band data buffers. • Reduces unnecessary memory copies. !67 PEP 574 -- Pickle protocol 5 with 
 out-of-band data
  • 68. !68 PEP 574 -- Pickle protocol 5 with 
 out-of-band data https://github.com/numpy/numpy/issues/11161
  • 69. • I was implementing an ICLR 2016 paper - 
 “Deep Compression: Compressing Deep Neural Networks with Pruning, Trained Quantization and Huffman Coding” 
 by Song Han, Huizi Mao, William J. Dally • This paper was about compressing Neural Network. • For example, compressing AlexNet from 233MB to 8.9MB 
 without loss of accuracy • The official repository did not contain Huffman Coding !69 Custom Binary Serializer
  • 70. !70 Custom Binary Serializer Your custom object Custom serialization logic bytearray object 1A FF 8B 9C
  • 71. !71 Custom Binary Serializer Quantized sparse
 numpy array Your custom object Custom serialization logic bytearray object 1A FF 8B 9C
  • 72. !72 Custom Binary Serializer Quantized sparse
 numpy array Your custom object Custom serialization logic bytearray object 1A FF 8B 9C Binary string to “0110110101010101000…”
  • 73. !73 Custom Binary Serializer bytearray object 1A FF 8B 9C Binary string to “0110110101010101000…”
  • 74. • struct module performs conversions between Python values and C structs represented as Python bytes objects. !74 struct module Python object bytes object 1A FF 8B 9C struct.pack() struct.unpack()
  • 76. • What is difference between cPickle and pickle? • What can be pickled? What cannot pickled? • When should we use pickle? !76 FAQ
  • 77. What is the difference between cPickle and pickle? • _pickle is a pickle module implemented in C language. 
 It is faster than Python implementation. • _pickle was known as cPickle in Python 2. • In Python 2, cPickle and pickle modules were separate. • In Python 3, pickle module imports and uses _pickle, if available. 
 Otherwise, it uses python implementation. • TLDR; Use Python 3, you don’t need to worry about anything.
  • 78. • None, True, and False • integers, floating point numbers, complex numbers • strings, bytes, bytearrays • tuples, lists, sets, and dictionaries containing only picklable objects • Classes, functions 
 (both built-in and user-defined) defined at the top level of a module (using def, not lambda) • Instances of such classes whose __dict__ or the result of calling __getstate__() is picklable (see section Pickling Class Instances for details). !78 What can be pickled and unpickled?
  • 79. !79 Can a function be pickled? 10110001 01110111 010111… function.pickle File
  • 80. !80 Can a function be pickled? 10110001 01110111 010111… function.pickle File
  • 81. !81 Can a function be pickled? 10110001 01110111 010111… function.pickle File
  • 82. • Only the function’s name is pickled, along with the name of the module the function is defined in. • Functions are pickled by name reference, not by value. This means you cannot pickle lambda functions. • Similarly, classes, methods, decorators (which is also a function) are pickled by reference, not by the value !82 Function content is not pickled
  • 83. • Pickle is probably most useful when you want to quickly store python objects to restore later. • JSON is more readable, simple, and cross-platform • Protobuf is good for its performance, especially for network communication • messagepack is an alternative for JSON, when you want faster and smaller serialization • Only the function’s name is pickled, along with the name of the module the function is defined in. A function’s content is NOT pickled. • When building a custom serializer, I recommend looking struct module in Python !83 Summary / Takeaways
  • 84. References • Official Python document • https://docs.python.org/3/library/pickle.html • CPython Source code 
 (Great content. Extensive comments here are quite easy to understand) • https://github.com/python/cpython/blob/master/Lib/pickle.py • https://github.com/python/cpython/blob/master/Lib/pickletools.py
  • 86. • 파이썬 웹서버 REST API 문서 쉽고 빠르게 작성하기 (Yongseon Lee) 
 18th (Sun) 11:55 ~ 12:35 • Advanced Python testing techniques (Jaeman An)
 18th (Sun) 11:55 ~ 12:35 • 실시간 의료 인공지능 데이터 처리를 위한 Django Query Optimization (Soyoung Yoon)
 18th (Sun) 13:55 ~ 14:35 • Pickle & Custom Binary Serializer (Young Seok Kim)
 18th (Sun) 14:55 ~ 15:35 Talks from AITRICS www.aitrics.com contact@aitrics.com Thank Soyoung Yoon <lovelife@kaist.ac.kr> We are Hiring! ● Software Engineer ● Machine Learning Researc