Ir al contenido principal

I have been playing with pdb for debugging code (introductory level)

Well, this is time to talk about something I have been playing, the pdb standard module python provides. I'll talk in the context of py 3.7+ since there are differences that improve how to work with this.

Well, first of all, as you should found in this field and coding experiences and challenges. You have to deal with bugs, unexpected errors, or even worst, unexpected behavior which sometimes is most difficult to trace.

Here are some lights on how to use it, this is really useful if your life is coding :). Consider the following commands table.

CommandDescription
sExecute the current line and stop at the first possible occasion.
nContinue the execution until the next line in the current function is reached or it returns.
pShows the values of variables in context code.
llList the whole source code for the current function or frame
lIn contrast to ll, this command shows a shorter snippet of code.
l.If you pass the param . to this command, it will show you always 11 lines around the current line.
bSet a breakpoint, you can specify either a line number or function name where execution will stop.
cContinues execution until a breakpoint is found.
qQuit debugging and exit.

Having seen the table above, then let's play a bit. See the following example:


After running python example1.py, pdb comes in place, you'll see three important elements:
  1. the file, and line number in which pdb stand at this moment
  2. line code to see the context in which we are
  3. the pdb command line waiting for a command
Let's run some commands there:


I entered ll command (see on the table it meaning), as you can realize it, it shows all the context of line 14, remember this command shows more lines to better understand the context of the code, also you always see the -> that shows you where you are. After that first command entered, I ran n which continues to the next line (15).

You could always check values on any variable while you are walking throughout your code, use the command p and the variable name as follow:


Finally, use q to exit pdb module.

Next post, we'll talk and play with breakpoint functionality.




Comentarios

Entradas populares de este blog

Follow up Java.

Java: So far, so good! A poco más de 20 días de una nueva aventura profesional, la perspectiva de aprovechamiento, tiempos de aprendizaje y transferencia del conocimiento ha sido interesante, en realidad muy positiva. Después de casi 3 semanas de haber comenzado,  el aprendizaje de los conceptos base (knowledge base, término ampliamente utilizado en el área),  la familiarización con la estructura del código, la codificación en sí, comienzan a dar frutos y a tomar forma. ¿Qué hemos aprendido hasta el momento? Estructura del lenguaje Tipos de variables, variables de miembros y cómo declararlas Uso de operadores y concepto de operandos Estructura básica de las clases, objetos, interfaces, packages Herencia de objetos, instancias, atributos estáticos, parámetros, variables, métodos Entre otros temas, esos son los avances. En retrospectiva, el bagaje de conocimiento  previo  (en lo personal)    ha sido de gran impacto en términos positivos y la curva de aprendizaje mu

How to delete one or more documents from your MongoDB repository

MongoDB seems to be complex at first sign, but in fact is not, you could perform a complete CRUD easily if you expend some time on the documentation. For example, lets say you need to delete some documents (rows, if you are thinking on a traditional RDBMS). First you want to find all these documents to be removed. > db.your_collection.find({ "cvectry": "MX", "layer_id": 20 }, {}) { "_id" : ObjectId("5e8123e8892355c921e6b436"), "layer_id" : 20 } > After finding all these documents to be removed, then you want to perform the following delete method as follow: > db.your_collection.remove({ "cvectry": "MX", "layer_id": 20 }, { justOne: true }) WriteResult({ "nRemoved" : 1 }) > In this case, after finding all these documents that match with "cvectry": "MX", "layer_id": 20. I got the result on the first command I ran, so I just wanted to delete just on