<commit_before>#! /usr/bin/env python3
#
# based on life64.ck monome app by tehn

import asyncio
import monome
import copy, itertools, random

class Life(monome.Monome):
    def __init__(self):
        super().__init__('/life')
        self.alive = True

    def ready(self):
        self.world = [[0 for col in range(self.width)] for row in range(self.height)]
        self.task = asyncio.async(self.begin())

    def disconnect(self):
        super().disconnect()
        self.task.cancel()
        self.led_all(0)

    def grid_key(self, x, y, s):
        if x == self.width - 1 and y == 0 and s == 1:
            self.alive = not self.alive
            self.led_set(self.width - 1, 0, int(not self.alive))
            return
        if s == 1:
            row, col = y, x
            self.world[row][col] ^= 1
            self.led_set(x, y, self.world[row][col])
        else:
            pass

    async def begin(self):
        while True:
            if self.alive:
                self.update()
                for i, row in enumerate(self.world):
                    self.led_row(0, i, row)
            await asyncio.sleep(0.2)

    def update(self):
        def neighbors(x, y):
            result = 0
            for dx, dy in [d for d in itertools.product([-1, 0, 1], repeat=2) if d != (0, 0)]:
                row = (y + dy) % self.height
                col = (x + dx) % self.width
                result += self.world[row][col]
            return result

        new_world = copy.deepcopy(self.world)
        for x in range(self.width):
            for y in range(self.height):
                n = neighbors(x, y)
                row, col = y, x
                if n == 3:
                    new_world[row][col] = 1
                elif (n < 2 or n > 3):
                    new_world[row][col] = 0
        self.world = copy.deepcopy(new_world)

    def randomize(self):
        for row in range(self.height):
            for col in range(self.width):
                self.world[row][col] = random.randint(0, 1)

if __name__ == '__main__':
    loop = asyncio.get_event_loop()
    asyncio.async(monome.create_serialosc_connection(Life), loop=loop)
    loop.run_forever()<commit_msg>update life example<commit_after>#! /usr/bin/env python3
#
# based on life64.ck monome app by tehn

import asyncio
import monome
import copy, itertools, random

class Life(monome.App):
    def __init__(self):
        super().__init__() # TODO: prefix
        self.alive = True

    def on_grid_ready(self):
        self.world = [[0 for col in range(self.grid.width)] for row in range(self.grid.height)]
        self.randomize()
        self.task = asyncio.async(self.begin())

    def on_grid_disconnect(self):
        self.task.cancel()

    def on_grid_key(self, x, y, s):
        if x == self.grid.width - 1 and y == 0 and s == 1:
            self.alive = not self.alive
            self.grid.led_set(self.grid.width - 1, 0, int(not self.alive))
            return
        if s == 1:
            row, col = y, x
            self.world[row][col] ^= 1
            self.grid.led_set(x, y, self.world[row][col])

    def quit(self):
        self.task.cancel()

    async def begin(self):
        try:
            while True:
                if self.alive:
                    self.update()
                    for i, row in enumerate(self.world):
                        self.grid.led_row(0, i, row)
                await asyncio.sleep(0.2)
        except asyncio.CancelledError:
            pass

    def update(self):
        def neighbors(x, y):
            result = 0
            for dx, dy in [d for d in itertools.product([-1, 0, 1], repeat=2) if d != (0, 0)]:
                row = (y + dy) % self.grid.height
                col = (x + dx) % self.grid.width
                result += self.world[row][col]
            return result

        new_world = copy.deepcopy(self.world)
        for x in range(self.grid.width):
            for y in range(self.grid.height):
                n = neighbors(x, y)
                row, col = y, x
                if n == 3:
                    new_world[row][col] = 1
                elif (n < 2 or n > 3):
                    new_world[row][col] = 0
        self.world = copy.deepcopy(new_world)

    def randomize(self):
        for row in range(self.grid.height):
            for col in range(self.grid.width):
                self.world[row][col] = random.randint(0, 1)

if __name__ == '__main__':
    loop = asyncio.get_event_loop()
    life_app = Life()
    asyncio.async(monome.SerialOsc.create(loop=loop, autoconnect_app=life_app))

    try:
        loop.run_forever()
    except KeyboardInterrupt:
        life_app.quit()
{'fraction_non_alphanumeric': 0.12248394004282655, 'fraction_numerical': 0.012205567451820129, 'mean_word_length': 1.6256323777403034, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, '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': '571907', 'n_tokens_mistral': 1545, 'n_tokens_neox': 1466, 'n_words': 389}