@@ -64,9 +64,9 @@ See note below for detail.
64
64
65
65
## Install
66
66
67
-
68
- $ pip install msgpack
69
-
67
+ ```
68
+ $ pip install msgpack
69
+ ```
70
70
71
71
### Pure Python implementation
72
72
@@ -103,18 +103,18 @@ msgpack provides `dumps` and `loads` as an alias for compatibility with
103
103
` unpack ` and ` load ` unpacks from a file-like object.
104
104
105
105
``` 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]
111
111
```
112
112
113
113
` unpack ` unpacks msgpack's array to Python's list, but can also unpack to tuple:
114
114
115
115
``` 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)
118
118
```
119
119
120
120
You should always specify the ` use_list ` keyword argument for backward compatibility.
@@ -129,18 +129,18 @@ Read the docstring for other options.
129
129
stream (or from bytes provided through its ` feed ` method).
130
130
131
131
``` py
132
- import msgpack
133
- from io import BytesIO
132
+ import msgpack
133
+ from io import BytesIO
134
134
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 ))
138
138
139
- buf.seek(0 )
139
+ buf.seek(0 )
140
140
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)
144
144
```
145
145
146
146
@@ -150,27 +150,27 @@ It is also possible to pack/unpack custom data types. Here is an example for
150
150
` datetime.datetime ` .
151
151
152
152
``` py
153
- import datetime
154
- import msgpack
153
+ import datetime
154
+ import msgpack
155
155
156
- useful_dict = {
157
- " id" : 1 ,
158
- " created" : datetime.datetime.now(),
159
- }
156
+ useful_dict = {
157
+ " id" : 1 ,
158
+ " created" : datetime.datetime.now(),
159
+ }
160
160
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
165
165
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
170
170
171
171
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 )
174
174
```
175
175
176
176
` Unpacker ` 's ` object_hook ` callback receives a dict; the
@@ -183,25 +183,25 @@ key-value pairs.
183
183
It is also possible to pack/unpack custom data types using the ** ext** type.
184
184
185
185
``` 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
205
205
```
206
206
207
207
@@ -226,22 +226,22 @@ You can pack into and unpack from this old spec using `use_bin_type=False`
226
226
and ` raw=True ` options.
227
227
228
228
``` 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']
234
234
```
235
235
236
236
### ext type
237
237
238
238
To use the ** ext** type, pass ` msgpack.ExtType ` object to packer.
239
239
240
240
``` 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')
245
245
```
246
246
247
247
You can use it with ` default ` and ` ext_hook ` . See below.
0 commit comments