DEPRECATED: Use glibs-ndb instead.
Very small library for parsing AppEngine keys as urlsafes when outside the AppEngine env.
Note: The code here belongs to Google and was modified to suit our needs.
Install directly from this repository.
$ pip install https://github.com/projetoeureka/ndb-key-parser.gitOr use with a tool like poetry:
[tool.poetry.dependencies]
# ...
glibs-ndbkeyparser = { git = "https://github.com/projetoeureka/ndb-key-parser.git" }There are two main usages of ndb.Key(): to get the ID from an urlsafe string, or to get the urlsafe string from an ID.
-
ndb.Key(urlsafe=urlsafe, app=app)from glibs.ndbkeyparser import ndb key = ndb.Key(urlsafe="<urlsafe string>", app="<app name>") key.id()
-
ndb.Key(kind, id, app=app)from glibs.ndbkeyparser import ndb key = ndb.Key("Kind", 123, app="<app name>") key.urlsafe()
It's a convenience class to ensure you're working with an ID or an urlsafe string, in cases you might get both.
-
ensure_key(urlsafe_or_id, kind=None)-> returns an urlsafeIf
urlsafe_or_idis an urlsafe string, it will only check if the kind matches (if passed).if
urlsafe_or_idis an ID, thenkindcan't beNoneand it will build the Key and return its corresponding urlsafe.from glibs.ndbkeyparser import ndb helper = ndb.ConverterHelper("<app name>") urlsafe = helper.ensure_key(urlsafe_or_id, kind="Kind")
-
ensure_id(urlsafe_or_id)-> returns a numerical ID (as string)If
urlsafe_or_idis an urlsafe string, it will build the Key and return its id.if
urlsafe_or_idis an ID, it will just cast to string (if necessary).from glibs.ndbkeyparser import ndb helper = ndb.ConverterHelper("<app name>") urlsafe = helper.ensure_key(urlsafe_or_id, kind="Kind")