gpt4 book ai didi

python - 使用 PickleProperty 在 GAE 中存储字典

转载 作者:太空宇宙 更新时间:2023-11-04 08:17:55 25 4
gpt4 key购买 nike

我有一个 python 字典,我试图通过 pickle 属性存储在 GAE Gql 数据存储中。

我试着按照这个问题和答案, Store a list of dictionaries in GAE

但它不适用于我下面的代码。我收到一条错误消息,提示我的列表有问题

我如下设置 pickle 属性和数据模型类,

class PickleProperty(db.Property):
def get_value_for_datastore(self, model_instance):
value = getattr(model_instance, self.name, None)
return pickle.dumps(value)

def make_value_from_datastore(self, value):
return pickle.mloads(value)

class MDB(db.Model):
Name = db.StringProperty(required=True)
Times = PickleProperty()
created = db.DateTimeProperty(auto_now_add = True)

我的代码中有一个函数可以插入数据并将其读出

def m_time_manage(m=""):

if not m:
r = db.GqlQuery("select * from MDB")
else:
#find specific masjid requested
r = db.GqlQuery("select * from MDB where Name = %s" % (s))

#if masjid time data doesn't exist in db throw in placeholders...
if r is None:
times = {}
times['a'] = "8:49"
times['b'] = "10:19"

times_entry = MDB(Name="abc",Times=times)
times_entry.put()

r = db.GqlQuery("select * from MasjidDB")
return (r[0].Name, r[0].Times)

在我的 get 函数中,我调用 m_time_manage() ftn 如下,

(mName, times) = m_time_manage()
self.write(times)

我得到这个错误,pickle.loads 和文件结束错误...完整的错误是,但我不确定如何在 python 中存储字典,

 line 31, in make_value_from_datastore
return pickle.loads(value)
File "/usr/lib/python2.7/pickle.py", line 1382, in loads
return Unpickler(file).load()
File "/usr/lib/python2.7/pickle.py", line 858, in load
dispatch[key](self)
File "/usr/lib/python2.7/pickle.py", line 880, in load_eof
raise EOFError

提前感谢您的帮助

最佳答案

我建议切换到 NDB 并使用其内置的 PickleProperty。参见 https://developers.google.com/appengine/docs/python/ndb/properties

关于python - 使用 PickleProperty 在 GAE 中存储字典,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10842693/

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