The marshal
module is used to serialize data—that is, convert data to and
from character strings, so that they can be stored on file or sent
over a network. Example 4-9 illustrates this.
The marshal
module uses a simple self-describing data format. For each data
item, the marshalled string contains a type code, followed by one or
more type-specific fields. Integers are stored in little-endian
order, strings are stored as length fields followed by the strings’ contents
(which can include null bytes), tuples are stored as length fields followed by
the objects that make up each tuple, etc.
Example 4-9. Using the marshal Module to Serialize Data
File: marshal-example-1.py import marshal value = ( "this is a string", [1, 2, 3, 4], ("more tuples", 1.0, 2.3, 4.5), "this is yet another string" ) data = marshal.dumps(value) # intermediate format print type(data), len(data) print "-"*50 print repr(data) print "-"*50 print marshal.loads(data)<type 'string'> 118
--------------------------------------------------
'( 04 00 00 00s 20 00 00 00this is a string
[ 04 00 00 00i 01 00 00 00i 02 00 00 00
i 03 00 00 00i 04 00 00 00( 04 00 00 00
s 13 00 00 00more tuplesf 031.0f 032.3f 034.
5s 32 00 00 00this is yet another string'
--------------------------------------------------
('this is a string', [1, 2, 3, 4], ('more tuples',
1.0, 2.3, 4.5), 'this is yet another string')
The marshal
module can also handle code objects (it’s used to store
precompiled Python modules). Example 4-10 demonstrates.
Example 4-10. Using the marshal Module to Serialize Code
File: marshal-example-2.py import marshal script = """ print 'hello' """ code = compile(script, "<script>", "exec") data = marshal.dumps(code) # intermediate format print type(data), len(data) print "-"*50 print repr(data) print "-"*50 exec marshal.loads(data)<type 'string'> 81
--------------------------------------------------
'c 00 00 00 00 01 00 00 00s 17 00 00 0
0177 00 00177 02 00d 00 00GHd 01 00S( 0
2 00 00 00s 05 00 00 00helloN( 00 00 00
000( 00 00 00 00s 10 00 00 00<script>s 01