PYTHON   175

Add debug version

Guest on 6th August 2021 08:16:57 PM

  1. import os
  2.  
  3. file_to_debug = input("What file do you wanna print-debug? ")
  4. file_to_debug_path = os.path.dirname(str(file_to_debug))
  5. print(file_to_debug_path)
  6. file_to_debug_name = os.path.basename(file_to_debug)
  7. print(file_to_debug_name)
  8. new_file_lines = []
  9.  
  10.  
  11. def get_num_of_whitespaces():
  12.     original_list = line.split(sep=' ')
  13.     index = 0
  14.     for char in original_list:
  15.         if char == '':
  16.             index += 1
  17.         else:
  18.             return len(line[:index])
  19.  
  20. def how_many(char: str):
  21.     subtract = 1
  22.     going_times = 2
  23.     indent_str = None
  24.     got_it = False
  25.     temp_indent_str = ''
  26.    
  27.     if char == ' ':
  28.         if not len(line) == 1:
  29.             if line.startswith(' '):
  30.                 whitespaces = get_num_of_whitespaces()
  31.                 indent_str = ' '*whitespaces
  32.     else:
  33.         return '\t'
  34.     return indent_str
  35.    
  36.    
  37. with open(file_to_debug_path+'/'+file_to_debug_name, 'r') as file:
  38.     file_contents = file.readlines()
  39.     going_num = 1
  40.     print_num = 0
  41.     for line in file_contents:
  42.         if not len(line.strip()) == 0:
  43.             if line.startswith('\t'):
  44.                 indent_str = how_many(char='\t')
  45.             elif line.startswith(' '):
  46.                 indent_str = how_many(char=' ')
  47.             else: indent_str = None
  48.             if indent_str:
  49.                 new_line = "{0}print({1})\n".format(indent_str, going_num)
  50.             else:
  51.                 new_line = "print({0})\n".format(going_num)
  52.             if not(
  53.                     line.strip().startswith('#') or
  54.                     line.strip().startswith('elif') or
  55.                     line.strip().startswith('else') or
  56.                     line.strip().startswith('except')
  57.                 ):
  58.                 new_file_lines.append(new_line)
  59.                 print('  ', new_line)
  60.                 print_num += 1
  61.         new_file_lines.append(line)
  62.         print(going_num, line)
  63.         going_num += 1
  64.     print("-------Added {0} print lines. Your new file is called dubug_{1}-------".format(print_num, file_to_debug_name))
  65.     os.chdir(file_to_debug_path)
  66.     with open(file_to_debug_path+'/'+"debug_{0}".format(file_to_debug_name), 'w') as new_file:
  67.         new_file.writelines(new_file_lines)

Raw Paste


Login or Register to edit or fork this paste. It's free.