在GAE ndb上腌制

时间:2013-05-14 04:31:59

标签: google-app-engine pickle app-engine-ndb defaultdict

我正在尝试将结构化数据pickle and unpickle到ndb.PickleProperty()属性中,如下所示:

month = MonthRecord.get_or_insert(month_yr_str, parent=ndb.Key('Type','Grocery'), record=pickle.dumps(defaultdict(list_list)))
names_dict = pickle.loads(month.record) # unpickle for updating
# ...                                   # some modifications on names_dict
month.record = pickle.dumps(names_dict) # pickle
month.put()                             # commit changes

其中模型MonthRecord定义为:

class MonthRecord(ndb.Model):
    record = ndb.PickleProperty() # {name: [[date&time],[expenses]]}

和list_list as:

def list_list(): # placeholder function needed by pickle at module level
    return [[],[]]

第一次运行正常(在get_or_insert中命中insert case,创建一个新的MonthRecord实体)。但是,在成功运行期间(即要记录的当月内的新费用),会发生以下错误:

Traceback (most recent call last):
  File "C:\GAE_Projects\qb_lite\fin.py", line 31, in update_db
    names_dict = pickle.loads(month.record)
  File "C:\Python27\lib\pickle.py", line 1382, in loads
    return Unpickler(file).load()
  File "C:\Python27\lib\pickle.py", line 858, in load
    dispatch[key](self)
  File "C:\Python27\lib\pickle.py", line 1133, in load_reduce
    value = func(*args)
TypeError: __init__() takes exactly 4 arguments (1 given)

关于错误原因的任何想法?

1 个答案:

答案 0 :(得分:2)

你不必腌制你的物品,这些物品将由PickleProperty处理。

而不是:

month = MonthRecord.get_or_insert(
    month_yr_str,
    parent=ndb.Key('Type','Grocery'),
    record=pickle.dumps(defaultdict(list_list)))

做的:

month = MonthRecord.get_or_insert(
    month_yr_str,
    parent=ndb.Key('Type','Grocery'),
    record=defaultdict(list_list))

酸洗和去皮将在内部处理。