ruamel.ext.msgpack


Nameruamel.ext.msgpack JSON
Version 0.2.2 PyPI version JSON
download
home_pagehttps://sourceforge.net/p/ruamel-ext-msgpack/code/ci/default/tree
Summarythin wrapper around msgpack to deal with naive datetime and ruamel defined extension types
upload_time2023-08-05 09:30:16
maintainer
docs_urlNone
authorAnthon van der Neut
requires_python>=3
licenseCopyright Ruamel bvba 2007-2023
keywords pypi statistics
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI No Travis.
coveralls test coverage No coveralls.
            
ruamel.ext.msgpack
==================

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

.. image:: https://sourceforge.net/p/ruamel-ext-msgpack/code/ci/default/tree/_doc/_static/license.svg?format=raw
     :target: https://opensource.org/licenses/MIT
.. image:: https://sourceforge.net/p/ruamel-ext-msgpack/code/ci/default/tree/_doc/_static/pypi.svg?format=raw
     :target: https://pypi.org/project/ruamel.ext.msgpack
.. image:: https://sourceforge.net/p/oitnb/code/ci/default/tree/_doc/_static/oitnb.svg?format=raw
   :target: https://pypi.org/project/oitnb/


naive datetime.datetime
+++++++++++++++++++++++

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

.. code:: python


  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:

.. code::

  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``. 

.. code:: python


  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:

.. code::

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


as you can see from the output this will make the instance, that was read back, timezone aware.

use_bin_type=True
++++++++++++++++++

The ``pack`` and ``packb`` routines do not change the default ``use_bin_type=True``.
So the UTF-8 "bytestrings" get dumped as bin 8/16/32 and not as the slightly more
efficient "fixstr" for strings up to a length of 31 bytes.
In the following the function hex() returns a string of hexadecimal values from the bytes array passed into it:

.. code:: python


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

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

``\xb5`` indicates "fixstr" of length 21,
``\xc4\x15`` indicates "bin 8" of length 21

.. code::

  \xb5\xe3\x81\x93\xe3\x82\x93\xe3\x81\xab\xe3\x81\xa1\xe3\x81\xaf\xe4\xb8\x96\xe7\x95\x8c 22
  こんにちは世界
  \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'


If you don't need byte arrays, and want conversion done of ``bytes`` to ``str`` on msgpack-roundtrip,
an alternate version of ``pack``/``unpack``  can be constructed,
that still handles naive datetime objects, and the other types provided by ``ruamel.ext.msgpack``:

.. code:: python


  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=False, datetime=True)
  packb = partial(msgpack.packb, default=msgpack_default, use_bin_type=False, datetime=True)

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

Although packing ``bytes``, now ``\xb5`` indicates "fixstr" of length 21, and the unpacking results in a ``str``:

.. code::

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


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.

.. code:: python


  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:

.. code::

  hex: \xd5\x11\x17\x82 4
  2011-10-02
  msgpack_default.date=17
  hex: \xd5\x11\x17\x82 4
  2011-10-02


            

Raw data

            {
    "_id": null,
    "home_page": "https://sourceforge.net/p/ruamel-ext-msgpack/code/ci/default/tree",
    "name": "ruamel.ext.msgpack",
    "maintainer": "",
    "docs_url": null,
    "requires_python": ">=3",
    "maintainer_email": "",
    "keywords": "pypi statistics",
    "author": "Anthon van der Neut",
    "author_email": "a.van.der.neut@ruamel.eu",
    "download_url": "https://files.pythonhosted.org/packages/b5/94/35e8e0cac3a5d6ac51efe039373e141c373417d88c22140726a65719d037/ruamel.ext.msgpack-0.2.2.tar.gz",
    "platform": null,
    "description": "\nruamel.ext.msgpack\n==================\n\nruamel.ext.msgpack is  a thin wrapper around msgpack, that deals with naive datetime instances and ruamel defined \nextension types (date).\n\n.. image:: https://sourceforge.net/p/ruamel-ext-msgpack/code/ci/default/tree/_doc/_static/license.svg?format=raw\n     :target: https://opensource.org/licenses/MIT\n.. image:: https://sourceforge.net/p/ruamel-ext-msgpack/code/ci/default/tree/_doc/_static/pypi.svg?format=raw\n     :target: https://pypi.org/project/ruamel.ext.msgpack\n.. image:: https://sourceforge.net/p/oitnb/code/ci/default/tree/_doc/_static/oitnb.svg?format=raw\n   :target: https://pypi.org/project/oitnb/\n\n\nnaive datetime.datetime\n+++++++++++++++++++++++\n\nIf you try to pack a naive datetime.datetime instance, you'll get an error:\n\n.. code:: python\n\n\n  import datetime\n  from msgpack import packb\n\n  try:\n      packb(datetime.datetime(2011, 10, 2), datetime=True)\n  except Exception as e:\n      print('exception:', type(e), e)\n\nwhich will print:\n\n.. code::\n\n  exception: <class 'ValueError'> can not serialize 'datetime.datetime' object where tzinfo=None\n\n\nusing ``pack``/``packb`` from ``ruamel.ext.msgpack``  will prevent this from happening, without\nhaving to go over the data structure and replacing all naive ``datetime.datetime`` instances.\nIt will provide both an argument to ``default`` that handles the naive datetimestamp and\nsome Ext types, set ``use_bin_type=True`` and ``datetime=True``. \n\n.. code:: python\n\n\n  import sys\n  import datetime\n  from ruamel.ext.msgpack import packb, unpackb\n\n  res = packb(datetime.datetime(2011, 10, 2, 17, 15, 1))\n  print(unpackb(res))\n\nwhich will print:\n\n.. code::\n\n  2011-10-02 17:15:01+00:00\n\n\nas you can see from the output this will make the instance, that was read back, timezone aware.\n\nuse_bin_type=True\n++++++++++++++++++\n\nThe ``pack`` and ``packb`` routines do not change the default ``use_bin_type=True``.\nSo the UTF-8 \"bytestrings\" get dumped as bin 8/16/32 and not as the slightly more\nefficient \"fixstr\" for strings up to a length of 31 bytes.\nIn the following the function hex() returns a string of hexadecimal values from the bytes array passed into it:\n\n.. code:: python\n\n\n  from ruamel.ext.msgpack import packb, unpackb, hex\n\n  res = packb('\u3053\u3093\u306b\u3061\u306f\u4e16\u754c')\n  print(hex(res), len(res))\n  print(unpackb(res))\n  res = packb('\u3053\u3093\u306b\u3061\u306f\u4e16\u754c'.encode('utf-8'))\n  print(hex(res), len(res))\n  print(unpackb(res))\n\n``\\xb5`` indicates \"fixstr\" of length 21,\n``\\xc4\\x15`` indicates \"bin 8\" of length 21\n\n.. code::\n\n  \\xb5\\xe3\\x81\\x93\\xe3\\x82\\x93\\xe3\\x81\\xab\\xe3\\x81\\xa1\\xe3\\x81\\xaf\\xe4\\xb8\\x96\\xe7\\x95\\x8c 22\n  \u3053\u3093\u306b\u3061\u306f\u4e16\u754c\n  \\xc4\\x15\\xe3\\x81\\x93\\xe3\\x82\\x93\\xe3\\x81\\xab\\xe3\\x81\\xa1\\xe3\\x81\\xaf\\xe4\\xb8\\x96\\xe7\\x95\\x8c 23\n  b'\\xe3\\x81\\x93\\xe3\\x82\\x93\\xe3\\x81\\xab\\xe3\\x81\\xa1\\xe3\\x81\\xaf\\xe4\\xb8\\x96\\xe7\\x95\\x8c'\n\n\nIf you don't need byte arrays, and want conversion done of ``bytes`` to ``str`` on msgpack-roundtrip,\nan alternate version of ``pack``/``unpack``  can be constructed,\nthat still handles naive datetime objects, and the other types provided by ``ruamel.ext.msgpack``:\n\n.. code:: python\n\n\n  from functools import partial\n  from ruamel.ext.msgpack import hex, unpackb, msgpack_default\n  import msgpack\n\n  pack = partial(msgpack.pack, default=msgpack_default, use_bin_type=False, datetime=True)\n  packb = partial(msgpack.packb, default=msgpack_default, use_bin_type=False, datetime=True)\n\n  res = packb('\u3053\u3093\u306b\u3061\u306f\u4e16\u754c'.encode('utf-8'))\n  print(hex(res), len(res))\n  print(unpackb(res))\n\nAlthough packing ``bytes``, now ``\\xb5`` indicates \"fixstr\" of length 21, and the unpacking results in a ``str``:\n\n.. code::\n\n  \\xb5\\xe3\\x81\\x93\\xe3\\x82\\x93\\xe3\\x81\\xab\\xe3\\x81\\xa1\\xe3\\x81\\xaf\\xe4\\xb8\\x96\\xe7\\x95\\x8c 22\n  \u3053\u3093\u306b\u3061\u306f\u4e16\u754c\n\n\nextension types\n+++++++++++++++\n\nThe following extension types are provided by ``ruamel.ext.msgpack``. Each has associated attribute\non ``msgpack_default`` with the type number. This number can be changed, but the same numbers should\nbe used for packing and unpacking. If a number is set to ``None`` the associated type will not be\npacked or unpacked.\nThe type used for naive ``datetime.datetime``,  -1, cannot be changed.\n\ndatetime.date\n^^^^^^^^^^^^^\n\nPython's ``datetime.date`` instances are packed in a two bytes stucture for dates in the range 2000-01-01 and 2126-12-31.\n\n.. code:: python\n\n\n  import datetime\n  from ruamel.ext.msgpack import packb, unpackb, hex, msgpack_default\n\n  res = packb(datetime.date(2011, 10, 2))\n  print('hex:', hex(res), len(res))\n  print(unpackb(res))\n  print(f'{msgpack_default.date=}')\n\n  msgpack_default.date = 42\n  res = packb(datetime.date(2011, 10, 2))\n  print('hex:', hex(res), len(res))\n  print(unpackb(res))\n\n  try:\n      msgpack_default.date = None\n      res = packb(datetime.date(2011, 10, 2))\n  except Exception as e:\n      print('exception:', type(e), e)\n\n\nwhich will print:\n\n.. code::\n\n  hex: \\xd5\\x11\\x17\\x82 4\n  2011-10-02\n  msgpack_default.date=17\n  hex: \\xd5\\x11\\x17\\x82 4\n  2011-10-02\n\n",
    "bugtrack_url": null,
    "license": "Copyright Ruamel bvba 2007-2023",
    "summary": "thin wrapper around msgpack to deal with naive datetime and ruamel defined extension types",
    "version": "0.2.2",
    "project_urls": {
        "Homepage": "https://sourceforge.net/p/ruamel-ext-msgpack/code/ci/default/tree"
    },
    "split_keywords": [
        "pypi",
        "statistics"
    ],
    "urls": [
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "d5ebe25e14f825fe88e5f4f51455db62b7c329b96d2a68eb8aa47f7e382612c5",
                "md5": "e1f085e0871b2df23b36abffae61e378",
                "sha256": "7a0ec51f0a7e7fa0f078bf1d1e1d3f643b055a2446fb230444979253e31f394c"
            },
            "downloads": -1,
            "filename": "ruamel.ext.msgpack-0.2.2-py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "e1f085e0871b2df23b36abffae61e378",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": ">=3",
            "size": 5954,
            "upload_time": "2023-08-05T09:30:18",
            "upload_time_iso_8601": "2023-08-05T09:30:18.461058Z",
            "url": "https://files.pythonhosted.org/packages/d5/eb/e25e14f825fe88e5f4f51455db62b7c329b96d2a68eb8aa47f7e382612c5/ruamel.ext.msgpack-0.2.2-py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "b59435e8e0cac3a5d6ac51efe039373e141c373417d88c22140726a65719d037",
                "md5": "02fe63f8662e60c5dad431abefd0a2b0",
                "sha256": "51e9ebd1d28245a6816eb7aad2f8d672ff921d2c55e864cd68b8cb798f6753d6"
            },
            "downloads": -1,
            "filename": "ruamel.ext.msgpack-0.2.2.tar.gz",
            "has_sig": false,
            "md5_digest": "02fe63f8662e60c5dad431abefd0a2b0",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": ">=3",
            "size": 14747,
            "upload_time": "2023-08-05T09:30:16",
            "upload_time_iso_8601": "2023-08-05T09:30:16.858478Z",
            "url": "https://files.pythonhosted.org/packages/b5/94/35e8e0cac3a5d6ac51efe039373e141c373417d88c22140726a65719d037/ruamel.ext.msgpack-0.2.2.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2023-08-05 09:30:16",
    "github": false,
    "gitlab": false,
    "bitbucket": false,
    "codeberg": false,
    "lcname": "ruamel.ext.msgpack"
}
        
Elapsed time: 0.09932s