How to use list_pop?

偶尔善良 提交于 2019-12-11 15:13:18

问题


This is related to the Zapier Python code. Has anyone been successful using the pop_list() function as part of the StoreClient library? I'm stuck on this simple program. Basically i'm trying to remove the first item in my list that is stored in MyList.get('MyIDList')

Any help is greatly appreciated.

Below is my code. Note if I remove the 2nd line, it'll work fine. My list is certainly not empty.

MyList= StoreClient('STSecretKey')

MyList.list_pop('MyIDList', location='head')

return {'FirstItem':MyList.get('MyIDList')[0]}

Error Message:

Traceback (most recent call last):

  File "/tmp/tmpPToKk0/usercode.py", line 9, in the_function

    MyList.list_pop('MyIDList', location='head')

  File "/var/task/store_api.py", line 226, in list_pop

    return self.call('patch', BASE_URL, json=data)

  File "/var/task/store_api.py", line 61, in call

    raise StoreCallException(json['error'])

StoreCallException: list is empty

回答1:


David here, from the Zapier Platform team. It's hard to tell exactly what's wrong, but my best guess is that your list isn't actually a list. You can quickly verify this by going to https://store.zapier.com/api/records?secret=STSecretKey, which shows the JSON that's stored in the database. If it's correct, it'll look like this:

{
  "MyIDList": {
    "list": [
      1,
      2,
      3
    ]
  }
}

To generate the above, I ran the following code:

store = StoreClient('davidkey')

store.list_push('MyIDList', 1)
store.list_push('MyIDList', 2)
store.list_push('MyIDList', 3)

popped = store.list_pop('j', location='head')  # got 1
accessed = store.get('j')[0]  # got error

You'll notice that the last line generated an error for me. Given that it works for you (but popping off the list doesn't), I'm guessing that you instead have a key of 0 that has data in it. If you make sure to use list_push to populate your storage, it should work as expected.



来源:https://stackoverflow.com/questions/49420573/how-to-use-list-pop

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!