Imgur API:字典值神奇地变成了无?

时间:2017-08-20 01:14:09

标签: python pass-by-reference imgur

我知道伏都教魔法可能不是原因 - 但看起来确实如此!

我有以下代码片段,使用了imgur API。 imgur = imgurpython.ImgurClient(client_id, client_secret) 对象是imgur API使用的客户端,其中包含一个属性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而不是它们应该是的整数。在这种情况下,NoneImgurClientRateLimitError有时会变成imgur.credits['UserRemaining'] == 0。为了让我的代码能够运行,我不得不在整个代码中添加try-catches而且它变得非常令人沮丧。顺便说一句,每当错误<?xml version="1.0" encoding="utf-8"?> <android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context="com.example.navjeevenmann.mytycoon.MainActivity"> <android.support.v7.widget.Toolbar android:id="@+id/toolbar2" android:layout_width="0dp" android:layout_height="59dp" android:background="#2C3AAD" android:minHeight="?attr/actionBarSize" android:theme="?attr/actionBarTheme" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintHorizontal_bias="1.0" app:layout_constraintLeft_toLeftOf="parent" app:layout_constraintRight_toRightOf="parent" app:layout_constraintTop_toTopOf="parent" app:layout_constraintVertical_bias="1.0" /> <TextView android:id="@+id/textView" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginBottom="8dp" android:text="" android:textColor="#000" android:textSize="25sp" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintHorizontal_bias="0.21" app:layout_constraintLeft_toLeftOf="parent" app:layout_constraintRight_toRightOf="parent" app:layout_constraintTop_toTopOf="parent" app:layout_constraintVertical_bias="0.01999998" /> <TextView android:id="@+id/textView2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="" android:textColor="#000" android:textSize="25sp" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintHorizontal_bias="0.8" app:layout_constraintLeft_toLeftOf="parent" app:layout_constraintRight_toRightOf="parent" app:layout_constraintTop_toTopOf="parent" app:layout_constraintVertical_bias="0.02" /> <ImageButton android:id="@+id/button" android:layout_width="247dp" android:layout_height="243dp" android:background="#0000" android:src="@drawable/opengraph" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintHorizontal_bias="0.503" app:layout_constraintLeft_toLeftOf="parent" app:layout_constraintRight_toRightOf="parent" app:layout_constraintTop_toTopOf="parent" /> <ImageButton android:id="@+id/autoclick" android:layout_width="49dp" android:layout_height="47dp" android:src="@drawable/cursor" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintHorizontal_bias="0.9" app:layout_constraintLeft_toLeftOf="parent" app:layout_constraintRight_toRightOf="parent" app:layout_constraintTop_toTopOf="parent" app:layout_constraintVertical_bias="0.991" /> <ImageButton android:id="@+id/imageview" android:layout_width="62dp" android:layout_height="58dp" android:adjustViewBounds="true" android:background="#2c3aad" android:maxHeight="62dp" android:maxWidth="5dp" android:scaleType="fitXY" android:src="@drawable/home" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintLeft_toLeftOf="parent" app:layout_constraintRight_toRightOf="parent" app:layout_constraintTop_toTopOf="parent" app:layout_constraintVertical_bias="1.0" /> <ImageButton android:id="@+id/singleclick" android:layout_width="47dp" android:layout_height="47dp" android:background="#2C3AAD" android:src="@drawable/singlecursor" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintHorizontal_bias="0.1" app:layout_constraintLeft_toLeftOf="parent" app:layout_constraintRight_toRightOf="parent" app:layout_constraintTop_toTopOf="parent" app:layout_constraintVertical_bias="0.99" /> <ImageButton android:id="@+id/imageButton2" android:src="@drawable/notes" android:layout_width="47dp" android:layout_height="49dp" android:adjustViewBounds="true" android:maxHeight="62dp" android:maxWidth="5dp" android:scaleType="fitXY" android:background="@null" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintLeft_toLeftOf="parent" app:layout_constraintRight_toRightOf="parent" app:layout_constraintTop_toTopOf="parent" app:layout_constraintVertical_bias="0.0" app:layout_constraintHorizontal_bias="0.047" /> <TextView android:id="@+id/textView5" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="" app:layout_constraintRight_toRightOf="parent" app:layout_constraintLeft_toLeftOf="parent" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintTop_toTopOf="parent" app:layout_constraintVertical_bias="0.100000024" /> 时都会调用此函数,即{{1}}时。我想知道是否有人知道为什么会这样。

1 个答案:

答案 0 :(得分:2)

在查看客户端的源代码后,似乎每次请求时都会自动更新。调用ImgurClient.make_request后,将从响应标头中获取更新的值。标头值从dict.get获得,如果密钥字典中不存在密钥,则可以返回None。参考代码在此处:https://github.com/Imgur/imgurpython/blob/master/imgurpython/client.py#L143

我不确定这些标题是否仍然用于404或403等错误,但我会从那里进一步调查。看起来,由于这种行为,您需要缓存以前的值或在这些情况下手动调用ImgurClient.get_credits方法以获取实际值。无论你采取哪种解决方案取决于你。