Google App Engine で Python を使用しているときに、Expando クラス オブジェクトを複製 (コピーを作成) する良い方法は何ですか?
ここでいくつかのコードに出くわしましたが、間違っていない限り、expando プロパティでは機能しません: Copy an entity in Google App Engine datastore in Python without know property names at 'compile' time
ありがとう!
Google App Engine で Python を使用しているときに、Expando クラス オブジェクトを複製 (コピーを作成) する良い方法は何ですか?
ここでいくつかのコードに出くわしましたが、間違っていない限り、expando プロパティでは機能しません: Copy an entity in Google App Engine datastore in Python without know property names at 'compile' time
ありがとう!
動的プロパティを含む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)