/ tuesdaytooling

Tuesday Tooling: Debugging Python With Icecream

No seriously, we're using ice cream.

Its been a while. How have you all been? In my world the last year has been "interesting" and not in a good way. I'm happy to say that all is getting better, and don't worry it is not health / family or work related.

So What Are We Talking About Today?

Debugging Python is all about print statements right?

print("This is the start")
for i in range(10):
    print(i)
    print("This should be the value of i")

Not really, but if it helps you solve a problem, then fair play. The right tool for the job is what you are comfortable with.
Earlier today I learnt about Icecream. Not the sweet and delicious treat, but a tool to debug Python code.

Icecream Info

logo
Icecream is the work of Ansgar Grunseid and it has the following features.

  • It prints both expressions/variable names and their values.
  • It's 60% faster to type.
  • Data structures are pretty printed.
  • Output is syntax highlighted.
  • It optionally includes program context: filename, line number, and parent function.

I already love ice cream, but it seems I now also love icecream too!

How Do I Install Icecream?

Via pip!

pip3 install icecream

How Do I Use Icecream?

Open your favourite Pytohn editor and import the icecream module, specifically the ic function.

from icecream import ic

We'll write a simple function that will take two arguments and add them together. Note that we use ic to write the output to the Python shell.

def function(a,b):
    ic(a + b)

Lastly we call the function and pass two values (a + b).

function(10,20)

All of the code looks like this

from icecream import ic
def function(a,b):
    ic(a + b)

function(10,20)

When we run the code we get this output

ic| a + b: 30

How Can I Do Something Cool With It?

Here is our scenario. We have a NASA JSON API that shows us who is onboard the ISS and a connected spacecraft. The problem is that when we print this out we get this.

{'people': [{'craft': 'Tiangong', 'name': 'Jing Haiping'}, {'craft': 'Tiangong', 'name': 'Gui Haichow'}, {'craft': 'Tiangong', 'name': 'Zhu Yangzhu'}, {'craft': 'ISS', 'name': 'Jasmin Moghbeli'}, {'craft': 'ISS', 'name': 'Andreas Mogensen'}, {'craft': 'ISS', 'name': 'Satoshi Furukawa'}, {'craft': 'ISS', 'name': 'Konstantin Borisov'}, {'craft': 'ISS', 'name': 'Oleg Kononenko'}, {'craft': 'ISS', 'name': 'Nikolai Chub'}, {'craft': 'ISS', 'name': "Loral O'Hara"}], 'number': 10, 'message': 'success'}

Sure we can read it, but for large datasets it can take a bit of work, so lets get some icecream and use it to make this much easier to read.

We start by importing icecream and requests. Requests is used to grab the data from NASA's API.

from icecream import ic
import requests

Next up we create a variable, url, to store the URL for NASA's API. We don't really have to do this but it makes it very easy to read.

url = "http://api.open-notify.org/astros.json"

We use the url with requests to download the dataset, storing it in an object called "r".

r = requests.get(url)

Lastly I use ic to return the contents of the "r" object and I specify that the data is in JSON format.

ic(r.json())

I save and run the code and I am presented with this wonderful block of data.
icecream

Complete Code Listing

from icecream import ic
import requests
url = "http://api.open-notify.org/astros.json"
r = requests.get(url)
ic(r.json())

Happy Hacking!