METADATA 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. Metadata-Version: 2.1
  2. Name: serpent
  3. Version: 1.40
  4. Summary: Serialization based on ast.literal_eval
  5. Home-page: https://github.com/irmen/Serpent
  6. Author: Irmen de Jong
  7. Author-email: irmen@razorvine.net
  8. License: MIT
  9. Keywords: serialization
  10. Platform: any
  11. Classifier: Development Status :: 5 - Production/Stable
  12. Classifier: Intended Audience :: Developers
  13. Classifier: License :: OSI Approved :: MIT License
  14. Classifier: Natural Language :: English
  15. Classifier: Operating System :: OS Independent
  16. Classifier: Programming Language :: Python
  17. Classifier: Programming Language :: Python :: 3.6
  18. Classifier: Programming Language :: Python :: 3.7
  19. Classifier: Programming Language :: Python :: 3.8
  20. Classifier: Programming Language :: Python :: 3.9
  21. Classifier: Topic :: Software Development
  22. Requires-Python: >=3.2
  23. Serpent is a simple serialization library based on ast.literal_eval.
  24. Because it only serializes literals and recreates the objects using ast.literal_eval(),
  25. the serialized data is safe to transport to other machines (over the network for instance)
  26. and de-serialize it there.
  27. *There is also a Java and a .NET (C#) implementation available. This allows for easy data transfer between the various ecosystems.
  28. You can get the full source distribution, a Java .jar file, and a .NET assembly dll.*
  29. The java library can be obtained from Maven central (groupid ``net.razorvine`` artifactid ``serpent``),
  30. and the .NET assembly can be obtained from Nuget.org (package ``Razorvine.Serpent``).
  31. **API**
  32. - ``ser_bytes = serpent.dumps(obj, indent=False, module_in_classname=False):`` # serialize obj tree to bytes
  33. - ``obj = serpent.loads(ser_bytes)`` # deserialize bytes back into object tree
  34. - You can use ``ast.literal_eval`` yourself to deserialize, but ``serpent.deserialize``
  35. works around a few corner cases. See source for details.
  36. Serpent is more sophisticated than a simple repr() + literal_eval():
  37. - it serializes directly to bytes (utf-8 encoded), instead of a string, so it can immediately be saved to a file or sent over a socket
  38. - it encodes byte-types as base-64 instead of inefficient escaping notation that repr would use (this does mean you have
  39. to base-64 decode these strings manually on the receiving side to get your bytes back.
  40. You can use the serpent.tobytes utility function for this.)
  41. - it contains a few custom serializers for several additional Python types such as uuid, datetime, array and decimal
  42. - it tries to serialize unrecognised types as a dict (you can control this with __getstate__ on your own types)
  43. - it can create a pretty-printed (indented) output for readability purposes
  44. - it outputs the keys of sets and dicts in alphabetical order (when pretty-printing)
  45. - it works around a few quirks of ast.literal_eval() on the various Python implementations
  46. Serpent allows comments in the serialized data (because it is just Python source code).
  47. Serpent can't serialize object graphs (when an object refers to itself); it will then crash with a ValueError pointing out the problem.
  48. Works with Python 3 recent versions.
  49. **FAQ**
  50. - Why not use XML? Answer: because XML.
  51. - Why not use JSON? Answer: because JSON is quite limited in the number of datatypes it supports, and you can't use comments in a JSON file.
  52. - Why not use pickle? Answer: because pickle has security problems.
  53. - Why not use ``repr()``/``ast.literal_eval()``? See above; serpent is a superset of this and provides more convenience.
  54. Serpent provides automatic serialization mappings for types other than the builtin primitive types.
  55. ``repr()`` can't serialize these to literals that ``ast.literal_eval()`` understands.
  56. - Why not a binary format? Answer: because binary isn't readable by humans.
  57. - But I don't care about readability. Answer: doesn't matter, ``ast.literal_eval()`` wants a literal string, so that is what we produce.
  58. - But I want better performance. Answer: ok, maybe you shouldn't use serpent in this case. Find an efficient binary protocol (protobuf?)
  59. - Why only Python, Java and C#/.NET, but no bindings for insert-favorite-language-here? Answer: I don't speak that language.
  60. Maybe you could port serpent yourself?
  61. - Where is the source? It's on Github: https://github.com/irmen/Serpent
  62. - Can I use it everywhere? Sure, as long as you keep the copyright and disclaimer somewhere. See the LICENSE file.
  63. **Demo**
  64. .. code:: python
  65. # -*- coding: utf-8 -*-
  66. import ast
  67. import uuid
  68. import datetime
  69. import pprint
  70. import serpent
  71. class DemoClass:
  72. def __init__(self):
  73. self.i=42
  74. self.b=False
  75. data = {
  76. "names": ["Harry", "Sally", "Peter"],
  77. "big": 2**200,
  78. "colorset": { "red", "green" },
  79. "id": uuid.uuid4(),
  80. "timestamp": datetime.datetime.now(),
  81. "class": DemoClass(),
  82. "unicode": "€"
  83. }
  84. # serialize it
  85. ser = serpent.dumps(data, indent=True)
  86. open("data.serpent", "wb").write(ser)
  87. print("Serialized form:")
  88. print(ser.decode("utf-8"))
  89. # read it back
  90. data = serpent.load(open("data.serpent", "rb"))
  91. print("Data:")
  92. pprint.pprint(data)
  93. # you can also use ast.literal_eval if you like
  94. ser_string = open("data.serpent", "r", encoding="utf-8").read()
  95. data2 = ast.literal_eval(ser_string)
  96. assert data2==data
  97. When you run this it prints:
  98. .. code:: python
  99. Serialized form:
  100. # serpent utf-8 python3.2
  101. {
  102. 'big': 1606938044258990275541962092341162602522202993782792835301376,
  103. 'class': {
  104. '__class__': 'DemoClass',
  105. 'b': False,
  106. 'i': 42
  107. },
  108. 'colorset': {
  109. 'green',
  110. 'red'
  111. },
  112. 'id': 'e461378a-201d-4844-8119-7c1570d9d186',
  113. 'names': [
  114. 'Harry',
  115. 'Sally',
  116. 'Peter'
  117. ],
  118. 'timestamp': '2013-04-02T00:23:00.924000',
  119. 'unicode': '€'
  120. }
  121. Data:
  122. {'big': 1606938044258990275541962092341162602522202993782792835301376,
  123. 'class': {'__class__': 'DemoClass', 'b': False, 'i': 42},
  124. 'colorset': {'green', 'red'},
  125. 'id': 'e461378a-201d-4844-8119-7c1570d9d186',
  126. 'names': ['Harry', 'Sally', 'Peter'],
  127. 'timestamp': '2013-04-02T00:23:00.924000',
  128. 'unicode': '€'}