[Ipdb] Web development beginners tried to summarize debugging with Python

Introduction

I will summarize debugging using ipdb, including organizing what the web development beginners have learned.

What is ipdb?

It provides an extension of pdb, which is a ** debugger ** that comes standard with Python.

How to use

Enter the following code at the position where you want to start the debugger and execute the program.

import ipdb; ipdb.set_trace()

When executed, it waits for ** standard input **, so you can operate the debugger by entering a command. The following is a summary of frequently used (likely) commands.

command motion
n Run to next line
s Execute until the next function
p variable name Show variable value
a Show the arguments of the function being executed
h help
q End

Try using

Let's actually use ipdb using the program of Aggressive Cows of POJ No.3468.

Aggressive.py


import ipdb
n = 5
m = 3
x = [1, 2, 8, 4, 9]

def C(d):
    last = 0
    for _ in range(1, m):
        crt = last + 1
        while crt<n and x[crt]-x[last]<d:
            crt += 1
        if crt == n:
            return False
        last = crt
    return True

x.sort()
lb, ub = 0, max(x)

while ub-lb > 1:
    ipdb.set_trace()
    mid = int( (lb+ub)/2 )
    if C(mid):
        lb = mid
    else:
        ub = mid

print(lb)

Execute.

$ python Aggressive.py
>c:/users/~/aggressive.py(21)<module>()
     20     import ipdb; ipdb.set_trace()
---> 21     mid = int( (lb+ub)/2 )
     22     if C(mid):

The set breakpoint is output as an arrow.

ipdb> h

Documented commands (type help <topic>):
========================================
EOF    cl         disable  interact  next    psource  rv         unt
a      clear      display  j         p       q        s          until
alias  commands   down     jump      pdef    quit     source     up
args   condition  enable   l         pdoc    r        step       w
b      cont       exit     list      pfile   restart  tbreak     whatis
break  continue   h        ll        pinfo   return   u          where
bt     d          help     longlist  pinfo2  retval   unalias
c      debug      ignore   n         pp      run      undisplay

Miscellaneous help topics:
==========================
exec  pdb

A list of usable commands is output.

ipdb> p mid
*** NameError: name 'mid' is not defined

The variable mid is not defined because line 21 has not been executed yet.

ipdb> n
> c:/users/~/aggressive.py(22)<module>()
     21     mid = int( (lb+ub)/2 )
---> 22     if C(mid):
     23         lb = mid

The arrow indicating the breakpoint shifts down by one.

ipdb> p mid
4

Since the 21st line is executed, the value of the variable mid is displayed.

ipdb> s
--Call--
> c:/users/~/aggressive.py(6)C()
      5 
----> 6 def C(d):
      7     last = 0

It seems that the call to the next function was executed.

ipdb> a
d = 4

Since the call to the next function was just below, the value of the variable mid confirmed earlier is displayed as an argument.

ipdb> q
Exiting Debugger.

Exit the debugger.

Summary

I used all the commands that are often used in ipdb. It was my first time to use the debugger, but I ran the program ** halfway ** and checked the contents of the variables each time, or ran it to the desired position without rewriting the program. I found it very convenient to go. It's hard to write a program because it's debugging, so I want to make good use of the debugger to write an efficient and error-free program.

Recommended Posts

[Ipdb] Web development beginners tried to summarize debugging with Python
[Django-Extensions] Web development beginners tried to summarize Django-Extensions
python beginners tried to find out
I tried to solve the ant book beginner's edition with python
[Pandas] I tried to analyze sales data with Python [For beginners]
I tried to summarize everyone's remarks on slack with wordcloud (Python)
I tried to summarize Python exception handling
[Episode 2] Beginners tried Numeron AI with python
[Episode 3] Beginners tried Numeron AI with python
Python3 standard input I tried to summarize
[Episode 0] Beginners tried Numeron AI with python
[Episode 1] Beginners tried Numeron AI with python
[For beginners] Try web scraping with Python
I tried to refer to the fun rock-paper-scissors poi for beginners with Python
I tried to build a Mac Python development environment with pythonz + direnv
Python beginners tried to code some energy drinks
Introduction to Tornado (1): Python web framework started with Tornado
[Final story] Beginners tried Numeron AI with python
[Web development with Python] query is also redirect
I tried to get CloudWatch data with Python
I tried to output LLVM IR with Python
I tried to automate sushi making with python
Explosive speed with Python (Bottle)! Web API development
[Web development with Python] Precautions when saving cookies
[ES Lab] I tried to develop a WEB application with Python and Flask ②
Machine learning beginners tried to make a horse racing prediction model with python
Easy debugging with ipdb
python beginners tried to predict the number of criminals
I tried to summarize how to use matplotlib of python
I tried to implement Minesweeper on terminal with python
I tried to get started with blender python script_Part 01
I tried to touch the CSV file with Python
I tried to draw a route map with Python
I tried to solve the soma cube with python
NW engineer tried to aggregate addresses with python netaddr
I tried to implement an artificial perceptron with python
Python beginners get stuck with their first web scraping
I tried to automatically generate a password with Python3
I tried to summarize how to use pandas in python
I tried to solve the problem with Python Vol.1
I tried to analyze J League data with Python
I tried to summarize the string operations of Python
I tried to solve AOJ's number theory with Python
[For beginners in competition professionals] I tried to solve 40 AOJ "ITP I" questions with python
[For beginners] Web scraping with Python "Access the URL in the page to get the contents"
[AWS] Development environment version that tried to build a Python environment with eb [Elastic Beanstalk]
I tried fp-growth with python
I tried scraping with Python
Web scraping with python + JupyterLab
I tried to make various "dummy data" with Python faker
I tried various methods to send Japanese mail with Python
Try to display various information useful for debugging with python
Debugging with pdb in Python
Connect to Wikipedia with Python
Post to slack with Python 3
Web application development with Flask
I tried web application development and thought about how to prevent beginners from getting sick.
[Part.2] Crawling with Python! Click the web page to move!
3 Reasons Beginners to Start Python
Web API with Python + Falcon
[Python] I tried to visualize tweets about Corona with WordCloud