为了检查TodoistAPI
类中是否存在键,我使用“in”关键字。但是,它会导致KeyError
.
我隔离了代码片段,以检查究竟是什么原因导致的KeyError
以及提供了哪些输入。
api = TodoistAPI(token)
api.sync()
print("id" in api.state["items"][0])
api.state["items"][0]
包含:
Item({'assigned_by_uid': None,
'checked': 0,
'child_order': 0,
'collapsed': 0,
'content': 'example task',
'date_added': '0',
'date_completed': None,
'day_order': 0,
'due': {'date': '0',
'is_recurring': True,
'lang': 'de',
'string': 'every day',
'timezone': None},
'has_more_notes': False,
'id': 0,
'in_history': 0,
'is_deleted': 0,
'labels': [0, 0],
'parent_id': None,
'priority': 1,
'project_id': 0,
'responsible_uid': None,
'section_id': None,
'sync_id': None,
'user_id': 0})
我希望的输出print(...)
是True
or False
,但实际输出如下
Traceback (most recent call last):
File "/Users/path/to/file.py", line 11, in
print("id" in applicationInterface.state["items"][0])
File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/todoist/models.py", line 16, in __getitem__
return self.data[key]
KeyError: 0
Process finished with exit code 1
我还在 Todoist Github-Repository 中创建了一个问题:https ://github.com/Doist/todoist-python/issues/64