Created
January 7, 2015 09:44
-
-
Save oberstet/6df18395c1c173a1cf89 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import ast | |
import uuid | |
import datetime | |
import pprint | |
import serpent | |
class DemoClass: | |
def __init__(self): | |
self.i=42 | |
self.b=False | |
data_orig = { | |
"names": ["Harry", "Sally", "Peter"], | |
"big": 2**200, | |
"colorset": { "red", "green" }, | |
"id": uuid.uuid4(), | |
"timestamp": datetime.datetime.now(), | |
"class": DemoClass(), | |
} | |
pprint.pprint(data_orig) | |
print "-"*10 | |
# serialize it | |
ser = serpent.dumps(data_orig, indent=True) | |
open("data.serpent", "wb").write(ser) | |
print("Serialized form:") | |
print(ser.decode("utf-8")) | |
# read it back | |
data = serpent.load(open("data.serpent", "rb")) | |
print("Data:") | |
pprint.pprint(data) | |
# you can also use ast.literal_eval if you like | |
ser_string = open("data.serpent", "rb").read() | |
data2 = ast.literal_eval(ser_string) | |
assert data2==data |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment