Debug Module in Python 3.x

Hi team, I need to debug step by step a script in python.
For example I need to debug a function

def convert_dict3(dict): #transforms dictionary into list, to be able to sort it
found = [[k,v] for k, v in dict.items()] #dict: {term → {id → {file → [message]}}} into list: [term, {id → {file → [message]}}]
for i in found: #for each term, transform dictionaries to list, like this:
i[1] = [[k,v] for k, v in i[1].items()] #[term, {id → {file → [message]}}] into [term, [id, {file → [message]}]]
for j in i[1]: #for each id, transform dictionaries to list, like this:
j[1] = [[k,v] for k, v in j[1].items()] #[term, [id, {file → [message]}]] into [term, [id, [file, [message]]]]
found.sort() #sorts list by searching criteria/terms
for i in found:
i[1].sort() #sorts deeper levels, ids
for j in i[1]:
j[1].sort() #sort files
for k in j[1]:
k[1].sort() #sort messages
return found #returns list structure based of dictionary

Regards

Please read this that explains how to post code here About the Python Help category

One way to debug code is to use the print function to print information you need to understand what the code is doing.

1 Like

Thanks Barry, if i use print function and the module finish with return variable. And the module has argument for exsmple

Def. Test( date, getdata):




Return search

How can to test/debug this module?

Regards

Again: Post your code as preformatted text!

to test a function in a module you will.

  1. import the module
  2. call the function with the arguments you want to test with

You will see the print output in the terminal.

Could you have any example about it?

Regards

This is what he means by formatting:

I need to read this module for example

def check_message(message, searching_criteria, file, found):                #evaluates each individual message to see if they have all terms in criteria
    matches = 0
    id = message[34:45]                                                     #identifies ID of each message
    datetime = message[1:24]                                                #identifies date and time of each message
    checklist = create_check_list(searching_criteria)                       #create checklist of terms to see if they were found and its last position
    for i in range(len(checklist)):
        term = checklist[i][0]
        if term[0] == "~":                                                  #in case you are looking for negatives, that is, terms that should not be present
            aux = term[1:]
            if message.find(aux) == -1:
                matches += 1
            else:
                break
        else:
            prev_pos_found = 0
            for j in range(i):
                prev_term = checklist[j][0]
                if prev_term == term:
                    prev_pos_found = checklist[j][1]                        #looks if term has been found already, if so, gets last found position
            new_pos_found = message.find(term, prev_pos_found)              #looks for term from last position found onwards
            if new_pos_found != -1:
                checklist[i][1] = new_pos_found + 1                         #if term is found, saves last position for future search
                matches += 1                                                #how many matches done so far
            else:
                break
    if matches == len(checklist):                                           #if matches equals to number of terms, adds searching criteria (comma separated strings) to dictionary
        print ( matches )
        text = ""
:$                                                                                                                                                                                                                                                          
        i[1].sort()                                                         #sorts deeper levels, ids
        for j in i[1]:
            j[1].sort()                                                     #sort files
            for k in j[1]:
                k[1].sort()                                                 #sort tran
                for l in k[1]:
                    l[1].sort()                                             #sort messages
    return found                                                            #returns list structure based of dictionary


Ok I am going to assume you need explicit instructions.
Start python from your terminal.
On windows type py on linux or macOS type python3.
You should get the python prompt >>>.
I will assume your module is called mymodule, replace with the real name.
I also have no idea what form the args to the function tame you will need to fill in details.

>>> import mymodule
>>> mymodule. check_message(“a message…”, etc, etc, etc)

I need to trace the function check_message, modules i haven’t related to this function

check_message(message, searching_criteria, file, found):
...
....

    return found  

So How Can I trace this function? I need to see all the lines debugged

Regards and thanks for your help!

Any function exists within a module (or script). So you have to start by importing that.
Say your function is in a file called myfile.py. Then you do

import myfile

Next, you define the arguments to the function:

message = 'some_message'
searching_criteria = ['this', 'that']
file = 'myfile.txt'
found = False

Feel free to replace the values (the stuff following the ‘=’) with whatever you need, these are just examples.
Finally, you call the function:

result = myfile.check_message(message, searching_criteria, file, found)

If you want to trace the function using Python’s builtin debugger pdb, you call it a bit differently:

import pdb
pdb.run(myfile.check_message(message, searching_criteria, file, found))

consult the Python docs for more info on what you can do with pdb.

N.B. instead of importing the entire module you can also import just the function:

from myfile import check_message

and then call it like this:

result = check_message(...)

Hi Albert,

Thanks for your answer. I need to trace a script that it is running now. this python script running with files linux. I need to check the function in real time.
How can I do it?

Regards

My script is called findHHIvan1.py and i call it in this way
#python3 findHHIvan1.py -s -f message.log.2024-05-10_1742.2024-05-10_1744.cexpswap4c -d //path

#!/usr/bin/env python

import sys
import os
import glob
import gzip
import time
import datetime
import codecs

from findHHIvan1.py import check_message

def check_message(message, searching_criteria, file, found):                #evaluates each individual message to see if they have all terms in criteria
    matches = 0
    id = message[34:45]                                                     #identifies ID of each message
    datetime = message[1:24]                                                #identifies date and time of each message
    checklist = create_check_list(searching_criteria)                       #create checklist of terms to see if they were found and its last position
    for i in range(len(checklist)):
        term = checklist[i][0]
        if term[0] == "~":                                                  #in case you are looking for negatives, that is, terms that should not be present
            aux = term[1:]
            if message.find(aux) == -1:
                matches += 1
            else:
                break
        else:
            prev_pos_found = 0
            for j in range(i):
                prev_term = checklist[j][0]
                if prev_term == term:
                    prev_pos_found = checklist[j][1]                        #looks if term has been found already, if so, gets last found position
            new_pos_found = message.find(term, prev_pos_found)              #looks for term from last position found onwards
            if new_pos_found != -1:
                checklist[i][1] = new_pos_found + 1                         #if term is found, saves last position for future search
                matches += 1                                                #how many matches done so far
            else:
                break
    if matches == len(checklist):                                           #if matches equals to number of terms, adds searching criteria (comma separated strings) to dictionary
        text = ""
        for i in searching_criteria:
            text = text + i + ","
        text = text[:-1]
        found = add_term(text, id, datetime, file, found)

    return found                                                            #returns a dictionary of all 




How can trace this function in real time?

Regards

I guess other people thought that by “debug” you mean just trying to figure out the problem, by making some guesses about what goes wrong and then trying to test them.

But it seems like you really mean “trace what the code is doing, step by step; set a breakpoint so the code will stop running at a specific point, and examine the program state at that point; etc.”.

For this task, please see the documentation for the built-in debugger: