Coercing data into a normalized form
Project description
This library provides functions that coerce datastructures into normalized forms. For example, converting an arbitrary dict into a form that is suitable for passing to json.dumps.
The tornado framework has a function called recursive_unicode in the tornado.escape module. It is a very simple recursive walk of datastructure that switches on type and transforms string values into unicode strings. I use this in production software regularly and it works like a charm. Or at least it did until my software encountered a deeply nested dictionary and I received a RuntimeError: maximum recursion depth exceeded error in my service log. This is one of the exceptions that strikes fear into most engineers when it rears it’s head in production.
That is the primary reason for this library existing. It provides the same simple string encoding function iteratively instead of recursively. At the same time, the need to coerce values into a normalized string form is something that I’ve had to do repeatedly so it might as well be plopped into a reusable library.
Examples
The following example shows one of the underlying reasons that this library was created. The commonly used msgpack implementation for python returns everything as byte strings which is problematic if you want to dump it as JSON since it will raise a TypeError if dictionary keys are not strings. (This is where recursive_unicode was so handy.)
>>> import json
>>> import coercion
>>> import msgpack
>>> bin_msg = msgpack.packb({u'\u00DCnicode': b'bytes', b'bytes': 'str'})
>>> decoded = msgpack.unpackb(bin_msg)
>>> decoded
{b'bytes': b'str', b'\xc3\x9cnicode': b'bytes'}
>>> json.dumps(decoded)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/Users/daveshawley/opt/lib/python3.5/json/__init__.py", line 230, in dumps
return _default_encoder.encode(obj)
File "/Users/daveshawley/opt/lib/python3.5/json/encoder.py", line 199, in encode
chunks = self.iterencode(o, _one_shot=True)
File "/Users/daveshawley/opt/lib/python3.5/json/encoder.py", line 257, in iterencode
return _iterencode(o, 0)
TypeError: keys must be a string
>>> json.dumps(coercion.normalize_collection(decoded))
'{"bytes": "str", "\\u00dcnicode": "bytes"}'
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
File details
Details for the file coercion-1.0.0.tar.gz
.
File metadata
- Download URL: coercion-1.0.0.tar.gz
- Upload date:
- Size: 7.9 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | cd1c27a47b26d0458d86ebb7c1d85838a63e37ba1c019531f9265b290031fe2c |
|
MD5 | 07510272def74390b77b12f7df0245cf |
|
BLAKE2b-256 | 7a97f1965afc2ae27074a05e5201236540364f53eff24c26d6ee04aa81d53b80 |
File details
Details for the file coercion-1.0.0-py2.py3-none-any.whl
.
File metadata
- Download URL: coercion-1.0.0-py2.py3-none-any.whl
- Upload date:
- Size: 7.0 kB
- Tags: Python 2, Python 3
- Uploaded using Trusted Publishing? No
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 0ede1ee36a6aa5f44333c1354968ef20031b4e513d4e33b180578c4d9d3c47bd |
|
MD5 | e6c9f1046a46c6c31084c439d7e7e840 |
|
BLAKE2b-256 | 041a67ea039a9d19f2a3bb26c8dfd1f0a6786c1d11cef3bb4e5e542479352859 |