mirror of
https://github.com/msgpack/msgpack-python.git
synced 2025-10-19 20:03:16 +00:00
format markdown
This commit is contained in:
parent
94336cf914
commit
051f9ded1f
1 changed files with 63 additions and 63 deletions
126
README.md
126
README.md
|
@ -64,9 +64,9 @@ See note below for detail.
|
||||||
|
|
||||||
## Install
|
## Install
|
||||||
|
|
||||||
|
```
|
||||||
$ pip install msgpack
|
$ pip install msgpack
|
||||||
|
```
|
||||||
|
|
||||||
### Pure Python implementation
|
### Pure Python implementation
|
||||||
|
|
||||||
|
@ -103,18 +103,18 @@ msgpack provides `dumps` and `loads` as an alias for compatibility with
|
||||||
`unpack` and `load` unpacks from a file-like object.
|
`unpack` and `load` unpacks from a file-like object.
|
||||||
|
|
||||||
```pycon
|
```pycon
|
||||||
>>> import msgpack
|
>>> import msgpack
|
||||||
>>> msgpack.packb([1, 2, 3], use_bin_type=True)
|
>>> msgpack.packb([1, 2, 3], use_bin_type=True)
|
||||||
'\x93\x01\x02\x03'
|
'\x93\x01\x02\x03'
|
||||||
>>> msgpack.unpackb(_, raw=False)
|
>>> msgpack.unpackb(_, raw=False)
|
||||||
[1, 2, 3]
|
[1, 2, 3]
|
||||||
```
|
```
|
||||||
|
|
||||||
`unpack` unpacks msgpack's array to Python's list, but can also unpack to tuple:
|
`unpack` unpacks msgpack's array to Python's list, but can also unpack to tuple:
|
||||||
|
|
||||||
```pycon
|
```pycon
|
||||||
>>> msgpack.unpackb(b'\x93\x01\x02\x03', use_list=False, raw=False)
|
>>> msgpack.unpackb(b'\x93\x01\x02\x03', use_list=False, raw=False)
|
||||||
(1, 2, 3)
|
(1, 2, 3)
|
||||||
```
|
```
|
||||||
|
|
||||||
You should always specify the `use_list` keyword argument for backward compatibility.
|
You should always specify the `use_list` keyword argument for backward compatibility.
|
||||||
|
@ -129,18 +129,18 @@ Read the docstring for other options.
|
||||||
stream (or from bytes provided through its `feed` method).
|
stream (or from bytes provided through its `feed` method).
|
||||||
|
|
||||||
```py
|
```py
|
||||||
import msgpack
|
import msgpack
|
||||||
from io import BytesIO
|
from io import BytesIO
|
||||||
|
|
||||||
buf = BytesIO()
|
buf = BytesIO()
|
||||||
for i in range(100):
|
for i in range(100):
|
||||||
buf.write(msgpack.packb(i, use_bin_type=True))
|
buf.write(msgpack.packb(i, use_bin_type=True))
|
||||||
|
|
||||||
buf.seek(0)
|
buf.seek(0)
|
||||||
|
|
||||||
unpacker = msgpack.Unpacker(buf, raw=False)
|
unpacker = msgpack.Unpacker(buf, raw=False)
|
||||||
for unpacked in unpacker:
|
for unpacked in unpacker:
|
||||||
print(unpacked)
|
print(unpacked)
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
@ -150,27 +150,27 @@ It is also possible to pack/unpack custom data types. Here is an example for
|
||||||
`datetime.datetime`.
|
`datetime.datetime`.
|
||||||
|
|
||||||
```py
|
```py
|
||||||
import datetime
|
import datetime
|
||||||
import msgpack
|
import msgpack
|
||||||
|
|
||||||
useful_dict = {
|
useful_dict = {
|
||||||
"id": 1,
|
"id": 1,
|
||||||
"created": datetime.datetime.now(),
|
"created": datetime.datetime.now(),
|
||||||
}
|
}
|
||||||
|
|
||||||
def decode_datetime(obj):
|
def decode_datetime(obj):
|
||||||
if '__datetime__' in obj:
|
if '__datetime__' in obj:
|
||||||
obj = datetime.datetime.strptime(obj["as_str"], "%Y%m%dT%H:%M:%S.%f")
|
obj = datetime.datetime.strptime(obj["as_str"], "%Y%m%dT%H:%M:%S.%f")
|
||||||
return obj
|
return obj
|
||||||
|
|
||||||
def encode_datetime(obj):
|
def encode_datetime(obj):
|
||||||
if isinstance(obj, datetime.datetime):
|
if isinstance(obj, datetime.datetime):
|
||||||
return {'__datetime__': True, 'as_str': obj.strftime("%Y%m%dT%H:%M:%S.%f")}
|
return {'__datetime__': True, 'as_str': obj.strftime("%Y%m%dT%H:%M:%S.%f")}
|
||||||
return obj
|
return obj
|
||||||
|
|
||||||
|
|
||||||
packed_dict = msgpack.packb(useful_dict, default=encode_datetime, use_bin_type=True)
|
packed_dict = msgpack.packb(useful_dict, default=encode_datetime, use_bin_type=True)
|
||||||
this_dict_again = msgpack.unpackb(packed_dict, object_hook=decode_datetime, raw=False)
|
this_dict_again = msgpack.unpackb(packed_dict, object_hook=decode_datetime, raw=False)
|
||||||
```
|
```
|
||||||
|
|
||||||
`Unpacker`'s `object_hook` callback receives a dict; the
|
`Unpacker`'s `object_hook` callback receives a dict; the
|
||||||
|
@ -183,25 +183,25 @@ key-value pairs.
|
||||||
It is also possible to pack/unpack custom data types using the **ext** type.
|
It is also possible to pack/unpack custom data types using the **ext** type.
|
||||||
|
|
||||||
```pycon
|
```pycon
|
||||||
>>> import msgpack
|
>>> import msgpack
|
||||||
>>> import array
|
>>> import array
|
||||||
>>> def default(obj):
|
>>> def default(obj):
|
||||||
... if isinstance(obj, array.array) and obj.typecode == 'd':
|
... if isinstance(obj, array.array) and obj.typecode == 'd':
|
||||||
... return msgpack.ExtType(42, obj.tostring())
|
... return msgpack.ExtType(42, obj.tostring())
|
||||||
... raise TypeError("Unknown type: %r" % (obj,))
|
... raise TypeError("Unknown type: %r" % (obj,))
|
||||||
...
|
...
|
||||||
>>> def ext_hook(code, data):
|
>>> def ext_hook(code, data):
|
||||||
... if code == 42:
|
... if code == 42:
|
||||||
... a = array.array('d')
|
... a = array.array('d')
|
||||||
... a.fromstring(data)
|
... a.fromstring(data)
|
||||||
... return a
|
... return a
|
||||||
... return ExtType(code, data)
|
... return ExtType(code, data)
|
||||||
...
|
...
|
||||||
>>> data = array.array('d', [1.2, 3.4])
|
>>> data = array.array('d', [1.2, 3.4])
|
||||||
>>> packed = msgpack.packb(data, default=default, use_bin_type=True)
|
>>> packed = msgpack.packb(data, default=default, use_bin_type=True)
|
||||||
>>> unpacked = msgpack.unpackb(packed, ext_hook=ext_hook, raw=False)
|
>>> unpacked = msgpack.unpackb(packed, ext_hook=ext_hook, raw=False)
|
||||||
>>> data == unpacked
|
>>> data == unpacked
|
||||||
True
|
True
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
|
@ -226,11 +226,11 @@ You can pack into and unpack from this old spec using `use_bin_type=False`
|
||||||
and `raw=True` options.
|
and `raw=True` options.
|
||||||
|
|
||||||
```pycon
|
```pycon
|
||||||
>>> import msgpack
|
>>> import msgpack
|
||||||
>>> msgpack.unpackb(msgpack.packb([b'spam', u'eggs'], use_bin_type=False), raw=True)
|
>>> msgpack.unpackb(msgpack.packb([b'spam', u'eggs'], use_bin_type=False), raw=True)
|
||||||
[b'spam', b'eggs']
|
[b'spam', b'eggs']
|
||||||
>>> msgpack.unpackb(msgpack.packb([b'spam', u'eggs'], use_bin_type=True), raw=False)
|
>>> msgpack.unpackb(msgpack.packb([b'spam', u'eggs'], use_bin_type=True), raw=False)
|
||||||
[b'spam', 'eggs']
|
[b'spam', 'eggs']
|
||||||
```
|
```
|
||||||
|
|
||||||
### ext type
|
### ext type
|
||||||
|
@ -238,10 +238,10 @@ and `raw=True` options.
|
||||||
To use the **ext** type, pass `msgpack.ExtType` object to packer.
|
To use the **ext** type, pass `msgpack.ExtType` object to packer.
|
||||||
|
|
||||||
```pycon
|
```pycon
|
||||||
>>> import msgpack
|
>>> import msgpack
|
||||||
>>> packed = msgpack.packb(msgpack.ExtType(42, b'xyzzy'))
|
>>> packed = msgpack.packb(msgpack.ExtType(42, b'xyzzy'))
|
||||||
>>> msgpack.unpackb(packed)
|
>>> msgpack.unpackb(packed)
|
||||||
ExtType(code=42, data='xyzzy')
|
ExtType(code=42, data='xyzzy')
|
||||||
```
|
```
|
||||||
|
|
||||||
You can use it with `default` and `ext_hook`. See below.
|
You can use it with `default` and `ext_hook`. See below.
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue