Skip to content

Instantly share code, notes, and snippets.

@sahid
Created November 25, 2010 16:22
Show Gist options
  • Save sahid/715591 to your computer and use it in GitHub Desktop.
Save sahid/715591 to your computer and use it in GitHub Desktop.
serialize and deserialze model with GAE
# 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