简体   繁体   English

如何在Python中解码Google App Engine实体密钥路径str?

[英]How to decode a Google App Engine entity Key path str in Python?

In Google App Engine, an entity has a Key. 在Google App Engine中,实体具有密钥。 A key can be made from a path, in which case str(key) is an opaque hex string. 可以从路径创建密钥,在这种情况下,str(密钥)是不透明的十六进制字符串。 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 set up the right paths to run the code. 如果您设置正确的路径来运行代码。

So, how can one take the hex string and get the path back? 那么,如何才能获取十六进制字符串并获取路径? I thought the answer would be in Key or entity group docs, but I can't see it. 我认为答案将在Key实体组文档中,但我看不到它。

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 run in a Development Console, this outputs: 在开发控制台中运行时,输出:

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

as requested. 按照要求。 A shorter alternative is to use the (unfortunately, I believe, undocumented) to_path method of Key instances: 更短的替代方法是使用Key实例的(不幸的是,我相信,未记录的) to_path方法:

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 relies only on documented methods. 但是第一个更长的版本仅依赖于记录的方法。

Once you have the Key object (which can be created by passing that opaque identifier to the constructor), use Key.to_path() to get the path of a Key as a list. 获得Key对象(可以通过将该不透明标识符传递给构造函数来创建)后,使用Key.to_path()Key的路径作为列表获取。 For example: 例如:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在Python App Engine中按实体键名过滤 - Filtering by entity key name in Google App Engine on Python 使用表单字段作为实体键google app engine python - Use a form field as the entity key google app engine python 如何检查单个实体的存在? Google App Engine,Python - How to check the existance of single Entity? Google App Engine, Python 如何在没有在Google App Engine中读取数据存储的情况下从实体实例获取引用的实体属性的键名? - How to get to the key name of a referenced entity property from an entity instance without a datastore read in google app engine? 如何在Python / Google App Engine中获取键名列表? - How to get a list of key names in Python/Google app engine? Google App Engine启动器Python路径 - Google App Engine Launcher Python Path Google App Engine:获取要在模板中使用的实体密钥 - Google App Engine: Get entity key to use in a template 从Google App Engine数据存储区中的密钥获取实体属性 - Getting entity properties from Key in google app engine datastore 如何在App Engine中解码JPG中的像素(使用纯Python)? - How to decode the pixels in a JPG in App Engine (using pure python)? 如何为我的Google App Engine应用设置PYTHON_PATH变量? - How do I set the PYTHON_PATH variable for my Google App Engine app?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM