<commit_before>from enum import Enum
from square import Square
from coordinate import Coordinate

class Direction(Enum):
    top = 1
    topRight = 2
    right = 3
    btmRight = 4
    btm = 5
    btmLeft = 6
    left = 7
    topLeft = 8

class Gameboard:
    rows = [[Coordinate.a8, Coordinate.b8, Coordinate.c8, Coordinate.d8, Coordinate.e8, Coordinate.f8, Coordinate.g8, Coordinate.h8],
            [Coordinate.a7, Coordinate.b7, Coordinate.c7, Coordinate.d7, Coordinate.e7, Coordinate.f7, Coordinate.g7, Coordinate.h7],
            [Coordinate.a6, Coordinate.b6, Coordinate.c6, Coordinate.d6, Coordinate.e6, Coordinate.f6, Coordinate.g6, Coordinate.h6],
            [Coordinate.a5, Coordinate.b5, Coordinate.c5, Coordinate.d5, Coordinate.e5, Coordinate.f5, Coordinate.g5, Coordinate.h5],
            [Coordinate.a4, Coordinate.b4, Coordinate.c4, Coordinate.d4, Coordinate.e4, Coordinate.f4, Coordinate.g4, Coordinate.h4],
            [Coordinate.a3, Coordinate.b3, Coordinate.c3, Coordinate.d3, Coordinate.e3, Coordinate.f3, Coordinate.g3, Coordinate.h3],
            [Coordinate.a2, Coordinate.b2, Coordinate.c2, Coordinate.d2, Coordinate.e2, Coordinate.f2, Coordinate.g2, Coordinate.h2],
            [Coordinate.a1, Coordinate.b1, Coordinate.c1, Coordinate.d1, Coordinate.e1, Coordinate.f1, Coordinate.g1, Coordinate.h1]]
    
    def __init__(self):
        self._squares = {}
        for k in Coordinate:
            self._squares[k] = None

    def _indexOf(self, square):
        """Returns a tuple with the index of given square in self.rows

        Args:
            square (Coordinate): the square to find index for
        Returns:
            tuple: it has the form (x,y). The square can be found by accessing
            self.rows[x][y]
        Raises:
            TypeError: if square is not of type Coordinate

        """
        if not isinstance(square, Coordinate):
            raise TypeError("square variable must be from Coordinate enum")
        for i in range(len(self.rows)):
            if square in self.rows[i]:
                return (i,self.rows[i].index(square))

    def neighbors(self, square):
        """Returns a dictionary dict[Direction]=Coordinate.

        Args:
            square (Coordinate): the square to get neighbors from
        Returns:
            dict: keys are Direction values; values are Coordinate elements 
            of the corresponding neighbor, None if neighbor doesn't exist
        Raises:
            TypeError: if square is not of type Coordinate
        """
        if not isinstance(square, Coordinate):
            raise TypeError("square variable must be from Coordinate enum")
        number = 1
        letter = 8
        top = None
        tr = None
        right = None
        br = None
        btm = None
        bl = None
        left = None
        tl = None
        try:
            newCoord = square + number
            if (newCoord // letter) == (square // letter):
                top = Coordinate(newCoord)
            else:
                pass # adding a square puts us in the next letter column
        except ValueError:
            pass # index is out of bounds, so neighbor stays None
        try:
            newCoord = square + letter + number
            if (newCoord // letter) == (square // letter + 1):
                tr  = Coordinate(newCoord)
        except ValueError:
            pass
        try:
            right  = Coordinate(square + letter)
        except ValueError:
            pass
        try:
            newCoord = square + letter - number
            if (newCoord // letter) == (square // letter + 1):
                br  = Coordinate(newCoord)
            else:
                pass # We are in the same column
        except ValueError:
            pass
        try:
            newCoord = square - number
            if (newCoord // letter) == (square // letter):
                btm  = Coordinate(newCoord)
        except ValueError:
            pass
        try:
            newCoord = square - letter - number
            if (newCoord // letter) == (square // letter - 1):
                bl  = Coordinate(newCoord)
        except ValueError:
            pass
        try:
            left  = Coordinate(square - letter)
        except ValueError:
            pass
        try:
            newCoord = square - letter + number
            if (newCoord // letter) == (square // letter - 1):
                tl  = Coordinate(newCoord)
        except ValueError:
            pass
        neighbors = {Direction.top: top,
                    Direction.topRight: tr,
                    Direction.right: right,
                    Direction.btmRight: br,
                    Direction.btm: btm,
                    Direction.btmLeft: bl,
                    Direction.left: left,
                    Direction.topLeft: tl}
        return neighbors

    def rowForSquare(self, square):
        """Returns a list representing the horizontal line where the square given belongs.

        Args:
            square (Coordinate): the square in the row to find
        Returns:
            list: elements are Coordinate elements in the row, including 'square'<commit_msg>Refactored code to avoid try/except<commit_after>from enum import Enum
from square import Square
from coordinate import Coordinate

class Direction(Enum):
    top = 1
    topRight = 2
    right = 3
    btmRight = 4
    btm = 5
    btmLeft = 6
    left = 7
    topLeft = 8

class Gameboard:
    rows = [[Coordinate.a8, Coordinate.b8, Coordinate.c8, Coordinate.d8, Coordinate.e8, Coordinate.f8, Coordinate.g8, Coordinate.h8],
            [Coordinate.a7, Coordinate.b7, Coordinate.c7, Coordinate.d7, Coordinate.e7, Coordinate.f7, Coordinate.g7, Coordinate.h7],
            [Coordinate.a6, Coordinate.b6, Coordinate.c6, Coordinate.d6, Coordinate.e6, Coordinate.f6, Coordinate.g6, Coordinate.h6],
            [Coordinate.a5, Coordinate.b5, Coordinate.c5, Coordinate.d5, Coordinate.e5, Coordinate.f5, Coordinate.g5, Coordinate.h5],
            [Coordinate.a4, Coordinate.b4, Coordinate.c4, Coordinate.d4, Coordinate.e4, Coordinate.f4, Coordinate.g4, Coordinate.h4],
            [Coordinate.a3, Coordinate.b3, Coordinate.c3, Coordinate.d3, Coordinate.e3, Coordinate.f3, Coordinate.g3, Coordinate.h3],
            [Coordinate.a2, Coordinate.b2, Coordinate.c2, Coordinate.d2, Coordinate.e2, Coordinate.f2, Coordinate.g2, Coordinate.h2],
            [Coordinate.a1, Coordinate.b1, Coordinate.c1, Coordinate.d1, Coordinate.e1, Coordinate.f1, Coordinate.g1, Coordinate.h1]]
    
    def __init__(self):
        self._squares = {}
        for k in Coordinate:
            self._squares[k] = None

    def _indexOf(self, square):
        """Returns a tuple with the index of given square in self.rows

        Args:
            square (Coordinate): the square to find index for
        Returns:
            tuple: it has the form (x,y). The square can be found by accessing
            self.rows[x][y]
        Raises:
            TypeError: if square is not of type Coordinate

        """
        if not isinstance(square, Coordinate):
            raise TypeError("square variable must be from Coordinate enum")
        for i in range(len(self.rows)):
            if square in self.rows[i]:
                return (i,self.rows[i].index(square))

    def neighbors(self, square):
        """Returns a dictionary dict[Direction]=Coordinate.

        Args:
            square (Coordinate): the square to get neighbors from
        Returns:
            dict: keys are Direction values; values are Coordinate elements 
            of the corresponding neighbor, None if neighbor doesn't exist
        Raises:
            TypeError: if square is not of type Coordinate
        """
        if not isinstance(square, Coordinate):
            raise TypeError("square variable must be from Coordinate enum")
        number = 1
        letter = 8
        top = None
        tr = None
        right = None
        br = None
        btm = None
        bl = None
        left = None
        tl = None

        newCoord = square + number
        if (newCoord // letter) == (square // letter):
            top = Coordinate(newCoord)

        newCoord = square + letter + number
        if (newCoord // letter) == (square // letter + 1) and newCoord < 64:
            tr  = Coordinate(newCoord)

        newCoord = square + letter
        if (newCoord // letter) == (square // letter + 1) and newCoord < 64:
            right  = Coordinate(newCoord)

        newCoord = square + letter - number
        if (newCoord // letter) == (square // letter + 1) and newCoord < 64:
            br  = Coordinate(newCoord)

        newCoord = square - number
        if (newCoord // letter) == (square // letter):
            btm  = Coordinate(newCoord)

        newCoord = square - letter - number
        if (newCoord // letter) == (square // letter - 1) and newCoord >= 0:
            bl  = Coordinate(newCoord)

        newCoord = square - letter
        if (newCoord // letter) == (square // letter - 1) and newCoord >= 0:
            left  = Coordinate(newCoord)

        newCoord = square - letter + number
        if (newCoord // letter) == (square // letter - 1) and newCoord >= 0:
            tl  = Coordinate(newCoord)

        neighbors = {Direction.top: top,
                    Direction.topRight: tr,
                    Direction.right: right,
                    Direction.btmRight: br,
                    Direction.btm: btm,
                    Direction.btmLeft: bl,
                    Direction.left: left,
                    Direction.topLeft: tl}
        return neighbors

    def rowForSquare(self, square):
        """Returns a list representing the horizontal line where the square given belongs.

        Args:
            square (Coordinate): the square in the row to find
        Returns:
            list: elements are Coordinate elements in the row, including 'square'
{'fraction_non_alphanumeric': 0.09154929577464789, 'fraction_numerical': 0.016800804828973843, 'mean_word_length': 1.853329506314581, 'pattern_counts': {'":': 0, '<': 6, '<?xml version=': 0, '>': 6, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5058646', 'n_tokens_mistral': 2856, 'n_tokens_neox': 2753, 'n_words': 858}