Skip to content
This repository has been archived by the owner on Aug 28, 2023. It is now read-only.

Latest commit

 

History

History
36 lines (20 loc) · 2.63 KB

README.md

File metadata and controls

36 lines (20 loc) · 2.63 KB

Debugging Python using PDB

The Python DeBugger (PDB) is a built-in module in the C-Python interpreters standard library. Thus you can import and use it in any python file. Just add import pdb; pdb.set_trace() anywhere to create a break point and arrive in an interactive REPL.

Python Debugger Shell

You then have a set of commands to navigate around, most of them having one letter shortcuts - press enter to execute them. The most important are:

  • l: show the current and surrounding lines of code
  • n: to execute the next step
  • c: close the REPL and continue execute
  • q: stop execution all together
  • p: print the following variable
  • h: shows you the help for you all commands

Don't forget that you are in a fully blown python interpreter with the current state arround you. Meaning you can use all introspection tools of python (like dir, __dict__, locals() or help) or even overwrite current data.

The Challenge

Alright then, let's take a look at the challenge. The function you are having is an object converter, iterating through the items of a dictionary and placing them in a specific format in an array. It is used to speak to an external API, which requires this format. They complained that the input you send them often claims to send format=int but then the values can't parsed as int. Which is why API calls fail sometimes.

You can execute the code with python2.7 directly. The output shows that this is indeed the case. Uncomment the python debugger and use the above mentioned commands to move around in the execution and examine, what goes wrong and when. Then propose a fix, try to fix it and run it again. Repeat until fixed.

Further Reading