[PYTHON] I tried to debug.

Hello! I think I'll practice debugging this time. Python Debugging Tips Python test code to try in the shortest time I'm just copying and trying, so please see here for details! !!

Debug with pdb

First debug with pdb. Below is the code used for debugging.


import pdb;

    
for ii in range(1, 21):
    if ii % 15 == 0:
        print('Takeuchi Tsuyoshi')
    elif ii % 3 == 0:
        print('Takeuchi')
    elif ii % 5 == 0:
        print('Tsuyoshi')
    else:
        print(ii)    

Start it first.


python -m pdb filename

If you type the command "n", it will be executed line by line.

(Pdb) n
> /home/takeuchi/test.py(4)<module>()
-> for ii in range(1, 21):
(Pdb) n
> /home/takeuchi/test.py(5)<module>()
-> if ii % 15 == 0:
(Pdb) n
> /home/takeuchi/test.py(7)<module>()
-
> elif ii % 3 == 0:
(Pdb) 

Then, when you type the command "c", the process proceeds at once instead of line by line.

> /home/takeuchi/test.py(4)<module>()
-> for ii in range(1, 21):
(Pdb) c
Takeuchi
4
Tsuyoshi
Takeuchi
7
8
Takeuchi
Tsuyoshi
11
Takeuchi
13
14
Takeuchi Tsuyoshi
16
17
Takeuchi
19
Tsuyoshi
The program finished and will be restarted
> /home/takeuchi/test.py(1)<module>()
-> import pdb;
(Pdb) 

You can specify a breakpoint with b and check the processing up to that point. As a trial, let's execute the processing up to the first branch of the if statement. It was output properly.

(Pdb) b 5
Breakpoint 1 at /home/takeuchi/test.py:5
(Pdb) c
> /home/takeuchi/test.py(5)<module>()
-> if ii % 15 == 0:
(Pdb) c
1
> /home/takeuchi/test.py(5)<module>()
-> if ii % 15 == 0:
(Pdb) c
2
> /home/takeuchi/test.py(5)<module>()
-> if ii % 15 == 0:
(Pdb) c
Takeuchi
> /home/takeuchi/test.py(5)<module>()
-> if ii % 15 == 0:
(Pdb) c
4
> /home/takeuchi/test.py(5)<module>()
-> if ii % 15 == 0:
(Pdb) c
Tsuyoshi
> /home/takeuchi/test.py(5)<module>()
-> if ii % 15 == 0:
(Pdb) 
Takeuchi
> /home/takeuchi/test.py(5)<module>()
-> if ii % 15 == 0:
c(Pdb) c
7
> /home/takeuchi/test.py(5)<module>()
-> if ii % 15 == 0:
(Pdb) c
8
> /home/takeuchi/test.py(5)<module>()
-> if ii % 15 == 0:
(Pdb) c
Takeuchi
> /home/takeuchi/test.py(5)<module>()
-> if ii % 15 == 0:
(Pdb) c
Tsuyoshi
> /home/takeuchi/test.py(5)<module>()
-> if ii % 15 == 0:
(Pdb) c
11
> /home/takeuchi/test.py(5)<module>()
-> if ii % 15 == 0:
(Pdb) c
Takeuchi
> /home/takeuchi/test.py(5)<module>()
-> if ii % 15 == 0:
(Pdb) c
13
> /home/takeuchi/test.py(5)<module>()
-> if ii % 15 == 0:
(Pdb) c
14
> /home/takeuchi/test.py(5)<module>()
-> if ii % 15 == 0:
(Pdb) c
Takeuchi Tsuyoshi
> /home/takeuchi/test.py(5)<module>()
-> if ii % 15 == 0:
(Pdb) 
(Pdb) b 5, ii == 15

By doing so, the processing will stop only when ii is 15.

Next, I would like to run the test in the same way. This is also a copy, so if you want to know more details, please see Reference.

test2.py


import unittest
import test as tt


class FizzBuzzTest(unittest.TestCase):
    def setUp(self):
        pass

    def tearDown(self):
        pass

    def test_normal(self):
        self.assertEqual(1, tt.fizzbuzz(1))

    def test_fizz(self):
        self.assertEqual("Tsuyoshi", tt.fizzbuzz(3))

    def test_buzz(self):
        self.assertEqual("Takeuchi", tt.fizzbuzz(5))

    def test_fizzbuzz(self):
        self.assertEqual("Takeuchi Tsuyoshi", tt.fizzbuzz(15))


if __name__ == "__main__":
    unittest.main()

def setUp (self) and def tearDown (self) seem to be functions that run at the timing of initialization / end. ʻAssertEqual ()` checks if the first and second arguments have the same value. And the following is the imported test.py.

test.py


def fizzbuzz(number):
    if number % 15 == 0:
        return "Takeuchi Tsuyoshi"
    if number % 5 == 0:
        return "Takeuchi"
    if number % 3 == 0:
        return "Tsuyoshi"

    return number


if __name__ == "__main__":
    for i in range(1, 101):
        print(fizzbuzz(i))

The execution result is as follows! !!

❯ python test2.py 
....
----------------------------------------------------------------------
Ran 4 tests in 0.001s

OK

References

Python Debugging Tips Python test code to try in the shortest time

Recommended Posts

I tried to debug.
I tried to paste
I tried to learn PredNet
I tried to organize SVM.
I tried to implement PCANet
I tried to reintroduce Linux
I tried to introduce Pylint
I tried to summarize SparseMatrix
I tried to touch jupyter
I tried to implement StarGAN (1)
I tried to implement Deep VQE
I tried to create Quip API
I tried to implement adversarial validation
I tried to explain Pytorch dataset
I tried Watson Speech to Text
I tried to touch Tesla's API
I tried to implement hierarchical clustering
I tried to organize about MCMC.
I tried to implement Realness GAN
I want to debug with Python
I tried to move the ball
I tried to estimate the interval.
I tried scraping
I tried PyQ
I tried AutoKeras
I tried papermill
I tried django-slack
I tried Django
I tried spleeter
I tried cgo
I tried to create a linebot (implementation)
I tried to summarize Python exception handling
I tried to implement PLSA in Python
I tried using Azure Speech to Text.
I tried to implement Autoencoder with TensorFlow
I tried to summarize the umask command
I tried to implement permutation in Python
I tried to create a linebot (preparation)
I tried to visualize AutoEncoder with TensorFlow
I tried to recognize the wake word
I tried to get started with Hy
I tried to implement PLSA in Python 2
Python3 standard input I tried to summarize
I tried to classify text using TensorFlow
I tried to summarize the graphical modeling.
I tried adding post-increment to CPython Implementation
I tried to implement ADALINE in Python
I tried to let optuna solve Sudoku
I tried to estimate the pi stochastically
I tried to touch the COTOHA API
I tried to implement PPO in Python
I tried to implement CVAE with PyTorch
I tried to make a Web API
I tried to solve TSP with QAOA
[Python] I tried to calculate TF-IDF steadily
I tried to touch Python (basic syntax)
I tried my best to return to Lasso
I tried to summarize Ansible modules-Linux edition
I tried to predict Covid-19 using Darts
I tried using parameterized
I tried using argparse