
Security News
MCP Community Begins Work on Official MCP Metaregistry
The MCP community is launching an official registry to standardize AI tool discovery and let agents dynamically find and install MCP servers.
Serpent is a simple serialization library based on ast.literal_eval.
Because it only serializes literals and recreates the objects using ast.literal_eval(), the serialized data is safe to transport to other machines (over the network for instance) and de-serialize it there.
There is also a Java and a .NET (C#) implementation available. This allows for easy data transfer between the various ecosystems.
You can get the full source distribution, a Java .jar file, and a .NET assembly dll.
The java library can be obtained from Maven central (groupid net.razorvine
artifactid serpent
),
and the .NET assembly can be obtained from Nuget.org (package Razorvine.Serpent
).
API
ser_bytes = serpent.dumps(obj, indent=False, module_in_classname=False):
# serialize obj tree to bytesobj = serpent.loads(ser_bytes)
# deserialize bytes back into object treeast.literal_eval
yourself to deserialize, but serpent.deserialize
works around a few corner cases. See source for details.Serpent is more sophisticated than a simple repr() + literal_eval():
Serpent allows comments in the serialized data (because it is just Python source code). Serpent can't serialize object graphs (when an object refers to itself); it will then crash with a ValueError pointing out the problem.
Works with Python 3 recent versions.
FAQ
repr()
/ast.literal_eval()
? See above; serpent is a superset of this and provides more convenience.
Serpent provides automatic serialization mappings for types other than the builtin primitive types.
repr()
can't serialize these to literals that ast.literal_eval()
understands.ast.literal_eval()
wants a literal string, so that is what we produce.Demo
.. code:: python
import ast import uuid import datetime import pprint import serpent
class DemoClass: def init(self): self.i=42 self.b=False
data = { "names": ["Harry", "Sally", "Peter"], "big": 2**200, "colorset": { "red", "green" }, "id": uuid.uuid4(), "timestamp": datetime.datetime.now(), "class": DemoClass(), "unicode": "€" }
ser = serpent.dumps(data, indent=True) open("data.serpent", "wb").write(ser)
print("Serialized form:") print(ser.decode("utf-8"))
data = serpent.load(open("data.serpent", "rb")) print("Data:") pprint.pprint(data)
ser_string = open("data.serpent", "r", encoding="utf-8").read() data2 = ast.literal_eval(ser_string)
assert data2==data
When you run this it prints:
.. code:: python
Serialized form:
{ 'big': 1606938044258990275541962092341162602522202993782792835301376, 'class': { 'class': 'DemoClass', 'b': False, 'i': 42 }, 'colorset': { 'green', 'red' }, 'id': 'e461378a-201d-4844-8119-7c1570d9d186', 'names': [ 'Harry', 'Sally', 'Peter' ], 'timestamp': '2013-04-02T00:23:00.924000', 'unicode': '€' } Data: {'big': 1606938044258990275541962092341162602522202993782792835301376, 'class': {'class': 'DemoClass', 'b': False, 'i': 42}, 'colorset': {'green', 'red'}, 'id': 'e461378a-201d-4844-8119-7c1570d9d186', 'names': ['Harry', 'Sally', 'Peter'], 'timestamp': '2013-04-02T00:23:00.924000', 'unicode': '€'}
FAQs
Serialization based on ast.literal_eval
We found that serpent demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
The MCP community is launching an official registry to standardize AI tool discovery and let agents dynamically find and install MCP servers.
Research
Security News
Socket uncovers an npm Trojan stealing crypto wallets and BullX credentials via obfuscated code and Telegram exfiltration.
Research
Security News
Malicious npm packages posing as developer tools target macOS Cursor IDE users, stealing credentials and modifying files to gain persistent backdoor access.