2017-03-24 8 views
1

Google App Engine의 NDB 데이터 저장소의 비동기 API와 함께 사용하기 위해 작성한 일부 태스크 릿에서 무슨 일이 벌어지고 있는지 이해하는 데 어려움이 있습니다. 개요는 다음과 같습니다. 사용자의 구매 내역을 수집해야하며 "UserPurchase"개체는 구입 한 "제품"또는 "사용자 지정"항목과 구입 한 "설치"항목을 참조하며 둘 다 KeyProperty UserPurchase ndb.Model의 속성. 나는 가능한 한 다양한 NDB 조회를 병렬화 할, 그래서 (https://cloud.google.com/appengine/docs/standard/python/ndb/async에 위치) NDB 문서에 매우 가까이 붙어 3 개 태스크 릿 구축했습니다 : 그런 다음ndb.tasklets을 Google App Engine에서 작동시키기가 어렵습니다.

@ndb.tasklet 
def get_establishment(purch): 
    resto = yield purch.p_establishment.get_async() 
    ret = { 
     "rkey": resto.key.urlsafe(), 
     "rname": resto.resto_name 
    } 
    raise ndb.Return(ret) 

@ndb.tasklet 
def get_item(purch): 
    item = yield purch.p_item.get_async() 
    if (purch.p_item.kind() == "Product"): 
     ret = { 
      "ikey": item.key.urlsafe(), 
      "iname": item.name 
     } 
    else: 
     ret = { 
      "ikey": item.key.urlsafe(), 
      "iname": item.cust_name 
     } 
    raise ndb.Return(ret) 

@ndb.tasklet 
def load_history(purch): 
    at, item = yield get_establishment(purch), get_item(purch) 
    ret = { 
     "date": purch.p_datetime, 
     "at_key": at.rkey, 
     "at": at.rname, 
     "item_key": item.ikey, 
     "item": item.iname, 
     "qty": purch.p_quantity, 
     "type": purch.p_type 
    } 
    raise ndb.Return(ret) 

을, 나는 호출과 같이합니다

pq = UserPurchase.query().order(-UserPurchase.p_datetime) 
phistory = pq.map(load_history, limit = 20) 

표시를 위해 가장 최근에 20 번의 구매를 검색합니다. 그러나 내가 그것을 실행할 때, 나는 무엇을 만들지 확신 할 수없는 이상한 예외를 얻었고 ... 나는 태스크 릿에 익숙하지 않고 어떻게 진행되고 있는지 자신있게 말하고있다. 누군가가 나를 찾아 무엇에 대한 조언을 해줄 수 있다면 정말 고맙습니다 ...! 나는 위의 코드를 실행하면 여기에 내가 얻을 예외입니다 : 오류를 바탕으로

 ... 
     File "/Programming/VirtualCellar/server/virtsom.py", line 2348, in get 
     phistory = pq.map(load_history, limit = 20) 
     File "/Applications/GoogleAppEngineLauncher.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine/google/appengine/ext/ndb/utils.py", line 160, in positional_wrapper 
     return wrapped(*args, **kwds) 
     File "/Applications/GoogleAppEngineLauncher.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine/google/appengine/ext/ndb/query.py", line 1190, in map 
     **q_options).get_result() 
     File "/Applications/GoogleAppEngineLauncher.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine/google/appengine/ext/ndb/tasklets.py", line 383, in get_result 
     self.check_success() 
     File "/Applications/GoogleAppEngineLauncher.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine/google/appengine/ext/ndb/tasklets.py", line 624, in _finish 
     result = [r.get_result() for r in self._results] 
     File "/Applications/GoogleAppEngineLauncher.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine/google/appengine/ext/ndb/tasklets.py", line 383, in get_result 
     self.check_success() 
     File "/Applications/GoogleAppEngineLauncher.app/Contents/Resources/GoogleAppEngine-default.bundle/Contents/Resources/google_appengine/google/appengine/ext/ndb/tasklets.py", line 430, in _help_tasklet_along 
     value = gen.send(val) 
     File "/Programming/VirtualCellar/server/virtsom.py", line 2274, in load_history 
     "at_key": at.rkey, 
    AttributeError: 'dict' object has no attribute 'rkey' 

을 거의 ... 당신은 오류 메시지를 잘못 해석하고

답변

1

을 반환 사전을 좋아하지 않는 태스크 릿 것 같다 : dict이 리턴되지만 존재하지 않는 해당 dict의 속성에 액세스하려고합니다.

 "at_key": at.rkey, 

에 :

 "at_key": at.get('rkey'), 

그럼 내부 load_history 변경 한 경우, 에게 DICT 내부의 값이 아닌 DICT의 atribute에 액세스 할 나타납니다 또는 :

 "at_key": at['rkey'], 

atitem dicts의 다른 dict 값에 액세스 할 때 비슷한 시도를 다시하고 싶습니다.

+0

감사합니다. 댄! dict.property와 dict [ "property"]가 파이썬에서 동일하다고 항상 생각했기 때문에이 일은했지만 완전히 혼란 스러웠습니다. 그래서 나머지 파이썬 코드의 대부분이 왜 작동하는지 전혀 모르겠습니다. 조금도....! –

+0

요점은 'dict.property'는 dict 객체 (임의의 dict 객체)의 속성이지만'dict [ "key"]'는 dict 내부의''key': value' 쌍에 해당하는 값입니다 , 그것은 dict 객체의 속성이 아닙니다. –

+0

Hehe, 내가 일하고있는 프로젝트에서 자바 스크립트와 파이썬 코딩을 모두하고 있으며 두 언어의 구문을 혼동스럽게 생각합니다 .... sheesh! :) –