<commit_before>import Tkinter as tk


class GridView(tk.Frame):

    def __init__(self, grid, master=None):
        tk.Frame.__init__(self, master)
        self.grid = grid
        
        self.master.bind("<w>", self.callback)
        self.master.bind("<a>", self.callback)
        self.master.bind("<s>", self.callback)
        self.master.bind("<d>", self.callback)

        self.text_IDs = []
        self.SIDE = 100
        self.canvas = tk.Canvas(
            master, width=self.SIDE * 4, height=self.SIDE * 4)
        self.canvas.pack()

    def layout_matrix(self, matrix):
        for ID in self.text_IDs:
            self.canvas.delete(ID)
        self.text_IDs = []
        for i in range(4):
            for j in range(4):
                current_rect = self.rects[i][j]
                currrent_cell = matrix[j][i]
                if currrent_cell == 0:
                    currrent_cell = ""
                text_ID = self.canvas.create_text(
                    current_rect[0], current_rect[1], text=currrent_cell)
                self.text_IDs.append(text_ID)

    def initUI(self, matrix):
        self.master = tk.Tk()
        self.master.title("2048")
        self.SIDE = 100
        self.canvas.pack()

        self.rects = []
        for i in range(4):
            row = []
            for j in range(4):
                self.canvas.create_rectangle(
                    self.SIDE * j, 0, self.SIDE * (j + 1), self.SIDE * (i + 1))
                row.append([self.SIDE * i + 50, self.SIDE * j + 50])
            self.rects.append(row)

        self.layout_matrix(matrix)

    def callback(self, event):
        if event.char == "w":
            self.grid.slide("up")
        elif event.char == "a":
            self.grid.slide("left")
        elif event.char == "s":
            self.grid.slide("down")
        elif event.char == "d":
            self.grid.slide("right")

if __name__ == "__main__":
    grid_view = GridView()

    matrix = [[0, 0, 0, 0],
              [0, 0, 2, 2],
              [0, 0, 0, 0],
              [4, 0, 0, 0]]

    grid_view.initUI(matrix)
    grid_view.mainloop()<commit_msg>Refactor view methods<commit_after>import Tkinter as tk


class GridView(tk.Frame):

    def __init__(self, grid, master=None):
        tk.Frame.__init__(self, master)
        self.grid = grid
        
        # Bind keys.
        for key in "<w>", "<a>", "<s>", "<d>":
            self.master.bind(key, self.callback)

        self.text_IDs = []
        self.SIDE = 100
        self.canvas = tk.Canvas(
            master, width=self.SIDE * 4, height=self.SIDE * 4)
        self.canvas.pack()

    def layout_matrix(self, matrix):
        '''Layout the internal representation of
         the grid to grid view.'''
        for ID in self.text_IDs:
            self.canvas.delete(ID)
        self.text_IDs = []
        for i in range(4):
            for j in range(4):
                current_rect = self.rects[i][j]
                currrent_cell = matrix[j][i]
                if currrent_cell == 0:
                    currrent_cell = ""
                text_ID = self.canvas.create_text(
                    current_rect[0], current_rect[1], text=currrent_cell)
                self.text_IDs.append(text_ID)

    def initUI(self, matrix):
        '''Handle initial drawing of the grid view.'''
        self.master = tk.Tk()
        self.master.title("2048")
        self.SIDE = 100
        self.canvas.pack()

        self.rects = []
        for row_index in range(4):
            row = []
            for cell_index in range(4):
                self.canvas.create_rectangle(self.SIDE * cell_index, 
                                            0, 
                                            self.SIDE * (cell_index + 1),
                                            self.SIDE * (row_index + 1))
                row.append([self.SIDE * row_index + 50, self.SIDE * cell_index + 50])
            self.rects.append(row)

        self.layout_matrix(matrix)

    def callback(self, event):
        '''Callback the grid object with respect 
        to the key pressed'''
        if event.char == "w":
            self.grid.slide("up")
        elif event.char == "a":
            self.grid.slide("left")
        elif event.char == "s":
            self.grid.slide("down")
        elif event.char == "d":
            self.grid.slide("right")
{'fraction_non_alphanumeric': 0.1301558203483043, 'fraction_numerical': 0.015582034830430797, 'mean_word_length': 1.5100632547441057, 'pattern_counts': {'":': 10, '<': 11, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 6, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '149412', 'n_tokens_mistral': 1391, 'n_tokens_neox': 1303, 'n_words': 294}