Skip to content

using the pdb debugger

Abdullah Bakhach edited this page Feb 19, 2017 · 9 revisions

Intro

We need a way to debug our python development. Currently our python runs from the alfred command line, so we should be able to fire up a debugger that puts break points on that process.

pdb

we are using pdb (see this PR). see reference for pdb. This is kind of the poor man's CLI debugger, we cannot use the remote debugging of pycharm since it's only available in the enterprise edition.

how to use pdb

  • instead of putting a breakpoint using GUI, the following text functions as a "break point":
 rpdb.set_trace()

so for example:

def get_projects_and_hours():
    service, spreadsheetId = credentials.get_service_and_spreadsheetId()
    sheetTitle, colNum = get_sheet_title_and_column(service, spreadsheetId)
    projectCells = 'B16:H19'
    initialProjectCellIndex = 16
    rangeName = "%s!%s" % (sheetTitle, projectCells)
    result = service.spreadsheets().values().get(
        spreadsheetId=spreadsheetId, range=rangeName, majorDimension='COLUMNS').execute()

    rpdb.set_trace()

    return colNum, sheetTitle, result.get('values',[])[0], result.get('values',[])[colNum+1]
  • run logger by hitting the hot key (currently L)
  • you'll see the alfred window showing up
screen shot 2017-02-18 at 6 40 19 am - open a terminal window and run the command `nc 127.0.0.1 4444` (❗ **note: as soon as click any part of the screen, the alfred window will disappear.. which is a problem with our current implementation, we should fix it**) - as soon as you run the command u'll get somethign like this
> /Users/abbood/Dropbox/Alfred/Alfred.alfredpreferences/workflows/user.workflow.B0A73475-E69F-45F9-AC1D-36769C0627DF/log_hours.py(45)get_projects_and_hours()
-> return colNum, sheetTitle, result.get('values',[])[0], result.get('values',[])[colNum+1]
(Pdb)
  • this is the pdb prompt.. take a look at this cheat sheet to see what commands you can use
  • in the above example suppose i wanted to know the value of the variable colNum, i simply type the following on the pdb CLI:
(Pdb) pp colNum
5
  • from the CLI you can see a list of commands by typing help:

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

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

Undocumented commands:
======================
retval  rv
  • to go to next line, hit c
  • this will return the regular command prompt, b/c u didn't have a breakpoint after that.

that's it!

note: you gotta quit debugger then run it again to resume execution

this happens to me when i hit the c command (to continue) and i got multiple break points.. the debugger simply hangs on the terminal, so i quit the debugger, then run it again to continue debugging



q
d
^C
abbood@Abdullahs-MacBook-Pro-2:~/dev/productivity_scripts/logtivly$ nc 127.0.0.1 4444
> /Users/abbood/Dropbox/Alfred/Alfred.alfredpreferences/workflows/user.workflow.B0A73475-E69F-45F9-AC1D-36769C0627DF/log_hours.py(30)get_sheet_title_and_column()
-> rpdb.set_trace()
(Pdb) l
 25  	        # TODO: replace current year with actual cell year, see http://stackoverflow.com/q/42216491/766570
 26  	        for row in values:
 27  	            for column in row:
 28  	                dateStr = "%s %s" % (column, datetime.date.today().year)
 29  	                cellDate = datetime.datetime.strptime(dateStr, '%b %d %Y')
 30  ->	                rpdb.set_trace()
 31  	                if cellDate.date() == datetime.date.today():
 32  	                    rpdb.set_trace()
 33  	                    return sheetTitle, colNum
 34
 35  	                colNum +=1
(Pdb) pp cellDate
datetime.datetime(2017, 1, 31, 0, 0)
(Pdb) pp column
u'Jan 31'

Clone this wiki locally