<commit_before>import uuid

from Blit import Color

from datetime import datetime


class Pixel(Color):

    RED_INDEX = 0
    GREEN_INDEX = 1
    BLUE_INDEX = 2
    ALPHA_INDEX = 3

    def __init__(self, r=0, g=0, b=0, a=255):
        super(Pixel, self).__init__(r, g, b, a)

        self.dirty = True

    def set_color(self, r, g, b, a=255):

        incoming = r / 255., g / 255., b / 255., a / 255.

        for idx, element in enumerate(incoming):
            if self._components[idx] != element:
                self.dirty = True

        self._components = incoming

    def set_color_n(self, r, g, b, a=1):

        incoming = r, g, b, a

        for idx, element in enumerate(incoming):
            if self._components[idx] != element:
                self.dirty = True

        self._components = incoming

    @property
    def r(self):
        return self._components[Pixel.RED_INDEX] * 255.

    @property
    def g(self):
        return self._components[Pixel.GREEN_INDEX] * 255.

    @property
    def b(self):
        return self._components[Pixel.BLUE_INDEX] * 255.

    @property
    def a(self):
        return self._components[Pixel.ALPHA_INDEX] * 255.

    @property
    def w(self):
        return (self.r + self.g + self.b) / 255

    @property
    def r_n(self):
        return self._components[Pixel.RED_INDEX]

    @property
    def g_n(self):
        return self._components[Pixel.GREEN_INDEX]

    @property
    def b_n(self):
        return self._components[Pixel.BLUE_INDEX]

    @property
    def a_n(self):
        return self._components[Pixel.ALPHA_INDEX]

    @property
    def w_n(self):
        return (self.r_n + self.g_n + self.b_n) / 3.

    def copy(self):
        return Pixel(self.r, self.g, self.b, self.a)

    def blend(self, other, mask=None, opacity=1, blendfunc=None):
        layer = super(Pixel, self).blend(other, mask, opacity, blendfunc)
        self.__set_color_from_layer(layer)

    def adjust(self, adjustfunc):
        layer = super(Pixel, self).adjust(adjustfunc)
        self.__set_color_from_layer(layer)

    def __set_color_from_layer(self, layer):
        r, g, b, a = layer.rgba(1, 1)

        self.set_color( self.__transform_component(r),
                        self.__transform_component(g),
                        self.__transform_component(b),
                        self.__transform_component(a))

    def __transform_component(self, packed_value):
        return packed_value[0][0] * 255

    def __component(self, value):
        return str(value).ljust(5) + " "

    def __repr__(self):
        return self.__component(self.r) + self.__component(self.g) + self.__component(self.b) + self.__component(self.a)

    def __str__(self):
        return self.__repr__()


class SpriteContainer(object):
    def __init__(self):
        self.sprites = [ ]

    def clear_sprites(self):
        self.sprites = [ ]

    def add_sprite(self, sprite):
        self.sprites.append(sprite)

    def get_sprites(self):
        return self.sprites[:]


class World(SpriteContainer):
    def __init__(self, pixel_count):
        super(World, self).__init__()
        self.pixels = [ Pixel() for i in range(pixel_count) ]
        self.state = { }

        self.run_enable = True

        self.timing_previous_frame = datetime.utcnow()
        self.timing_lag = 0.0
        self.timing_ms_per_update = 33.33

    def update(self):
        for sprite in self.sprites:
            sprite.update_from(self)

    def render(self):
        for sprite in self.sprites:
            sprite.render_to(self.pixels)

    def run(self, world_frame_callback=None):
        lag = 0.0
        while self.run_enable:
            timing_current = datetime.utcnow()
            timing_elapsed = (timing_current - self.timing_previous_frame).microseconds / 1000

            self.timing_previous_frame = timing_current
            lag += timing_elapsed

            while lag > self.timing_ms_per_update:
                self.update()
                lag -= self.timing_ms_per_update

            if world_frame_callback:
                world_frame_callback(self)

            self.render()


class Sprite(SpriteContainer):
    def __init__(self, position=0):<commit_msg>BUG: ensure at least one update happens before rendering (some sprites may use update to get to a known state)<commit_after>import uuid

from Blit import Color

from datetime import datetime


class Pixel(Color):

    RED_INDEX = 0
    GREEN_INDEX = 1
    BLUE_INDEX = 2
    ALPHA_INDEX = 3

    def __init__(self, r=0, g=0, b=0, a=255):
        super(Pixel, self).__init__(r, g, b, a)

        self.dirty = True

    def set_color(self, r, g, b, a=255):

        incoming = r / 255., g / 255., b / 255., a / 255.

        for idx, element in enumerate(incoming):
            if self._components[idx] != element:
                self.dirty = True

        self._components = incoming

    def set_color_n(self, r, g, b, a=1):

        incoming = r, g, b, a

        for idx, element in enumerate(incoming):
            if self._components[idx] != element:
                self.dirty = True

        self._components = incoming

    @property
    def r(self):
        return self._components[Pixel.RED_INDEX] * 255.

    @property
    def g(self):
        return self._components[Pixel.GREEN_INDEX] * 255.

    @property
    def b(self):
        return self._components[Pixel.BLUE_INDEX] * 255.

    @property
    def a(self):
        return self._components[Pixel.ALPHA_INDEX] * 255.

    @property
    def w(self):
        return (self.r + self.g + self.b) / 255

    @property
    def r_n(self):
        return self._components[Pixel.RED_INDEX]

    @property
    def g_n(self):
        return self._components[Pixel.GREEN_INDEX]

    @property
    def b_n(self):
        return self._components[Pixel.BLUE_INDEX]

    @property
    def a_n(self):
        return self._components[Pixel.ALPHA_INDEX]

    @property
    def w_n(self):
        return (self.r_n + self.g_n + self.b_n) / 3.

    def copy(self):
        return Pixel(self.r, self.g, self.b, self.a)

    def blend(self, other, mask=None, opacity=1, blendfunc=None):
        layer = super(Pixel, self).blend(other, mask, opacity, blendfunc)
        self.__set_color_from_layer(layer)

    def adjust(self, adjustfunc):
        layer = super(Pixel, self).adjust(adjustfunc)
        self.__set_color_from_layer(layer)

    def __set_color_from_layer(self, layer):
        r, g, b, a = layer.rgba(1, 1)

        self.set_color( self.__transform_component(r),
                        self.__transform_component(g),
                        self.__transform_component(b),
                        self.__transform_component(a))

    def __transform_component(self, packed_value):
        return packed_value[0][0] * 255

    def __component(self, value):
        return str(value).ljust(5) + " "

    def __repr__(self):
        return self.__component(self.r) + self.__component(self.g) + self.__component(self.b) + self.__component(self.a)

    def __str__(self):
        return self.__repr__()


class SpriteContainer(object):
    def __init__(self):
        self.sprites = [ ]

    def clear_sprites(self):
        self.sprites = [ ]

    def add_sprite(self, sprite):
        self.sprites.append(sprite)

    def get_sprites(self):
        return self.sprites[:]


class World(SpriteContainer):
    def __init__(self, pixel_count):
        super(World, self).__init__()
        self.pixels = [ Pixel() for i in range(pixel_count) ]
        self.state = { }

        self.run_enable = True

        self.timing_previous_frame = datetime.utcnow()
        self.timing_lag = 0.0
        self.timing_ms_per_update = 33.33

    def update(self):
        for sprite in self.sprites:
            sprite.update_from(self)

    def render(self):
        for sprite in self.sprites:
            sprite.render_to(self.pixels)

    def run(self, world_frame_callback=None):
        lag = 0.0

        #do at least one update before rendering
        self.update()

        while self.run_enable:
            timing_current = datetime.utcnow()
            timing_elapsed = (timing_current - self.timing_previous_frame).microseconds / 1000

            self.timing_previous_frame = timing_current
            lag += timing_elapsed

            while lag > self.timing_ms_per_update:
                self.update()
                lag -= self.timing_ms_per_update

            if world_frame_callback:
                world_frame_callback(self)

            self.render()


class Sprite(SpriteContainer):
    def __init__(self, position=0):
{'fraction_non_alphanumeric': 0.105746859004188, 'fraction_numerical': 0.014890646812470917, 'mean_word_length': 2.206639313688922, 'pattern_counts': {'":': 0, '<': 3, '<?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': '2174493', 'n_tokens_mistral': 2873, 'n_tokens_neox': 2650, 'n_words': 606}