在谷歌应用程序引擎上使用Python时,克隆(复制)Expando类对象的好方法是什么?
我在这里遇到了一些代码,但除非我错了,它不能在扩展属性上工作:在Python中复制Google App Engine数据存储中的实体而不知道属性名称'compile'时间
谢谢!
Nick函数的修改版本包含了动态属性:
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)