How to decrypt a Google App Engine object? Key path in Python?

In the Google App Engine, an object has a key. The key can be made from the path, in which case str (the key) is an opaque hexadecimal string. Example:

from google.appengine.ext import db foo = db.Key.from_path(u'foo', u'bar', _app=u'baz') print foo 

gives

 agNiYXpyDAsSA2ZvbyIDYmFyDA 

if you configured the correct paths to run the code.

So, how can I take the sixth line and get the way back? I thought the answer would be in Key or entity group docs, but I do not see this.

+6
python google-app-engine
source share
2 answers
 from google.appengine.ext import db k = db.Key('agNiYXpyDAsSA2ZvbyIDYmFyDA') _app = k.app() path = [] while k is not None: path.append(k.id_or_name()) path.append(k.kind()) k = k.parent() path.reverse() print 'app=%r, path=%r' % (_app, path) 

when launched in the Development Console, it produces:

 app=u'baz', path=[u'foo', u'bar'] 

upon request. A shorter alternative is to use the to_path method of to_path instances for Key (unfortunately, mistrust)

 k = db.Key('agNiYXpyDAsSA2ZvbyIDYmFyDA') _app = k.app() path = k.to_path() print 'app=%r, path=%r' % (_app, path) 

with the same results. But the first, longer version depends only on documented methods.

+7
source share

Once you have a Key object (which can be created by passing this opaque identifier to the constructor), use Key.to_path() to get the path to Key as a list. For example:

 from google.appengine.ext import db opaque_id = 'agNiYXpyDAsSA2ZvbyIDYmFyDA' path = db.Key(opaque_id).to_path() 
+2
source share

All Articles