Guest User

python grid

a guest
Feb 6th, 2022
152
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.20 KB | None | 0 0
  1. try :
  2.     from termcolor import colored
  3. except ImportError :
  4.     import os
  5.     os.system("pip install termcolor")
  6.     from termcolor import colored
  7. class Grid:
  8.     def __init__(self, x, y, debug=False):
  9.         self.x = x
  10.         self.y = y
  11.         self.gridValue = self.x * self.y
  12.         self.grid = {}
  13.         self.debug = debug
  14.         count = 1
  15.         # initialising grid
  16.         """
  17.        for i in range(self.x) :
  18.            self.grid[f"column{i}"] = None
  19.            for x in range(self.y):
  20.  
  21.                for i in range(self.x) :
  22.                    print(i)
  23.                    if i == 0 :
  24.                        count += 1
  25.                    print(count - 1)
  26.                    self.grid[f"row{i}"] = {f"column{i}"}
  27.        """
  28.         for i in range(self.gridValue) :
  29.             self.grid[f"obj{i}"] = None
  30.         if self.debug == True :
  31.             print(self.grid)
  32.             print(self.gridValue)
  33.             print(len(self.grid))
  34.             self.gridIntermostPartsToComplete = self.gridValue - len(self.grid)
  35.             print(colored("If this shows something other than 0, Something is wrong :", "blue"))
  36.             print(colored(self.gridIntermostPartsToComplete, "green"))
  37.             if self.gridIntermostPartsToComplete != 0 :
  38.                 print(colored("Thats an error! The intermost parts need building! Your parameters may be incorrect or your module might be faulty!", "red"))
  39.  
  40.     def insert(self, data, object):
  41.         self.grid[object] = data
  42.  
  43.     def visualize(self):
  44.         table = []
  45.         for i in range(len(self.grid)) :
  46.             if i % self.x == 0 :
  47.                 current = str(self.grid[f'obj{i}']) + "\n"
  48.                 print(current),
  49.                 table.append(current)
  50.             else :
  51.                 current = str(self.grid[f'obj{i}'])
  52.                 print(current),
  53.                 table.append(current)
  54.  
  55.         if self.debug == True :
  56.             print(table)
  57.         '''    
  58.        for i in range(len(table)) :
  59.            print(table[i], sep="")
  60.        '''
  61.  
  62.  
  63.  
  64.  
  65. # column=x, row=y
  66. grid = Grid(4,4, debug=False)
  67. grid.insert("hi","obj2")
  68. grid.insert("hello", "obj1")
  69. grid.insert("hey", "obj0")
  70. grid.visualize()
Advertisement
Add Comment
Please, Sign In to add comment