Skip to content

Instantly share code, notes, and snippets.

@javi830810
Last active August 29, 2015 14:05
Show Gist options
  • Save javi830810/bf0f1e384dc345aa4b63 to your computer and use it in GitHub Desktop.
Save javi830810/bf0f1e384dc345aa4b63 to your computer and use it in GitHub Desktop.
Clone Entity
def clone_entity_ndb(e, **extra_args):
"""use Populate method
"""
pass
def clone_entity_db(e, **extra_args):
"""Clones an entity, adding or overriding constructor attributes.
The cloned entity will have exactly the same property values as the original
entity, except where overridden. By default it will have no parent entity or
key name, unless supplied.
Args:
e: The entity to clone
extra_args: Keyword arguments to override from the cloned entity and pass
to the constructor.
Returns:
A cloned, possibly modified, copy of entity e.
"""
klass = e.__class__
props = dict((k, v.__get__(e, klass)) for k, v in klass.properties().iteritems())
props.update(extra_args)
return klass(**props)
@javi830810
Copy link
Author

Clone Entity for ndb

def clone_entity(e, **extra_args):
"""Clones an entity, adding or overriding constructor attributes.

The cloned entity will have exactly the same property values as the original
entity, except where overridden. By default it will have no parent entity or
key name, unless supplied.

Args:
e: The entity to clone
extra_args: Keyword arguments to override from the cloned entity and pass
to the constructor.
Returns:
A cloned, possibly modified, copy of entity e.
"""
klass = e.class
props = e.to_dict()
props.update(extra_args)
return klass(**props)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment