Created
November 25, 2010 16:22
-
-
Save sahid/715591 to your computer and use it in GitHub Desktop.
serialize and deserialze model with GAE
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
# Largely inspired by: http://blog.notdot.net/2009/9/Efficient-model-memcaching | |
def serialize(models): | |
if isinstance(models, db.Model): | |
return db.model_to_protobuf(models).Encode() | |
elif isinstance(models, list): | |
return [serialize(x) for x in models] | |
elif isinstance(models, dict): | |
return dict([(k, serialize(x)) for k, x in models.items()]) | |
def deserialize(data): | |
if isinstance(data, basestring): | |
return db.model_from_protobuf(datastore.entity_pb.EntityProto(data)) | |
elif isinstance(data, list): | |
return [deserialize (x) for x in data] | |
elif isinstance(data, dict): | |
return dict([(k, deserialize(x)) for k, x in data.items()]) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment