Have you ever seen an error output like the following:
2 divided by 1 is equal to 2.0.
Traceback (most recent call last):
File "loguru_example.py", line 17, in <module>
divide_numbers(num_list)
File "loguru_example.py", line 11, in divide_numbers
res = division(num1, num2)
File "loguru_example.py", line 5, in division
return num1/num2
ZeroDivisionError: division by zero
and want to make the output a bit easier to understand as shown here?
Image by author
You may also want to visualize which lines of code are being executed and how many times they are executed in real time:
GIF by author
If so, this article will give you the tools to do exactly the above. Those 3 tools are:
- Loguru — print better exceptions
- snoop — prints the lines of code that are executed in a function
- heartrate — visualizes the execution of a Python program in real time
And all it takes to use these tools is one line of code!
loguru is a library that aims to make python login enjoyable. Loguru provides a lot of cool functionality, but one functionality that I found most useful is the ability to detect unexpected errors Y show what value of a variable causes your code to fail.
To install Loguru, type
To understand how Loguru can be useful, imagine you have 2 functions division
Y divide_numbers
and the function divide_numbers
is executed.
Note that combinations([2,1,0], 2)
returns [(2, 1), (2, 0), (1, 0)]
. After running the above code, we get this error:
2 divided by 1 is equal to 2.0.
Traceback (most recent call last):
File "loguru_example.py", line 17, in <module>
divide_numbers(num_list)
File "loguru_example.py", line 11, in divide_numbers
res = division(num1, num2)
File "loguru_example.py", line 5, in division
return num1/num2
ZeroDivisionError: division by zero
From the output, we know that the line return num1/num2
is where the error occurs, but we don’t know what values of num1
Y num2
cause the error. Fortunately, this can be easily tracked by adding Loguru’s logger.catch
decorator:
Production:
Image by author
Adding logger.catch
, exceptions are much easier to understand! It turns out that the error occurs when dividing 2
for 0
.
What if there is no bug in the code, but we want to find out what is going on in the code? That’s when snoop comes in handy.
snoop is a Python package that prints the lines of code that are executed along with the values of each variable by adding just a decorator.
To install snoop, type:
Let’s imagine we have a function calledfactorial
that find the factorial of an integer.
Production:
The factorial of 5 is 120
To understand why the output of factorial(5)
is 20
we can add snoop
decorator to function factorial
.
Production:
Image by author
In the output above, we can see the values of the variables and which lines of code are executed. Now we can understand how recursion works much better!
If you want to visualize which lines are executed and how many times they are executed, try heartrate.
heart rate It is also created by the creator of snoop. To install heartrate, type:
Now let’s add heartrate.trace(browser=True)
to our previous code. This will open a browser window showing the display of the file where trace()
was called
A new browser should appear when you run the above code. If not, go to http://localhost:9999. You should see the output like below:
Image by author
Fresh! The bars show the lines that have been touched. Longer bars mean more hits, lighter colors mean more recent.
From the above output, we can see that the program executes:
if x==1
5 timesreturn 1
oncereturn (x * factorial(x-1))
4 times
The output makes sense since the initial value of x
is 5 and the function is called repeatedly until x
equal to 1
.
Now let’s see what it’s like to visualize a Python program running in real time using heart rate. let’s add sleep(0.5)
to make the program run a bit slower and increase num
a 20
.
GIF by author
Awesome! We can see which lines of code are being executed and how many times each of them has been executed in real time.
Congratulations! You’ve just learned 3 tools to track and visualize the execution of your Python code. I hope debugging will be less painful for you when you use these 3 tools. Since these tools only require one line of code, why not give them a try to see how useful they are?
Feel free to play around and fork the source code of this article here.
Star this repository.
Recommend Tran is a prolific data science writer and has written an impressive collection of useful data science topics along with code and articles. Khuyne is currently seeking a Machine Learning Engineer position, Data Scientist position, or Developer Advocate position in the Bay Area after May 2022, so please reach out to someone with his skills.
Original. Reposted with permission.