Last active
August 29, 2015 14:05
-
-
Save javi830810/bf0f1e384dc345aa4b63 to your computer and use it in GitHub Desktop.
Clone Entity
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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)