【发布时间】:2017-08-20 01:14:09
【问题描述】:
我知道伏都教魔法可能不是造成这种情况的原因 - 但看起来确实如此!
我有以下代码 sn-ps,使用 imgur API。 imgur 对象是 imgur API 使用的客户端,它包含一个属性 credits,该属性显示用户在网站上拥有的访问信用数。
imgur = imgurpython.ImgurClient(client_id, client_secret)
调用:
imgur.credits
正常返还学分,即:
{'ClientLimit': 12500, 'UserReset': 1503185179, 'UserLimit': 500, 'UserRemaining': 0, 'ClientRemaining': 12000}
但是,当我尝试在以后的函数中调用字典时:
def check_credits(imgur):
'''
Receives a client - and if there is not much credits left,
wait until the credit refills - i.e. pause the program
'''
credits = imgur.credits
credits_remaining = credits['UserRemaining']
reset_time = credits['UserReset']
if credits_remaining < 10:
print('not enough credits, remaining: %i' % credits_remaining)
now = int(dt.utcnow().timestamp())
wait_time = reset_time - now
print('waiting for: %i' % wait_time)
time.sleep(wait_time)
有时字典中的值似乎变成了None,而不是它们应该是的整数。在这种情况下,reset_time 和 credits_remaining 有时会变成 None。为了让我的代码运行,我不得不在代码中添加try-catch,这变得非常令人沮丧。顺便说一句,每当出现错误ImgurClientRateLimitError,即imgur.credits['UserRemaining'] == 0 时,都会调用此函数。我想知道是否有人知道为什么会这样。
【问题讨论】:
-
无法帮助您了解他们为什么返回
None,但为什么要到处添加try/except?只需在首次分配这些变量时检查None,并在那时替换一个适当的虚拟值。
标签: python pass-by-reference imgur