Serpent is a simple serialization library for Python based on `ast.literal_eval`. It serializes object trees into a safe, human-readable, UTF-8 encoded string (a valid Python literal expression), suitable for data interchange between Python, Java, and .NET. As of version 1.42, it actively maintains support for modern Python versions with a release cadence of a few months to a year.
pip install serpentVerified import paths — ran on the pinned version, not inferred.
This quickstart demonstrates basic serialization and deserialization of a Python dictionary containing various literal types. The `indent=True` option is used for human-readable output, and the data is converted to a UTF-8 string for printing.
Do not deserialize data from untrusted sources. If you must process untrusted data, implement strict input validation and consider applying resource limits to prevent DoS attacks.
After `loads()`, inspect string fields for base-64 encoded byte data and use `serpent.tobytes()` to convert them. Or, serialize with `dumps(obj, bytes_repr=True)` and handle the larger output.
Ensure that the object tree you are serializing does not contain any circular references. You may need to preprocess your data structure to break such cycles.
Upgrade to Serpent version 1.41 or newer if you are using Python 3.11+ and rely on custom `__getstate__` implementations for your serializable objects.
For concurrent serialization, create a new `dumps` call (or ensure a new serializer instance is implicitly used) for each thread, or synchronize access to the object being serialized and the serializer instance.
Convert the unsupported object into a serializable type (e.g., convert a `set` to a `list`) before passing it to `serpent.dumps()`.
```python
import serpent
my_set = {1, 2, 3}
serialized_data = serpent.dumps(list(my_set))
# To deserialize back to a set:
deserialized_set = set(serpent.loads(serialized_data))
```Ensure the input string is a well-formed Python literal expression, typically by generating it with `serpent.dumps()` or by carefully constructing it according to Python's literal syntax.
```python
import serpent
# Incorrect input (e.g., missing closing bracket or invalid syntax)
# serpent.loads("[1, 2, 3")
# Correct input
data_string = serpent.dumps([1, 2, 3])
deserialized_data = serpent.loads(data_string)
print(deserialized_data)
```Install the `serpent` package using pip. ```bash pip install serpent ```
Ensure that the data being passed to `serpent.loads()` is a string correctly decoded as UTF-8. If reading from a file, explicitly specify `encoding='utf-8'`.
```python
import serpent
# If reading from a file, ensure correct encoding:
# with open('my_data.serpent', 'r', encoding='utf-8') as f:
# serpent_string = f.read()
# data = serpent.loads(serpent_string)
# If you have bytes, decode them as UTF-8 before passing to loads:
encoded_bytes = b'{"key": "value"}' # Example valid UTF-8 bytes
data = serpent.loads(encoded_bytes.decode('utf-8'))
print(data)
```No dependency data recorded yet.