NDB

2013-05-14 32 views
1

我试图酸洗和unpickle结构化数据上酸洗GAE成像这样的ndb.PickleProperty()属性:NDB

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为:

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

第一次运行正常工作(其中插入的情况下被击中的get_or_insert,创建一个新的MonthRe绳索实体)。然而,随后的运行期间(本月内即新的费用记录)出现以下错误:

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) 

任何想法,错误的原因是什么?

回答

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)) 

封装状态将采取的内部照顾。

+1

验证是否有额外的/多余的酸洗,代码有效。问题的真正原因是带有tzinfo的日期时间对象,由于TimeZone未在模块级别定义,因此无法正确采样。通过删除日期时间对象中的tzinfo来修复。当我除去多余的酸洗层时,发现根本原因。现在正确地使用PickleProperty。谢谢。 – silvernightstar 2013-05-14 11:57:39