@@ -64,9 +64,9 @@ See note below for detail.
6464
6565## Install
6666
67-
68- $ pip install msgpack
69-
67+ ```
68+ $ pip install msgpack
69+ ```
7070
7171### Pure Python implementation
7272
@@ -103,18 +103,18 @@ msgpack provides `dumps` and `loads` as an alias for compatibility with
103103` unpack ` and ` load ` unpacks from a file-like object.
104104
105105``` pycon
106- >>> import msgpack
107- >>> msgpack.packb([1, 2, 3], use_bin_type=True)
108- '\x93\x01\x02\x03'
109- >>> msgpack.unpackb(_, raw=False)
110- [1, 2, 3]
106+ >>> import msgpack
107+ >>> msgpack.packb([1 , 2 , 3 ], use_bin_type = True )
108+ '\x93\x01\x02\x03'
109+ >>> msgpack.unpackb(_, raw = False )
110+ [1, 2, 3]
111111```
112112
113113` unpack ` unpacks msgpack's array to Python's list, but can also unpack to tuple:
114114
115115``` pycon
116- >>> msgpack.unpackb(b'\x93\x01\x02\x03', use_list=False, raw=False)
117- (1, 2, 3)
116+ >>> msgpack.unpackb(b ' \x93\x01\x02\x03 ' , use_list = False , raw = False )
117+ (1, 2, 3)
118118```
119119
120120You should always specify the ` use_list ` keyword argument for backward compatibility.
@@ -129,18 +129,18 @@ Read the docstring for other options.
129129stream (or from bytes provided through its ` feed ` method).
130130
131131``` py
132- import msgpack
133- from io import BytesIO
132+ import msgpack
133+ from io import BytesIO
134134
135- buf = BytesIO()
136- for i in range (100 ):
137- buf.write(msgpack.packb(i, use_bin_type = True ))
135+ buf = BytesIO()
136+ for i in range (100 ):
137+ buf.write(msgpack.packb(i, use_bin_type = True ))
138138
139- buf.seek(0 )
139+ buf.seek(0 )
140140
141- unpacker = msgpack.Unpacker(buf, raw = False )
142- for unpacked in unpacker:
143- print (unpacked)
141+ unpacker = msgpack.Unpacker(buf, raw = False )
142+ for unpacked in unpacker:
143+ print (unpacked)
144144```
145145
146146
@@ -150,27 +150,27 @@ It is also possible to pack/unpack custom data types. Here is an example for
150150` datetime.datetime ` .
151151
152152``` py
153- import datetime
154- import msgpack
153+ import datetime
154+ import msgpack
155155
156- useful_dict = {
157- " id" : 1 ,
158- " created" : datetime.datetime.now(),
159- }
156+ useful_dict = {
157+ " id" : 1 ,
158+ " created" : datetime.datetime.now(),
159+ }
160160
161- def decode_datetime (obj ):
162- if ' __datetime__' in obj:
163- obj = datetime.datetime.strptime(obj[" as_str" ], " %Y%m%d T%H:%M:%S.%f " )
164- return obj
161+ def decode_datetime (obj ):
162+ if ' __datetime__' in obj:
163+ obj = datetime.datetime.strptime(obj[" as_str" ], " %Y%m%d T%H:%M:%S.%f " )
164+ return obj
165165
166- def encode_datetime (obj ):
167- if isinstance (obj, datetime.datetime):
168- return {' __datetime__' : True , ' as_str' : obj.strftime(" %Y%m%d T%H:%M:%S.%f " )}
169- return obj
166+ def encode_datetime (obj ):
167+ if isinstance (obj, datetime.datetime):
168+ return {' __datetime__' : True , ' as_str' : obj.strftime(" %Y%m%d T%H:%M:%S.%f " )}
169+ return obj
170170
171171
172- packed_dict = msgpack.packb(useful_dict, default = encode_datetime, use_bin_type = True )
173- this_dict_again = msgpack.unpackb(packed_dict, object_hook = decode_datetime, raw = False )
172+ packed_dict = msgpack.packb(useful_dict, default = encode_datetime, use_bin_type = True )
173+ this_dict_again = msgpack.unpackb(packed_dict, object_hook = decode_datetime, raw = False )
174174```
175175
176176` Unpacker ` 's ` object_hook ` callback receives a dict; the
@@ -183,25 +183,25 @@ key-value pairs.
183183It is also possible to pack/unpack custom data types using the ** ext** type.
184184
185185``` pycon
186- >>> import msgpack
187- >>> import array
188- >>> def default(obj):
189- ... if isinstance(obj, array.array) and obj.typecode == 'd':
190- ... return msgpack.ExtType(42, obj.tostring())
191- ... raise TypeError("Unknown type: %r" % (obj,))
192- ...
193- >>> def ext_hook(code, data):
194- ... if code == 42:
195- ... a = array.array('d')
196- ... a.fromstring(data)
197- ... return a
198- ... return ExtType(code, data)
199- ...
200- >>> data = array.array('d', [1.2, 3.4])
201- >>> packed = msgpack.packb(data, default=default, use_bin_type=True)
202- >>> unpacked = msgpack.unpackb(packed, ext_hook=ext_hook, raw=False)
203- >>> data == unpacked
204- True
186+ >>> import msgpack
187+ >>> import array
188+ >>> def default (obj ):
189+ ... if isinstance (obj, array.array) and obj.typecode == ' d' :
190+ ... return msgpack.ExtType(42 , obj.tostring())
191+ ... raise TypeError (" Unknown type: %r " % (obj,))
192+ ...
193+ >>> def ext_hook (code , data ):
194+ ... if code == 42 :
195+ ... a = array.array(' d' )
196+ ... a.fromstring(data)
197+ ... return a
198+ ... return ExtType(code, data)
199+ ...
200+ >>> data = array.array(' d' , [1.2 , 3.4 ])
201+ >>> packed = msgpack.packb(data, default = default, use_bin_type = True )
202+ >>> unpacked = msgpack.unpackb(packed, ext_hook = ext_hook, raw = False )
203+ >>> data == unpacked
204+ True
205205```
206206
207207
@@ -226,22 +226,22 @@ You can pack into and unpack from this old spec using `use_bin_type=False`
226226and ` raw=True ` options.
227227
228228``` pycon
229- >>> import msgpack
230- >>> msgpack.unpackb(msgpack.packb([b'spam', u'eggs'], use_bin_type=False), raw=True)
231- [b'spam', b'eggs']
232- >>> msgpack.unpackb(msgpack.packb([b'spam', u'eggs'], use_bin_type=True), raw=False)
233- [b'spam', 'eggs']
229+ >>> import msgpack
230+ >>> msgpack.unpackb(msgpack.packb([b ' spam' , u ' eggs' ], use_bin_type = False ), raw = True )
231+ [b'spam', b'eggs']
232+ >>> msgpack.unpackb(msgpack.packb([b ' spam' , u ' eggs' ], use_bin_type = True ), raw = False )
233+ [b'spam', 'eggs']
234234```
235235
236236### ext type
237237
238238To use the ** ext** type, pass ` msgpack.ExtType ` object to packer.
239239
240240``` pycon
241- >>> import msgpack
242- >>> packed = msgpack.packb(msgpack.ExtType(42, b'xyzzy'))
243- >>> msgpack.unpackb(packed)
244- ExtType(code=42, data='xyzzy')
241+ >>> import msgpack
242+ >>> packed = msgpack.packb(msgpack.ExtType(42 , b ' xyzzy' ))
243+ >>> msgpack.unpackb(packed)
244+ ExtType(code=42, data='xyzzy')
245245```
246246
247247You can use it with ` default ` and ` ext_hook ` . See below.
0 commit comments