My small program [programming interview questions database]

directory

  • Where that push past
  • Used to introduce
  • Take a simple chestnut

Where that push past

First, I’ll take a look at PDB debugging, which is a built-in python module for debugging Python code from the command line. Why use the command line, you might say, when it’s easy to debug code using an editor like Pycharm? I used to think the same thing until the code had to run on Linux (Pycharm now works on the code remotely, but we’ll leave that aside).

Used to introduce

How do I add breakpoints?

When it comes to debug, you must add breakpoints. There are two ways to add breakpoints:

  • Add a line after you want the breakpoint code
pdb.set_trace()
Copy the code

In this way, breakpoint debugging can be done by simply running the Python file.

  • Use the command line to add breakpoints
B line_number (line of code)Copy the code

To use this approach, python -m PDB xxx.py is required to initiate breakpoint debugging.

Common commands

First a brief introduction to the use of commands, here do not remember, and so on when used to check back on the line.

  • 1 Run the python -m PDB xxx.py command to switch to the Debug mode

  • 2 h :(help

  • 3 w :(where) prints the current execution stack

  • 4 d :(down) perform the jump to a further layer in the current stack.

  • 5 u :(up) performs a jump to the upper layer of the current stack

  • 6 b :(break) adds a breakpoint

    B line_no: add a breakpoint on line_no line of the current script b filename:line_no: add a breakpoint on line_no line of the filename b function: Add a breakpoint at the first executable statement of functionCopy the code
  • 7 tbreak :(temporary break) indicates a temporary breakpoint

    The breakpoint is automatically deleted after the first execution of the breakpointCopy the code
  • 8 cl :(clear) clear the breakpoint

    Cl clear all breakpoints CL bpNumber1 BPNumber2... Clear breakpoint number bpNumber1, BPNumber2... Cl lineno :line_no Clears the breakpoint on lineno line of the current script cl filename:line_no Clears the breakpoint on line_no line of filenameCopy the code
  • 9 Disable: disables breakpoints with the bpnumber parameter. Breakpoints are still available but not enabled

  • 10 Enable: activates the breakpoint. The parameter is bpnumber

  • 11 s :(step) run the next command

    If this sentence is a function call, s executes to the first line of the functionCopy the code
  • 12 n :(next) executes the next statement

    If this is a function call, the function is executed, followed by the next line of the currently executed statement.Copy the code
  • 13 r :(return) executes the currently running function until the end

  • 14 c :(continue) execution continues until the next breakpoint is reached

  • 15 l :(list) lists the source code

    L first Second Lists the code in the first--second range. If second<first, second is resolved to the number of linesCopy the code
  • 16a :(args) lists functions that currently execute functions

  • 17 p expression :(print) displays the expression value

  • 18 PP expression: better p expression

  • 19 Run: restarts the debug function

  • 20 q :(quit) exits the debugging

  • 21 j lineno :(jump) sets the next statement function to execute

    You can jump only at the bottom of the stack and re-execute backwards, directly to the line numberCopy the code
  • 22) unt :(until) executes to the next line (breaks out of the loop), or the current stack ends

  • 23) condition bpnumber conditon, set the condition for the breakpoint. The bpnumber breakpoint is valid if condition returns True, otherwise bpnumber breakpoint is invalid

Take a simple chestnut

To verify the use of PDB, I wrote a simple Python code that looks like this:

__author__ = 'zone'
__gzh__ = 'Company Number: Zone7'
import pdb

class MyScrapy:
    urls = []

    def start_url(self, urls):
        pdb.set_trace()
        for url in urls:
            print(url)
            self.urls.append(url)

    def parse(self):
        pdb.set_trace()
        for url in self.urls:
            result = self.request_something(url)

    def request_something(self, url):
        print('requesting... ')
        data = ' ''
         
       Title    '' '
        return data


scrapy= MyScrapy()
scrapy.start_url(["http://www.zone7.cn"."http://www.zone7.cn"."http://www.zone7.cn"."http://www.zone7.cn", ])
scrapy.parse()
Copy the code

Run example :(here for the convenience of everyone to read, I added Chinese comments, the actual runtime will not have comments)

D:\work\venv\Scripts\python.exe D:/work_test/test/pdb_test/pdb_test.py
> d:\work_test\test\pdb_test\pdb_test.py(11)start_url()
-> for url inUrl: (Pdb) n note: n (next) perform the next step > d: work_test\test\pdb_test\pdb_test.py(12)start_url()
-> print7 def start_url(self, urls): 10 pdb.set_trace() 11for url in urls:
 12  ->	            print(url)
 13  	            self.urls.append(url)
 14  	
 15  	    def parse(self):
 16  	        pdb.set_trace()
 17  	        for url inSelf. Urls: (Pdb)continue), continue until the next breakpoint is hit http://www.zone7.cn http://www.zone7.cn http://www.zone7.cn http://www.zone7.cn > d: work_test\test\pdb_test\pdb_test.py(17)parse()
-> for url inSelf.urls: (Pdb) n Note: n (next) Perform the next step > d: work_test\test\pdb_test\pdb_test.py(18)parse() -> result = self.request_something(url) (Pdb) l 13 def parse(self): 16 pdb.set_trace() 17 def parse(self): 16 pdb.set_trace() 17for url in self.urls:
 18  ->	            result = self.request_something(url)
 19  	
 20  	    def request_something(self, url):
 21  	        print('requesting... ')
 22  	        data = ' ''
       23 < HTML lang="en"> (Pdb) s --Call-- > d:\work_test\test\pdb_test\pdb_test.py(20)request_something() -> def request_something(self, url): D :\work_test\test\pdb_test\pdb_test.py(21)request_something() -> print('requesting...Pdb.set_trace () 17 for url in self.urls: 18 result = self.request_something(url) 19 20 def request_something(self, url): 21 -> print('requesting...') 22 data = '' '<! DOCTYPE html> 23 <html lang="en">
 24  	<head>
 25  	    <meta charset="UTF-8"> 26 <title> title </title> (Pdb) pprint) prints out the data for the URL variable'http://www.zone7.cn'(Pdb) n Note: n (next) Perform the next requesting... > d:\work_test\test\pdb_test\pdb_test.py(31)request_something()
-> </html>' '*** NameError: name *** NameError: name *** NameError: name 'data'is not defined (Pdb) n N (next) execute next > d: work_test\test\pdb_test\pdb_test.py(32)request_something() -> return data (Pdb) p data P (print) print data for specified variable '<! DOCTYPE html>\n<html lang="en">\n<head>\n    <meta charset="UTF-8">\n    <title>Title</title>\n</head>\n<body>\n\n</body>\n</html>'(Pdb) q Comment: q (quit) ExitsCopy the code

conclusion

According to the above example of a set of down, the basic usage can learn, the key or have their own practice, today I write this, but also want to write an article about performance debugging, I do not know whether there is time for these two days.