Skip to content

Instantly share code, notes, and snippets.

@sahid
Created September 21, 2010 16:52
Show Gist options
  • Save sahid/590028 to your computer and use it in GitHub Desktop.
Save sahid/590028 to your computer and use it in GitHub Desktop.
def prefetch_refprops(entities, *props):
fields = [(entity, prop) for entity in entities for prop in props]
ref_keys = [prop.get_value_for_datastore(x) for x, prop in fields]
# Because if ref_keys has a None value,
# db.get raise a BadArgumentError exception.
for (i, v) in enumerate (ref_keys):
if v is None:
del ref_keys[i]
del fields[i]
ref_entities = dict((x.key(), x) for x in db.get(set(ref_keys)))
for (entity, prop), ref_key in zip(fields, ref_keys):
prop.__set__(entity, ref_entities[ref_key])
return entities
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment