vendredi 31 août 2018

Python - If json.object is empty, repeat the function until new value?

So I have been trying to find out a more beautiful way to actually do some fault and errors in a script I am working on.

Basically I have a json_resp = resp.json() who either gives me values or [] meaning either there is something or not.

Now the question I am having trouble with is that I don't know which way is the best to repeat a function if it is empty, shall I repeat the whole function or what else would be most "best reason" to solve it in a good way?

What I have done is that I changed the objects from the json resp to a len. If its 0 then repeat else do other stuff:

#json_resp['objects'] either has empty [] or not always.

json_resp = resp.json()

        if len(json_resp['objects']) == 0:
            print('Sleeping in 2 sec')
            time.sleep(2)
            run_method() #Shall I call the function to start over?

        else:
            print(len(json_resp['objects']))
            continue do rest of the code

As you can see right now I am compare with len of the json_resp but what makes me unsure is that if it is a good way to actually call the function again? Wouldn't it have a limit or maybe delay the whole process... Im not sure but what is your thoughts of making this function "better, smarter, faster"?

My thought was maybe to either put a try except or while loop that? Let me know what you guys think

Aucun commentaire:

Enregistrer un commentaire