Debugging Python- Have your best debug code at the tip of your fingers

debugging pythonDebugging Python

I’m learning how to program in Python and have put together some decent gui code which can be very helpful when trouble-shooting your code.

Trying to solve issues by just looking at your code can be done but it isn’t nearly as fast as using some real debugging python code.

 

AutoHotKey Debugging Python code

 
#Persistent
#SingleInstance , Force
#NoEnv
Menu, tray, icon, B:ProgsIconsProgsPython.ico, 1
;**********************Py_Lists*********************************
Menu,Py_Lists,Add, Count of x,            Py_Lists_Count

Menu,Py_Lists,Add, Append,                Py_Lists_Append
Menu,Py_Lists,Add, Concatenate,           Py_Lists_Concatenate
Menu,Py_Lists,Add, Insert,                Py_Lists_Insert
Menu,Py_Lists,Add, Pop,                   Py_Lists_Pop
Menu,Py_Lists,Add, Remove,                Py_Lists_Remove
Menu,Py_Lists,Add, Replace x,             Py_Lists_Replace
Menu,Py_Lists,Add, Reverse,               Py_Lists_Reverse
Menu,Py_Lists,Add, Sort,                  Py_Lists_Sort

;**********************Py_Strings*********************************
	;**********************Py_Strings_Case*********************************
	Menu,Py_Strings_Case,Add, Cap 1st Letter,      Py_Strings_Capitalize_first
	Menu,Py_Strings_Case,Add, Lowercase,           Py_Strings_Lower
	Menu,Py_Strings_Case,Add, Uppercase,           Py_Strings_Upper
	Menu,Py_Strings_Case,Add, Title case,          Py_Strings_Title	

Menu,Py_Strings,Add, Change Case, :Py_Strings_Case
	;**********************Py_Strings_Slice*********************************
	Menu,Py_Strings_Slice,Add, Slice me,      Py_Strings_Capitalize_first
Menu,Py_Strings,Add, Slicing, :Py_Strings_Slice
	;**********************Py_Strings_Return*********************************
	Menu,Py_Strings_Return,Add, Count of Substring,  Py_Strings_Count
	Menu,Py_Strings_Return,Add, index of Substring,  Py_Strings_Find
	Menu,Py_String_Returns,Add, Length,              Py_Strings_Length
Menu,Py_Strings,Add, Index and Counts, :Py_Strings_Return
	
	;**********************Py_Strings_Append_Trim*********************************
	Menu,Py_Strings_Append_Trim,Add, LTrim,          Py_Strings_LTrim
	Menu,Py_Strings_Append_Trim,Add, Strip (drops front and back),Py_Strings_Strip
	
Menu,Py_Strings,Add, Append / Trim, :Py_Strings_Append_Trim

	;**********************Py_Strings_Regex*********************************
	Menu,Py_Strings_Regex,Add, email,          			 Py_Strings_Regex_Email
	Menu,Py_Strings_Regex,Add, Match -named subvars,  	 Py_Strings_Regex_Named_Match
	Menu,Py_Strings_Regex,Add, Split on Whitespace, 	 Py_Strings_Regex_Split_WhiteSpace
	Menu,Py_Strings_Regex,Add, Split on range in needle, Py_Strings_Regex_Split_On_Range
Menu,Py_Strings,Add, RegEx, :Py_Strings_Regex

;*******************************************************
;*******************************************************
;**********************Tuples*********************************
Menu, Py_Tuples, Add, Sort, 							Py_Tuple_Sort
Menu, Py_Tuples, Add, Functions Available, 				Py_Tuple_Avail_Funcs
	Menu,Py_Change_Case,Add, Lowercase Tuple,			Py_Tuple_LowerCase
	Menu,Py_Change_Case,Add, Uppercase Tuple,			Py_Tuple_UpperCase
Menu,Py_Tuples,Add, Change Case, 						:Py_Change_Case
;*******************************************************
;**********************Stats*********************************
;*******************************************************
;**********************Pandas*********************************
;**********************Panda Options*********************************
Menu,Py_Pandas_Options,Add, Have df wrap columns,		Py_Pandas_Options_Expand_Frame
Menu,Py_Pandas_Options,Add, Width of your disply,	 	Py_Pandas_Options_Width
Menu,Py_Pandas_Options,Add, Max column width, 			Py_Pandas_Options_Max_Col_Width
Menu,Py_Pandas_Options,Add, Max # columns,	 			Py_Pandas_Options_Max_Col
Menu,Py_Pandas_Options,Add, Max # rows, 				Py_Pandas_Options_Max_Rows
Menu,Py_Pandas_Options,Add,
Menu,Py_Pandas_Options,Add, Column Header Justification,Py_Pandas_Options_ColHead_Justify
Menu,Py_Pandas_Options,Add, Number of decimal places ,	Py_Pandas_Options_Precision
Menu,Py_Pandas_Options,Add, Default encoding, 			Py_Pandas_Options_Encoding
	Menu,Py_Pandas,Add, Options, :Py_Pandas_Options
;**********************Panda Data Frame*********************************
		Menu,Py_Pandas_DataFrame,Add, Select Multiple Columns, 			Py_Pandas_DataFrame_Col_Select
		Menu,Py_Pandas_DataFrame,Add, Frequency Count on Column,		Py_Pandas_DataFrame_Col_Count
		Menu,Py_Pandas_DataFrame,Add, Filter Column on Single value,	Py_Pandas_DataFrame_Col_Filter_Single
		Menu,Py_Pandas_DataFrame,Add, Filter Column on Multiple values,	Py_Pandas_DataFrame_Col_Filter_Multiple
	Menu,Py_Pandas,Add, DataFrame, :Py_Pandas_DataFrame
	
		Menu,Py_Pandas_Graph, Add, Graph necessities for plot,  		Py_Pandas_Graph_Graph_Necess
	Menu,Py_Pandas,Add,Graph, :Py_Pandas_Graph
;*******************************************************
menu,Py_Tuples 		,Add, Tuple example, 						Py_DUMMY_PLACEHOLDER
menu,Py_Stats  		,Add, Pandas, 						:Py_Pandas
menu,Py_Dictionaries,Add, Dictionaries example, 				Py_DUMMY_PLACEHOLDER

;**********************Convert Vars*********************************
Menu,Py_Convert		,Add, Convert Integer to String,     		Py_Convert_To_String
Menu,Py_Convert		,Add, Convert String to Integer,     		Py_Convert_To_Integer
Menu,Py_Convert		,Add, Convert String to Float,  	   		Py_Convert_To_Float
Menu,Py_Convert		,Add,
Menu,Py_Convert		,Add, Convert List to Tuple,	     		Py_Convert_List_to_Tuple
Menu,Py_Convert		,Add, Convert Tuple to List,	     		Py_Convert_Tuple_To_List

;*******************************************************;*******************************************************
;*******************************************************;*******************************************************
;**********************verbs*********************************
;*******************************************************;*******************************************************
;*******************************************************;*******************************************************
	Menu,Py_Funcs_FruitLess,Add,Fruitless Functions, 		Py_DUMMY_PLACEHOLDER
Menu,Py_Funcs	,Add, Fruit LESS Functions, 		:Py_Funcs_FruitLess

	Menu,Py_Funcs_Fruitful,Add,Fruitful Functions, 				Py_DUMMY_PLACEHOLDER
Menu,Py_Funcs	,Add, Fruit FULL Functions, 		:Py_Funcs_Fruitful

	Menu,Py_Loops_While	,Add, Iterate through object, 			Py_DUMMY_PLACEHOLDER
	Menu,Py_Loops_While	,Add, Dedupe, 							Py_DUMMY_PLACEHOLDER
Menu,Py_Loops,Add, While,  	:Py_Loops_While

	Menu,Py_Loops_For	,Add, While loops, 						Py_DUMMY_PLACEHOLDER
Menu,Py_Loops,Add, For,  	:Py_Loops_For
;*******************************************************
;**********************Debugging Python*********************************

Menu,Py_Debugging,Add, Python Path, 						Py_Python_Path
Menu,Py_Debugging,Add, Global Variables and their types,	Py_Globals
Menu,Py_Debugging,Add, Local Variables and their types,		Py_Locals
Menu,Py_Debugging,Add, Structure / Shape,					Py_StructShape
Menu,Py_Debugging,Add, Interactive mode in SciTE,			Py_Interactive
Menu,Py_Debugging,Add, Display Documentation,				Py_Display_Doc
Menu,Py_Debugging,Add, Display Help via Browser,			Py_Display_Help_HTML
Menu,Py_Debugging,Add, List installed Modules and Version,	Py_Installed_Modules_Versions

;**********************Tools  / Utils*********************************
Menu,Py_Tools_Utils,Add, Print function with optional title,Py_Tools_Utils_Print
Menu,Py_Tools_Utils,Add, Current date/time 				   ,Py_Tools_Utils_Date_Time
Menu,Py_Tools_Utils,Add, Launch iPython,					Py_Tools_Utils_iPython

;**********************Python Main menu*********************************
Menu,Python, Add, Lists,       	:Py_Lists
Menu,Python, Add, Strings,     	:Py_Strings
Menu,Python, Add, Tupless,     	:Py_Tuples
Menu,Python, Add, Dictionaries,	:Py_Dictionaries
Menu,Python, Add, Convert Vars,	:Py_Convert
Menu,Python, Add, Stats,		:Py_Stats
Menu,Python, Add,  ;blank line to separate
Menu,Python, Add, Functions,	:Py_Funcs
Menu,Python, Add, Loops,		:Py_Loops
Menu,Python, Add,  ;blank line to separate
Menu,Python, Add, Debugging,		:Py_Debugging
Menu,Python, Add, Tools / Utilities,:Py_Tools_Utils
;~ Menu,Python, Add, Non-Fruitful Functions,:Py_Fruit_Func

^+Lbutton::Menu, Python, Show  ; Control + Left mouse button
;*******************************************************
Py_DUMMY_PLACEHOLDER:
return

;**********************Debugging*********************************
Py_Python_Path:
ClipProcess("import sys  # Shows Executable, bitness and paths`r`nprint ""rExecutable:"",sys.executable,""r  Platform:"",sys.platform,""r""`r`nfor i in sorted(sys.path): print ""      Path: "",i`r`n")
Return

Py_Globals:
ClipProcess("for varname,value in globals().items():`r`n  print varname,' = ',value,' : ',type(value)  # Prints global variables")
return
Py_Locals:
ClipProcess("for varname,value in locals().items():`r`n  print varname,' = ',value,' : ',type(value)  # Prints Local variables")
return
Py_StructShape:
ClipProcess("from structshape import structshape  #prints structure and shape of object from Think Python`rprint structshape(MyVar)")
return

Py_Interactive:
ClipProcess("import code; code.interact(local=locals())  #Allows shell w/in SciTe- look at output window")
return

Py_Display_Doc:
ClipProcess("help(MyModule)  # swap out MyModule with Method or object")
return

Py_Display_Help_HTML:
ClipProcess("import os `; os.system('python -m pydoc -g')  #Display help in HTML")
return

Py_Installed_Modules_Versions:
ClipProcess("import pip; print('n'.join(sorted(['%s=%s' % (i.key, i.version) for i in pip.get_installed_distributions()])))  #Lists installed Python Modules and their versions")

return
;**********************Python*********************************
Py_Lists_Append:
ClipProcess("MyList.append(x)  # Add an item to the end of the list; x is new item")
Return

Py_Lists_Count:
ClipProcess("ct = MyList.count('a')  # Return the number of times x appears in the list")
Return

Py_Lists_Concatenate:
ClipProcess("MyList + [36,49,64]  # Concatenate values to list")
Return

Py_Lists_Insert:
ClipProcess("MyList.insert(7,'Insert 7')  # Insert item at position. MyList.insert(0,'32') inserts 32 at beginning")
Return

Py_Lists_Pop:
ClipProcess("MyList.pop(7)  # Remove item at position in the list, AND RETURN IT. If no index is specified, removes and returns the last item. (The brackets around the i denote that the parameter is optional)")
Return

Py_Lists_Remove:
ClipProcess("MyList.remove('text')  # Remove the FIRST item from the list whose value is x. It is an error if there is no such item.")
Return

Py_Lists_Replace:
ClipProcess("MyList[7]= '65'  # Replace the indexed value with 65")
Return

Py_Lists_Reverse:
ClipProcess("MyList.reverse()  # Reverse the elements of the list")
Return

Py_Lists_Sort:
ClipProcess("list_Name.sort(cmp=None, key=None, reverse=False)  # Sort the items of the list in place (the arguments can be used for sort customization, see sorted() for their explanation).")
Return
;**********************Python Strings*********************************
Py_Strings_Capitalize_first:
ClipProcess("MyVar = MyVar.capitalize()  # Use w/o 'MyVar =' if you don't need to change original variable")
return

Py_Strings_Count:
ClipProcess("ct = MyVar.count('l',0,33) # Return the # of non-overlapping occurrences of substring sub in the range (range is optional)")
Return

Py_Strings_Find:
ClipProcess("i = MyVar.find('l',0,33)  # Return the lowest index in the string where substring sub is found. Returns -1 if not found")
Return

Py_Strings_Length:
ClipProcess("len(MyVar)  # obtain length of string")
Return

Py_Strings_Lower:
ClipProcess("MyVar = MyVar.lower()  # Use w/o 'MyVar =' if you don't need to change original variable")
Return

Py_Strings_LTrim:
ClipProcess("MyVar = MyVar.lstrip('trim')  # Return a copy of the string with leading characters removed. (Removes whitespace if no charcters provided")
Return

Py_Strings_Strip:
ClipProcess("MyVar = MyVar.strip('trim')  # Return a copy of the string with leading and ending characters removed. (Removes whitespace if no charcters provided")
Return
Py_Strings_Upper:
ClipProcess("MyVar = MyVar.upper()  # Use w/o 'MyVar =' if you don't need to change original variable")
Return

Py_Strings_Title:
ClipProcess("MyVar = MyVar.title()  # Use w/o 'MyVar =' if you don't need to change original variable")
Return

;**********************REgex*********************************
Py_Strings_Regex_Email:
ClipProcess("import re`rstr = 'purple [email protected] monkey dishwasher'`rmatch = re.search('([w.-]+)@([w.-]+)', str)`rif match:`r    print match.group()   ## '[email protected]' (the whole match)`r    print match.group(1)  ## 'alice-b' (the username, group 1)`r    print match.group(2)  ## 'google.com' (the host, group 2)")
Return

Py_Strings_Regex_Named_Match:
ClipProcess("import re`r######### Named Regex extracting address`rname = r'[a-zA-z_]w+'`rneedle = '(?:' + name + ')'`rhaystack='Joe King 836 Kilbridge Ln.  Coppell  tx  75029nJoe Two 836 Kilbridge Ln.  Coppell  tx  75029'`rneedle='(?P<First_Name>w+)s+(?P<Last_Name>w+)s+(?P<Address>d{1,5}s+w+s+w+.?)s+(?P<City>w+)s+(?P<State>ww)s+(?P<Zipcode>d{1,5})'`rm = re.match(needle, haystack,re.I)`rprint m.group('First_Name'),m.group('Last_Name'), m.group('Address'), m.group('City'), m.group('State').upper(),m.group('Zipcode')")
return

Py_Strings_Regex_Split_WhiteSpace:
ClipProcess("import re`rprint(re.split(r'(s*)', 'heare are some words'))  # s is whitespace and splits Haystack")
Return

Py_Strings_Regex_Split_On_Range:
ClipProcess("import re`rprint(re.split(r'[a-fA-F]', 'jfdsklajfkeawfjkSawDFaw',re.I|re.M)) #splits on range listed in needle")
Return
;*******************************************************
;**********************Tuples*********************************
Py_Tuple_Avail_Funcs:
ClipProcess("print(dir(MyTuple))  # List of functions available on Tuple")
Return

Py_Tuple_UpperCase:
ClipProcess("Lower_MyTuple=[x.lower() for x in MyTuple]  #List comprehension on Tuple")
return

Py_Tuple_LowerCase:
ClipProcess("Upper_MyTuple=[x.upper() for x in MyTuple]  #List comprehension on Tuple")
return

Py_Tuple_Sort:
ClipProcess("s=sorted(MyTuple)  #Sort tuple")
return
;*******************************************************
;**********************stats*********************************
;**********************pandas*********************************
;**********************Pandas Options*********************************
Py_Pandas_Options_width:
ClipProcess("pd.options.display.width = 1000  #Set default width")
return
Py_Pandas_Options_Max_Col_Width:
ClipProcess("pd.options.display.max_colwidth = 100  #Set Max col width")
return
Py_Pandas_Options_Max_Col:
ClipProcess("pd.options.display.max_columns = 10  #None =no max cols 0=Autodetect")
return
Py_Pandas_Options_Max_Rows:
ClipProcess("pd.options.display.max_rows = 50  #Max number of rows")
return
Py_Pandas_Options_Expand_Frame:
ClipProcess("pd.options.display.expand_frame_repr = False  #show DataFrame across multiple lines")
return
Py_Pandas_Options_Precision:
ClipProcess("pd.options.display.precision = 4  #Control decimals")
return
Py_Pandas_Options_ColHead_Justify:
ClipProcess("pd.options.display.colheader_justify = 'left' #left right justify header")
return
Py_Pandas_Options_Encoding:
ClipProcess("pd.options.display.encoding = 'UTF-8' #this is default so prob not needed")
return
;**********************data frame*********************************
Py_Pandas_DataFrame_Col_Select:
ClipProcess("mydf=df[['MyCols 1','MyCols 2','MyCols 3']][0:14]  #Select columns and first 15 rows")
Return

Py_Pandas_DataFrame_Col_Count:
ClipProcess("df['MyCol 1'].value_counts()  #Count of items- use max row to expand")
return

Py_Pandas_DataFrame_Col_Filter_Single:
ClipProcess("myFiltered=df['MyCol'] == 'My Value'  #filter single column on value")
return

Py_Pandas_DataFrame_Col_Filter_Multiple:
ClipProcess("MyFilter_1 = df['MyVar 1'] == 'MyVal 1'  #Filter 1`rMyFilter_2 = df['MyVar 2'] == 'MyVal 2'  #Filter 2`rMyFilterd_All_Cols = df[MyFilter_1 & MyFilter_2][:99] #2 filters, all cols, 99 rows`rMyFilterd_Spc_Cols = df[MyFilter_1 & MyFilter_2][['MyVar 1','MyVar 2','MyVar 3']][:99]  #2 filters, spec cols, 99 rows")
return

;**********************Pandas- Graph*********************************
Py_Pandas_Graph_Graph_Necess:
ClipProcess("import matplotlib.pyplot as plot  #Necessary to add so plot will show up`nplot.show()")
Return

;**********************Convert Var*********************************
Py_Convert_To_Integer:
ClipProcess("MyVar = int(MyVar)  # Converts string to Integer. Use w/o 'MyVar =' if you don't need to change original variable")
return

Py_Convert_To_String:
ClipProcess("MyVar = str(MyVar)  # Converts to string. Use w/o 'MyVar =' if you don't need to change original variable")
return

Py_Convert_To_Float:
ClipProcess("MyVar = float(MyVar)  # Converts to float. Use w/o 'MyVar =' if you don't need to change original variable")
return

Py_Convert_List_to_Tuple:
ClipProcess("MyList=list(MyTuple)  #re-casting tuple to list")
Return

Py_Convert_Tuple_To_List:
ClipProcess("MyTuple=tuple(MyList)  #re-casting list to tuple")
return

;**********************Tools / Utilities*********************************
Py_Tools_Utils_Print:
ClipProcess("def p(arg,title=''):  #My Print Function`r`tprint('<<<---START------'+str(title)+'------>>>r'+str(arg)+'r<<<---END------'+str(title)+'--->>>rr')")
return

Py_Tools_Utils_Date_Time:
ClipProcess("import datetime `; print('Today is: {0:%a %b %d %H:%M:%S %Y}'.format(datetime.datetime.now()))")
return

Py_Tools_Utils_iPython:
ClipProcess("import os `; os.system('ipython notebook')  #Display help in HTML")
return


;~ ClipProcess("")
;**************ClipProcess function*********************************
ClipProcess(TXT){
TXT.="`r`n`r`n"
Store:=Clipboard
Clipboard:=TXT
Send, ^v
Sleep, 60
Clipboard:=Store
}
RAlt::
Browser_Forward::Reload

There are other tools for debugging Python but this is a good starting place.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.