如何解码Google App Engine实体Python中的关键路径str? [英] How to decode a Google App Engine entity Key path str in Python?

查看:111
本文介绍了如何解码Google App Engine实体Python中的关键路径str?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Google App Engine中,实体拥有一个密钥。一个键可以由一个路径构成,在这种情况下,str(key)是一个不透明的十六进制字符串。示例:

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

/ p>

  agNiYXpyDAsSA2ZvbyIDYmFyDA 



如果你设置了正确的路径来运行代码。

那么,如何获取十六进制字符串并返回路径呢?我认为答案将在关键 entity group docs,但我看不到它。

解决方案

  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,路径)

在开发控制台中运行时,会输出:

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

按要求。更简单的选择是使用 Key 实例的(不幸的是,我相信,未记录) to_path 方法:

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

具有相同的结果。但第一个更长的版本仅依赖于记录的方法。


In Google App Engine, an entity has a Key. A key can be made from a path, in which case str(key) is an opaque hex 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 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.

解决方案

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:

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.

这篇关于如何解码Google App Engine实体Python中的关键路径str?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆