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"}'
Release files for coercion 1.0.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| coercion-1.0.0.tar.gz | 7.9 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| coercion-1.0.0-py2.py3-none-any.whl | Python 3, Python 2 | none | any | Details |
Total release size: 14.9 kB
Release files / coercion-1.0.0.tar.gz
| Download URL | coercion-1.0.0.tar.gz |
|---|---|
| Size | 7.9 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
cd1c27a47b26d0458d86ebb7c1d85838a63e37ba1c019531f9265b290031fe2c
|
|
BLAKE2b-256 checksum How to use checksums |
7a97f1965afc2ae27074a05e5201236540364f53eff24c26d6ee04aa81d53b80
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
Release files / coercion-1.0.0-py2.py3-none-any.whl
| Download URL | coercion-1.0.0-py2.py3-none-any.whl |
|---|---|
| Size | 7.0 kB |
| Tags | Python 2 Python 3 |
|
SHA-256 checksum How to use checksums |
0ede1ee36a6aa5f44333c1354968ef20031b4e513d4e33b180578c4d9d3c47bd
|
|
BLAKE2b-256 checksum How to use checksums |
041a67ea039a9d19f2a3bb26c8dfd1f0a6786c1d11cef3bb4e5e542479352859
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |