Advertisement
Lenoch

fiver request

Jul 28th, 2023
1,101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 10.52 KB | None | 0 0
  1. # So, I have a grid of "buttons", stored in a list, in a json. ( config["front"]["buttons"] = [{},{},{},..] )
  2. # It's not a matrix, but you can convert it with this code:
  3.  
  4. def create_matrix(config):
  5.     matrix = []
  6.     for folder_name, folder_content in config["front"]["buttons"].items():
  7.         row_count = 0
  8.         for count, button in enumerate(folder_content, start=1):
  9.             if row_count >= len(matrix):
  10.                 matrix.append([])
  11.             matrix[row_count].append(button)
  12.             calc_height = int(config['front']['width'])
  13.             if count % calc_height == 0:
  14.                 row_count += 1
  15.         break
  16.     return len(matrix), len(matrix[0])
  17.  
  18. # With this function you can also get the old height and width:
  19.  
  20. oldheight, oldwidth = create_matrix(config)
  21.  
  22.  
  23. # It will be necessary to put it back to normal at the end of the script with this code:
  24.  
  25. def unmatrix(matrix):
  26.     new_folder_content = []
  27.     for row in matrix:
  28.         for button in row:
  29.             new_folder_content.append(button)
  30.     return new_folder_content
  31.  
  32. # The matrix can contain voids ( {"VOID": "VOID"} ), and your objective is to create an `update_gridsize(config)` function to
  33. # modify the size of the grid. If the size increases just add a line, which I have already coded:
  34.  
  35. if oldheight != int(config['front']['height']):
  36.     if oldheight < int(config['front']['height']):
  37.         difference = int(config['front']['height']) - oldheight
  38.         for _ in range(difference):
  39.             for folder_name, folder_content in config["front"]["buttons"].items():
  40.                 for _ in range(int(config['front']['width'])):
  41.                     folder_content.append({"VOID": "VOID"})
  42.  
  43. if oldwidth != int(config['front']['width']):
  44.     if oldwidth < int(config['front']['width']):
  45.         difference = int(config['front']['width']) - oldwidth
  46.         for folder_name, folder_content in config["front"]["buttons"].items():
  47.             for diff_count, _ in enumerate(range(difference)):
  48.                 additional_count = 0
  49.                 for count, button in enumerate(folder_content, start=1):
  50.                     calc_width = oldwidth + diff_count
  51.                     if count % calc_width == 0:
  52.                         print(f'{count} | {additional_count}')
  53.                         folder_content.insert(count+additional_count, {"VOID": "VOID"})
  54.                         additional_count += 1
  55.  
  56. # If the size decreases, all the buttons must absolutely remain, and must be moved if necessary, in order to be able to have the
  57. # reduced size of a line in the end (if this is what is required). If the requested height is really small, you have to do everything
  58. # to enter all the buttons in the respected size. If that's really impossible even removing all the void,
  59. # just print("can't resize the grid")
  60. # Also, grid size are stored in config['front']['height'] and config['front']['width']
  61.  
  62. # Please let me know if you have a question or if I was unclear in my request
  63.  
  64.  
  65. # Here's my entiere code that I tried, but it got a lot of bugs (cause I'm bad at coding):
  66.  
  67.  
  68. def update_gridsize(config):
  69.    
  70.     # create matrix to calculate the height
  71.     def create_matrix(config):
  72.         matrix = []
  73.         for folder_name, folder_content in config["front"]["buttons"].items():
  74.             row_count = 0
  75.             for count, button in enumerate(folder_content, start=1):
  76.                 if row_count >= len(matrix):
  77.                     matrix.append([])
  78.                 matrix[row_count].append(button)
  79.                 calc_height = int(config['front']['width'])
  80.                 if count % calc_height == 0:
  81.                     row_count += 1
  82.             break
  83.         return len(matrix), len(matrix[0])
  84.  
  85.     oldheight, oldwidth = create_matrix(config)
  86.  
  87.  
  88.     if oldheight != int(config['front']['height']):
  89.         if oldheight < int(config['front']['height']):
  90.             difference = int(config['front']['height']) - oldheight
  91.             for _ in range(difference):
  92.                 for folder_name, folder_content in config["front"]["buttons"].items():
  93.                     for _ in range(int(config['front']['width'])):
  94.                         folder_content.append({"VOID": "VOID"})
  95.  
  96.         else:
  97.             print('height decreased')
  98.  
  99.             difference = oldheight - int(config['front']['height'])
  100.             print(f'difference: {difference}')
  101.             print(f'oldheight: {oldheight}')
  102.             print(f"newheight: {config['front']['height']}")
  103.            
  104.             def remove_height():
  105.                 remove_success = True
  106.                 reversed_matrix = []
  107.                 if remove_success:
  108.                     for folder_name, folder_content in config["front"]["buttons"].items():
  109.                         before_config = config["front"]["buttons"].copy()  # Copier la configuration actuelle
  110.            
  111.                         def unmatrix(matrix):
  112.                             new_folder_content = []
  113.                             for row in matrix:
  114.                                 for button in row:
  115.                                     new_folder_content.append(button)
  116.                             return new_folder_content
  117.            
  118.                         matrix = []
  119.                         row_count = 0
  120.                         for count, button in enumerate(folder_content, start=1):
  121.                             if row_count >= len(matrix):
  122.                                 matrix.append([])
  123.                             matrix[row_count].append(button)
  124.                             calc_height = oldwidth + diff_count
  125.                             if count % calc_height == 0:
  126.                                 row_count += 1
  127.                         print(matrix)
  128.                         new_height = len(matrix)
  129.            
  130.                         success = False
  131.                         for row in reversed(matrix):
  132.                             if all(element == {"VOID": "VOID"} for element in row):
  133.                                 matrix.remove(row)
  134.                                 success = True
  135.                                 break
  136.            
  137.                         if success:
  138.                             folder_content = unmatrix(matrix)
  139.                         else:
  140.                             reversed_matrix = []
  141.                             for col in range(len(matrix[0])):
  142.                                 reversed_matrix.append([])
  143.                                 for row in range(len(matrix)):
  144.                                     try:
  145.                                         reversed_matrix[col].append(matrix[row][col])
  146.                                     except:
  147.                                         pass
  148.                             print(reversed_matrix)
  149.            
  150.                             success = True
  151.                             for col in reversed_matrix:
  152.                                 if success:
  153.                                     try:
  154.                                         empty_index = len(col) - col[::-1].index({"VOID": "VOID"}) - 1
  155.                                         for i in range(empty_index, len(col) - 1):
  156.                                             col[i] = col[i + 1]
  157.                                     except ValueError:
  158.                                         success = False
  159.                                         print("Impossible de replacer le bouton.")
  160.            
  161.                             if success:
  162.                                 for col in reversed_matrix:
  163.                                     col.pop()
  164.            
  165.                                 result_matrix = []
  166.                                 for col in range(len(reversed_matrix[0])):
  167.                                     result_matrix.append([])
  168.                                     for row in range(len(reversed_matrix)):
  169.                                         try:
  170.                                             result_matrix[col].append(reversed_matrix[row][col])
  171.                                         except:
  172.                                             pass
  173.                                 matrix = result_matrix
  174.                                 del result_matrix
  175.                                 folder_content = unmatrix(matrix)
  176.                             else:
  177.                                 remove_success = False
  178.                                 config["front"]["buttons"] = before_config  # Restaurer la configuration précédente
  179.            
  180.                         if remove_success:
  181.                             config["front"]["buttons"][folder_name] = folder_content
  182.                         else:
  183.                             config["front"]["buttons"][folder_name] = before_config[folder_name]
  184.            
  185.                         del matrix
  186.                         try:
  187.                             del reversed_matrix
  188.                         except:
  189.                             pass
  190.            
  191.             for diff_count, _ in enumerate(range(difference)):
  192.                 remove_height()
  193.            
  194.  
  195.             # after_height, after_width = create_matrix(config)
  196.             # while after_height > int(config['front']['height']):
  197.             #     print(f'After height: {after_height}')
  198.             #     print(f"Before height: {config['front']['height']}")
  199.             #     remove_height()
  200.             #     after_height, after_width = create_matrix(config)
  201.  
  202.             # creates a infinite loop sometimes for some reason
  203.  
  204.  
  205.     if oldwidth != int(config['front']['width']):
  206.         if oldwidth < int(config['front']['width']):
  207.             difference = int(config['front']['width']) - oldwidth
  208.             for folder_name, folder_content in config["front"]["buttons"].items():
  209.                 for diff_count, _ in enumerate(range(difference)):
  210.                     additional_count = 0
  211.                     for count, button in enumerate(folder_content, start=1):
  212.                         calc_width = oldwidth + diff_count
  213.                         if count % calc_width == 0:
  214.                             print(f'{count} | {additional_count}')
  215.                             folder_content.insert(count+additional_count, {"VOID": "VOID"})
  216.                             additional_count += 1
  217.         else:
  218.             print('width decreased')
  219.            
  220.            
  221.  
  222.  
  223.     # recreate matrix to calculate the height
  224.     new_height, new_width = create_matrix(config)
  225.     config["front"]["height"] = str(new_height)
  226.     config["front"]["width"] = str(new_width)
  227.  
  228.     with open('config.json', 'w', encoding="utf-8")as json_file:
  229.         json.dump(config, json_file, indent=4)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement