benjhar/JokeAPI-Python

get_joke() Example

Closed this issue · 2 comments

Hi. I was trying to use the Python wrapper of Joke API. I was going through the README.md when I came across this get_joke() example:

from jokeapi import Jokes # Import the Jokes class
import asyncio

async def print_joke():
    j = await Jokes()  # Initialise the class
    await j.get_joke()  # Retrieve a random joke
    if joke["type"] == "single": # Print the joke
        print(joke["joke"])
    else:
        print(joke["setup"])
        print(joke["delivery"])

asyncio.run(print_joke())

Bugs:

  • NameError: name 'joke' is not defined
  • Even if I correct the name error by adding joke = await j.get_joke() in the second line of the function it still gives me an error along with the joke: RuntimeError: Event loop is closed

To Reproduce

  1. Just copy the example under the README.md and run it.
  2. See error

Expected behavior

I expected the function to return a joke after each call.

It seems that this is a Windows only issue that I wasn't aware of as I don't have a Windows machine. Please try prepending the following snippet to the code.

asyncio.set_event_loop_policy(asyncio.WindowsSelectorEventLoopPolicy())

Will fix the documentation mistake when I get the chance.

Alright, thanks for the update.