-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add function to bootstrap the first generation.
- Loading branch information
Showing
34 changed files
with
560 additions
and
390 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
# Generated by Django 5.1.1 on 2024-09-15 17:47 | ||
|
||
from django.db import migrations, models | ||
|
||
|
||
class Migration(migrations.Migration): | ||
|
||
dependencies = [ | ||
('api', '0005_remove_newvote_artist_remove_newvote_user_artist_and_more'), | ||
] | ||
|
||
operations = [ | ||
migrations.AlterField( | ||
model_name='artist', | ||
name='public_link', | ||
field=models.URLField(null=True), | ||
), | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
import random | ||
|
||
from api import art_storage | ||
from api import models | ||
from painter.graphics import engine | ||
from painter import painter as _painter | ||
|
||
|
||
_GRAPHICS_ENGINE = None | ||
|
||
def graphics_engine(): | ||
global _GRAPHICS_ENGINE | ||
if not _GRAPHICS_ENGINE: | ||
_GRAPHICS_ENGINE = engine.TurtleEngine() | ||
return _GRAPHICS_ENGINE | ||
|
||
|
||
# Hardcode a few special values because they're just used once. | ||
_NUM_ARTISTS = 64 | ||
_NUM_CHROMOSOMES = 32 | ||
_MIN_CHROMOSOME_LENGTH = 256 | ||
_MAX_CHROMOSOME_LENGTH = 512 | ||
|
||
|
||
def bootstrap(): | ||
"""Create a first generation of artists. | ||
""" | ||
gen = models.Generation.objects.create() | ||
|
||
for i in range(_NUM_ARTISTS): | ||
# Build the DNA string of chromosomes. | ||
chromosomes = [] | ||
for _ in range(_NUM_CHROMOSOMES): | ||
chromo_len = random.randint(_MIN_CHROMOSOME_LENGTH, _MAX_CHROMOSOME_LENGTH-1) | ||
chromo_str = ''.join(random.choices('ATCG', k=chromo_len)) | ||
chromosomes.append(chromo_str) | ||
|
||
dna = '\n'.join(chromosomes) | ||
artist = models.Artist.objects.create( | ||
dna=dna, | ||
generation=gen, | ||
) | ||
paint(artist, gen) | ||
|
||
|
||
def paint(artist, gen): | ||
"""Paints and saves this artist's masterpiece to the artist model. | ||
""" | ||
graphics_engine().reset() | ||
p = _painter.Painter(artist.dna, graphics_engine()) | ||
while p.still_growing(): | ||
p.paint() | ||
p.age_up() | ||
public_url = graphics_engine().save_image(art_storage.ArtStorage(), gen.id, artist.id) | ||
artist.public_url = public_url | ||
artist.save() |
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
import abc | ||
import io | ||
import tempfile | ||
import turtle | ||
|
||
from PIL import Image | ||
|
||
from painter.graphics import actions | ||
|
||
|
||
class EngineInterface(metaclass=abc.ABCMeta): | ||
|
||
def __init__(self, action_class): | ||
self.action_class = action_class | ||
|
||
def save_image(self, output_filename: str): | ||
pass | ||
|
||
def get_action(self, index): | ||
return self.action_class(index) | ||
|
||
def get_action_count(self): | ||
return len(self.action_class.__members__) | ||
|
||
|
||
class TurtleEngine(EngineInterface): | ||
|
||
def __new__(cls): | ||
"""Create or return the singleton instance of the ArtStorage.""" | ||
if not hasattr(cls, 'instance'): | ||
cls.instance = super(TurtleEngine, cls).__new__(cls) | ||
return cls.instance | ||
|
||
def __init__(self): | ||
EngineInterface.__init__(self, actions.TurtleAction) | ||
self.reset() | ||
|
||
def save_image(self, storage, generation: int, artist_id: int): | ||
"""Save a JPEG image to the art_storage. | ||
Args: | ||
- storage: The object responsible for storaging images. | ||
- generation: The generation number, used by `storage` to know where to save the image. | ||
- artist_id: The artist's numeric ID, used by `storage` to know where to save the image. | ||
Returns: | ||
- str: The public URL to the newly saved image file. | ||
""" | ||
canvas = turtle.getscreen().getcanvas() | ||
ps = canvas.postscript() | ||
image_file = storage.new_image_file(generation, artist_id) | ||
with Image.open(io.BytesIO(ps.encode('utf-8'))) as img: | ||
with storage.open(image_file) as fp: | ||
img.save(fp, format='JPEG') | ||
return image_file.public_url | ||
|
||
def reset(self): | ||
turtle.clearscreen() | ||
turtle.speed(10) | ||
turtle.hideturtle() | ||
turtle.getscreen().colormode(255) |
File renamed without changes.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
File renamed without changes.
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.