-1

I have been trying to optimize requests but i can't figure it out

def chuck_norris_jokes():
    import requests
    url = "https://api.chucknorris.io/jokes/random"
    response = requests.request("GET", url)
    dict = response.text
    import json
    test_string = dict 
    dict = json.loads(test_string)
    print(dict.get('value'))
Karl Knechtel
  • 62,466
  • 11
  • 102
  • 153
Apio
  • 1
  • 3
    what are you trying to optimize? – Tom McLean Sep 01 '22 at 21:32
  • Welcome to Stack Overflow. What do you mean by "optimize"? If I say that the code is already optimized, why am I wrong? What should happen when the code runs, and **how is that different** from what already happens? Please read [ask] (and also the [formatting help](https://stackoverflow.com/help/formatting)). – Karl Knechtel Sep 01 '22 at 21:42

3 Answers3

0

It's not specified what you are trying to optimize.

In case you are looking to optimize the code, you can drop the json import and use the .json method of requests response object:

import requests

url = "https://api.chucknorris.io/jokes/random"

def chuck_norris_jokes():
    response = requests.get(url)
    print(response.json().get('value'))

chuck_norris_jokes()

If the problem is that you want to run this function hundreds of times and it is taking too long than the problem is not with the code but with the duration of the requests. If this is the case I would look into the Python threading or asyncio modules.

Jurgen R
  • 81
  • 1
  • 3
-1

just move the imports outside of the function. Remove test_string = dict.

This works:

import requests
import json

def chuck_norris_jokes():
    url = "https://api.chucknorris.io/jokes/random"
    response = requests.request("GET", url)
    x = response.text
    d = json.loads(x)
    print(d.get('value'))

chuck_norris_jokes()

this returns one of the jokes.

D.L
  • 4,339
  • 5
  • 22
  • 45
  • 1
    Imports are cached, and a simple assignment takes negligible time, so this won't make a meaningful difference overall. It's also not even remotely clear what OP wants to "optimize", what supposedly isn't "optimized" about it already, or whether the problem is solvable at all (perhaps the underlying question is "why does it take so long to get a result from the web site?" and the answer is "because your internet connection is bad, or because the web site is overloaded".) Please don't try to answer questions like this. – Karl Knechtel Sep 01 '22 at 21:44
  • @KarlKnechtel, the internet connection is the only part of the comment that is correct. Outside of this, the code was inefficient: https://stackoverflow.com/questions/128478/should-import-statements-always-be-at-the-top-of-a-module This is the part that myself (and others) have answered. – D.L Sep 02 '22 at 09:27
-1

I guess you are trying to optimize the code, maybe you can do something like that

def chuck_norris_jokes():
    import requests, json
    url = "https://api.chucknorris.io/jokes/random"
    response = requests.request("GET", url)
    print(json.loads(response.text).get('value'))
Skrullwar
  • 29
  • 3