Skip to main content

ruamel.ext.msgpack

ruamel.ext.msgpack is a thin wrapper around msgpack, that deals with naive datetime instances and ruamel defined extension types (date).

naive datetime.datetime

If you try to pack a naive datetime.datetime instance, you’ll get an error:

import datetime
from msgpack import packb

try:
    packb(datetime.datetime(2011, 10, 2), datetime=True)
except Exception as e:
    print('exception:', type(e), e)

which will print:

exception: <class 'ValueError'> can not serialize 'datetime.datetime' object where tzinfo=None

using pack/packb from ruamel.ext.msgpack will prevent this from happening, without having to go over the data structure and replacing all naive datetime.datetime instances. It will provide both an argument to default that handles the naive datetimestamp and some Ext types, set use_bin_type=True and datetime=True.

import sys
import datetime
from ruamel.ext.msgpack import packb, unpackb

res = packb(datetime.datetime(2011, 10, 2, 17, 15, 1))
print(unpackb(res))

which will print:

2011-10-02 17:15:01+00:00

as you can see from the output this will make the instance timezone aware.

use_bin_type=False

The pack and packb routines set use_bin_type=False. So the UTF-8 “bytestrings” get dumped as “fixstr”, “str 8/16/32”, and those will load as str back into Python (the function hex() returns a string of hexadecimal values from the bytes array passed into it):

from ruamel.ext.msgpack import packb, unpackb, hex

res = packb('こんにちは世界'.encode('utf-8'))
print(hex(res), len(res))
print(unpackb(res))

\xb5 indicates “fixstr” of length 21

\xb5\xe3\x81\x93\xe3\x82\x93\xe3\x81\xab\xe3\x81\xa1\xe3\x81\xaf\xe4\xb8\x96\xe7\x95\x8c 22
こんにちは世界

If you need byte arrays, an alternate version of pack/unpack can be constructed, that still handle naive datetime objects, and the other types:

from functools import partial
from ruamel.ext.msgpack import hex, unpackb, msgpack_default
import msgpack

pack = partial(msgpack.pack, default=msgpack_default, use_bin_type=True, datetime=True)
packb = partial(msgpack.packb, default=msgpack_default, use_bin_type=True, datetime=True)

res = packb('こんにちは世界'.encode('utf-8'))
print(hex(res), len(res))
print(unpackb(res))

\xc4\x15 indicates “bin 8” of length 21

extension types

The following extension types are provided by ruamel.ext.msgpack. Each has associated attribute on msgpack_default with the type number. This number can be changed, but the same numbers should be used for packing and unpacking. If a number is set to None the associated type will not be packed or unpacked. The type used for naive datetime.datetime, -1, cannot be changed.

datetime.date

Python’s datetime.date instances are packed in a two bytes stucture for dates in the range 2000-01-01 and 2126-12-31.

\xc4\x15\xe3\x81\x93\xe3\x82\x93\xe3\x81\xab\xe3\x81\xa1\xe3\x81\xaf\xe4\xb8\x96\xe7\x95\x8c 23
b'\xe3\x81\x93\xe3\x82\x93\xe3\x81\xab\xe3\x81\xa1\xe3\x81\xaf\xe4\xb8\x96\xe7\x95\x8c'
import datetime
from ruamel.ext.msgpack import packb, unpackb, hex, msgpack_default

res = packb(datetime.date(2011, 10, 2))
print('hex:', hex(res), len(res))
print(unpackb(res))
print(f'{msgpack_default.date=}')

msgpack_default.date = 42
res = packb(datetime.date(2011, 10, 2))
print('hex:', hex(res), len(res))
print(unpackb(res))

try:
    msgpack_default.date = None
    res = packb(datetime.date(2011, 10, 2))
except Exception as e:
    print('exception:', type(e), e)

which will print:

hex: \xd5\x11\x17\x82 4
2011-10-02
msgpack_default.date=17
hex: \xd5\x2a\x17\x82 4
2011-10-02
exception: <class 'ValueError'> year out of range 2000-2126

Metadata

Release files for ruamel.ext.msgpack 0.1.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for ruamel.ext.msgpack 0.1.0
File Size Uploaded Provenance Actions
ruamel.ext.msgpack-0.1.0.tar.gz 13.8 kB Without provenance Details for ruamel.ext.msgpack-0.1.0.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for ruamel.ext.msgpack 0.1.0
File Interpreter ABI Platform Actions
ruamel.ext.msgpack-0.1.0-py3-none-any.whl Python 3 none any Details for ruamel.ext.msgpack-0.1.0-py3-none-any.whl

Total release size: 19.1 kB

Release files / ruamel.ext.msgpack-0.1.0.tar.gz

Download URL ruamel.ext.msgpack-0.1.0.tar.gz
Size 13.8 kB
Tags Source
SHA-256 checksum
How to use checksums
c5f47059bedd990fb3e9f61a8c0a88934e6b983d1fec15c314fc6ad327c930cd
BLAKE2b-256 checksum
How to use checksums
ccffaa3cc492af399ff9346a4e1bb8c775040cb867fe3ef7ff610b6a3512fdce
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.1 CPython/3.10.5

Release files / ruamel.ext.msgpack-0.1.0-py3-none-any.whl

Download URL ruamel.ext.msgpack-0.1.0-py3-none-any.whl
Size 5.3 kB
Tags Python 3
SHA-256 checksum
How to use checksums
8bb1fae69b9c1be4de24dd7587c9f7358106c3eca8692b7425a7fe4e1f72acb8
BLAKE2b-256 checksum
How to use checksums
2c7dd7423ee017eb8e379b3a45c325cbfe7ce3591d9d5fbe9f8d968c3f4527ce
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.1 CPython/3.10.5

Security for ruamel.ext.msgpack 0.1.0

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page