<commit_before>from bibliopixel.animation import BaseMatrixAnim
from bibliopixel import colors
import random


class LangtonsAnt(BaseMatrixAnim):

    def __init__(self, layout, antColor=colors.Green, pathColor=colors.Red):
        super(LangtonsAnt, self).__init__(layout)
        self.antColor = tuple(antColor)
        self.pathColor = tuple(pathColor)
        self.offColor = colors.Off
        self.curColor = self.offColor

    def pre_run(self):
        self.x = random.randrange(self.width)
        self.y = random.randrange(self.height)
        self.d = random.randrange(4)

    def __rollValue(self, val, step, min, max):
        val += step
        if val < min:
            diff = min - val
            val = max - diff + 1
        elif val > max:
            diff = val - max
            val = min + diff - 1
        return val

    def __changeDir(self, dir):
        if dir:
            dir = 1
        else:
            dir = -1
        self.d = self.__rollValue(self.d, dir, 0, 3)

    def __moveAnt(self):
        if self.d == 0:
            self.y = self.__rollValue(self.y, 1, 0, self.height - 1)
        elif self.d == 1:
            self.x = self.__rollValue(self.x, 1, 0, self.width - 1)
        elif self.d == 2:
            self.y = self.__rollValue(self.y, -1, 0, self.height - 1)
        elif self.d == 3:
            self.x = self.__rollValue(self.x, -1, 0, self.width - 1)

        self.curColor = self.layout.get(self.x, self.y)
        self.layout.set(self.x, self.y, self.antColor)

    def step(self, amt=1):
        if self.curColor == self.pathColor:
            self.layout.set(self.x, self.y, self.offColor)
            self.__changeDir(False)
            self.__moveAnt()
        else:
            self.layout.set(self.x, self.y, self.pathColor)
            self.__changeDir(True)
            self.__moveAnt()


class LangtonsAntRainbow(BaseMatrixAnim):

    def __init__(self, layout, antColor=colors.White):
        super(LangtonsAntRainbow, self).__init__(layout)
        self.color_cycle = [colors.Red, colors.Orange,
                            colors.Yellow, colors.Green,
                            colors.Blue, colors.Violet]
        self.antColor = tuple(antColor)
        self.offColor = colors.Off
        self.curColor = self.offColor
        self.curColorIndex = -1

    def pre_run(self):
        self.x = random.randrange(self.width)
        self.y = random.randrange(self.height)
        self.d = random.randrange(4)

    def __rollValue(self, val, step, min, max):
        val += step
        if val < min:
            diff = min - val
            val = max - diff + 1
        elif val > max:
            diff = val - max
            val = min + diff - 1
        return val

    def __changeDir(self, dir):
        if dir:
            dir = 1
        else:
            dir = -1
        self.d = self.__rollValue(self.d, dir, 0, 3)

    def __moveAnt(self):
        if self.d == 0:
            self.y = self.__rollValue(self.y, 1, 0, self.height - 1)
        elif self.d == 1:
            self.x = self.__rollValue(self.x, 1, 0, self.width - 1)
        elif self.d == 2:
            self.y = self.__rollValue(self.y, -1, 0, self.height - 1)
        elif self.d == 3:
            self.x = self.__rollValue(self.x, -1, 0, self.width - 1)

        self.curColor = self.layout.get(self.x, self.y)
        if self.curColor == self.offColor:
            i = 0
        else:
            i = self.color_cycle.index(self.curColor)
            i += 1
            i %= len(self.color_cycle)

        self.curColor = self.color_cycle[i]
        self.curColorIndex = i
        self.layout.set(self.x, self.y, self.antColor)

    def step(self, amt=1):
        self.layout.set(self.x, self.y, self.curColor)
        if self.curColorIndex % 2 == 0:
            self.__changeDir(False)
            self.__moveAnt()
        else:
            self.__changeDir(True)
            self.__moveAnt()<commit_msg>Use inheritance with the LangstonsAnts<commit_after>from bibliopixel.animation import BaseMatrixAnim
from bibliopixel import colors
import random


class LangtonsAntBase(BaseMatrixAnim):

    def __init__(self, layout, antColor=colors.Green, pathColor=colors.Red):
        super().__init__(layout)
        self.antColor = tuple(antColor)
        self.pathColor = tuple(pathColor)
        self.offColor = colors.Off
        self.curColor = self.offColor

    def pre_run(self):
        self.x = random.randrange(self.width)
        self.y = random.randrange(self.height)
        self.d = random.randrange(4)

    def _rollValue(self, val, step, min, max):
        val += step
        if val < min:
            diff = min - val
            val = max - diff + 1
        elif val > max:
            diff = val - max
            val = min + diff - 1
        return val

    def _changeDir(self, dir):
        if dir:
            dir = 1
        else:
            dir = -1
        self.d = self._rollValue(self.d, dir, 0, 3)

    def _moveAnt(self):
        if self.d == 0:
            self.y = self._rollValue(self.y, 1, 0, self.height - 1)
        elif self.d == 1:
            self.x = self._rollValue(self.x, 1, 0, self.width - 1)
        elif self.d == 2:
            self.y = self._rollValue(self.y, -1, 0, self.height - 1)
        elif self.d == 3:
            self.x = self._rollValue(self.x, -1, 0, self.width - 1)

        self.curColor = self.layout.get(self.x, self.y)
        self._postMove()


class LangtonsAnt(LangtonsAntBase):
    def _postMove(self):
        self.layout.set(self.x, self.y, self.antColor)

    def step(self, amt=1):
        if self.curColor == self.pathColor:
            self.layout.set(self.x, self.y, self.offColor)
            self._changeDir(False)
            self._moveAnt()
        else:
            self.layout.set(self.x, self.y, self.pathColor)
            self._changeDir(True)
            self._moveAnt()


class LangtonsAntRainbow(LangtonsAntBase):
    DEFAULT_COLORS = [colors.Red, colors.Orange, colors.Yellow,
                      colors.Green, colors.Blue, colors.Violet]

    def __init__(self, layout, antColor=colors.White, colors=None):
        super().__init__(layout, antColor)
        self.colors = colors or self.DEFAULT_COLORS
        self.curColorIndex = -1

    def _postMove(self):
        if self.curColor == self.offColor:
            i = 0
        else:
            i = self.colors.index(self.curColor)
            i += 1
            i %= len(self.colors)

        self.curColor = self.colors[i]
        self.curColorIndex = i
        self.layout.set(self.x, self.y, self.antColor)

    def step(self, amt=1):
        self.layout.set(self.x, self.y, self.curColor)
        if self.curColorIndex % 2 == 0:
            self._changeDir(False)
            self._moveAnt()
        else:
            self._changeDir(True)
            self._moveAnt()
{'fraction_non_alphanumeric': 0.11795620437956204, 'fraction_numerical': 0.012116788321167884, 'mean_word_length': 1.731658692185008, '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': '3095538', 'n_tokens_mistral': 2270, 'n_tokens_neox': 2168, 'n_words': 498}