开发者

Clone and Expando class object in Python for App Engine

What is a good way to clone (make a copy of) an Expando class object when using Python on Google App Engine?

I came across some code on here, but unless I'm mistaken, it does not work on expando properties开发者_Python百科: Copy an entity in Google App Engine datastore in Python without knowing property names at 'compile' time

Thanks!


Here's a revised version of Nick's function that includes dynamic properties:

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 = dict((k, v.__get__(e, klass)) for k, v in klass.properties().iteritems())
  props.update(dict([(k, getattr(e, k)) for k in e.dynamic_properties()]))
  props.update(extra_args)
  return klass(**props)
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜