【发布时间】:2018-06-13 14:42:30
【问题描述】:
尝试使用 collections.defaultdict() 在 google-app-engine 中创建直方图:
class myDS(ndb.Model):
values = ndb.PickleProperty()
hist = ndb.PickleProperty()
class Handler:
my_ds = myDS()
my_ds.values = {}
my_ds.hist = defaultdict(lambda : 0)
并得到错误(来自日志)
File "/base/alloc/tmpfs/dynamic_runtimes/python27/277b61042b697c7a_unzipped/python27_lib/versions/1/google/appengine/ext/ndb/model.py", line 1331, in call
newvalue = method(self, value)
File "/base/alloc/tmpfs/dynamic_runtimes/python27/277b61042b697c7a_unzipped/python27_lib/versions/1/google/appengine/ext/ndb/model.py", line 1862, in _to_base_type
return pickle.dumps(value, pickle.HIGHEST_PROTOCOL)
PicklingError: Can't pickle <type 'function'>: attribute lookup __builtin__.function failed
有什么办法解决吗?
【问题讨论】:
-
请注意,
ndb仅在标准 GAE 环境中受支持,该环境仅适用于 python 2.7。不知道这是否与您的问题有关。 -
@DanCornilescu defaultdict 应该包含在 python2.7 中。docs.python.org/2/library/…
-
能否提供日志中的追溯信息?
-
如果你这样做
my_ds.hist = defaultdict(int)是否有效?这在本地对我有用,但我还没有在云中尝试过。
标签: python-3.x google-app-engine app-engine-ndb google-app-engine-python defaultdict