Serialize and deserialize data to python source notation (hbn)
Project description
Hiss Byte Notation
Library to make it easy to use python literal syntax as a data format
Have you seen people try to print a dict and then use the JSON library to parse the output? This library is some helper function for that scenario. It is a small wrapper around ast.literal_eval and will have a API similar to other serializer/deserializers such as json, pickle, pyyaml, etc.
Safety
ast.literal_eval
is safer than eval
but the python docs still imply that there are malicious payloads. I'm not
sure if they are the same problems that could affect json or other formats.
Usage
import hissbytenotation as hbn
data = {
"mammal": "cat",
"reptile": ["snake", "lizard"],
"version": 1
}
data_as_string = hbn.dumps(data)
rehydrated = hbn.loads(data_as_string)
print(rehydrated)
# {'mammal': 'cat', 'reptile': ['snake', 'lizard'], 'version': 1}
How it works
Serialization is done by calling repr, checking if ast.literal_eval can read it. Repr can be called on more data structures than ast.literal_eval can handle.
Because ast.literal_eval is so slow, there are other options for deserialization:
- default: ast.literal_eval with validation enabled. Very slow, very safe.
- eval. Slow, only for trusted data.
- exec. Slow, only for trusted data.
- import. Two times faster than exec, only for trusted data.
Serialization and deserialization times for 10,000 dumps/loads
Pickle: 0.89 seconds
JSON: 2.00 seconds
HBN (no validation): 20.80 seconds
HBN (validation): 40.57 seconds
HBN (unsafe): 15.95 seconds
HBN (exec): 18.08 seconds
HBN (by import): 12.26 seconds
Prior art
-
literal-python-to-pickle A faster replacement for ast.literal_eval and corresponding question on stackoverflow.
-
You could just call
repr
andast.literal_eval
directly.
Possibly astor which serializes to a string representation of the AST, which looks nothing like the source code, nor json.
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 hissbytenotation-0.2.0.tar.gz
.
File metadata
- Download URL: hissbytenotation-0.2.0.tar.gz
- Upload date:
- Size: 5.4 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/4.0.2 CPython/3.11.4
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 34787e8119526374bd75bce45766939f816f23a2fac3b1d027c21cbadd31b104 |
|
MD5 | d0813186fc304f997375b0bff8addda7 |
|
BLAKE2b-256 | e5e3e50af7af1e77ae6c6063194029ccf3af60df631b1f7030eb8e4db38cd6d7 |
File details
Details for the file hissbytenotation-0.2.0-py3-none-any.whl
.
File metadata
- Download URL: hissbytenotation-0.2.0-py3-none-any.whl
- Upload date:
- Size: 5.8 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/4.0.2 CPython/3.11.4
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 7d85ad4f0b9587b256818c0c5f8da14d60306a78577419eedc064fce5a1000da |
|
MD5 | 23a0e2341201fc0e8ee35eee4bff46a2 |
|
BLAKE2b-256 | a95a626f46d664637699bf3d04305d422a95fa0b12d346634c7c96c6eb1cdc6f |