gpt4 book ai didi

python - App Engine 中 Python 中的克隆和 Expando 类对象

转载 作者:行者123 更新时间:2023-12-01 06:11:02 24 4
gpt4 key购买 nike

在 Google App Engine 上使用 Python 时,克隆(制作副本)Expando 类对象的好方法是什么?

我在这里遇到了一些代码,但除非我弄错了,否则它不适用于扩展属性:Copy an entity in Google App Engine datastore in Python without knowing property names at 'compile' time

谢谢!

最佳答案

这是 Nick's function 的修订版本其中包括动态属性:

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)

关于python - App Engine 中 Python 中的克隆和 Expando 类对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5879301/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com