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
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.
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?
My script is called findHHIvan1.py and i call it in this way #python3findHHIvan1.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
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: