<commit_before>from __future__ import print_function
from PIL import Image, ImageDraw

class Table(object):
    def __init__(self, x, y, width, height):
        self.x = x
        self.y = y
        self.width = width
        self.height = height

class Renderer(object):

    def __init__(self, mode="text", data_path=None):
        self.mode = mode
        self.data_path = data_path

    def load_map(self, path):
        """
        Load a specific image to serve as the base map.
        """
        print("Loading map at %s" % (path))
        self.im = Image.open(path)
        self.draw = ImageDraw.Draw(self.im)
        print("Format: %s\nSize: %s\nMode: %s" %
              (self.im.format, self.im.size, self.im.mode))

    def load_data(self, path=None):
        """
        Load the restaurant data depending on the mode.
        """
        if self.mode == "text":
            return None
        elif self.mode == "db":
            return NotImplementedError

    def _draw_table(self, table):
        self.draw.line((0,0) + self.im.size, fill=128)
        self.draw.line((0, self.im.size[1], self.im.size[0], 0),
                       fill=128)
        return None

    def add_tables(self):
        t = Table(0, 0, 10, 10)
        self._draw_table(t)

    def save_map(self, path):
        self.im.save(path, "PNG")


def main():
    r = Renderer()
    r.load_map("../share/test_map.png")
    r.load_data("../share/data_table.txt")
    r.add_tables()
    r.save_map("../share/test_saved.png")



if __name__ == "__main__":
    main()<commit_msg>Add ability to create images from csv files<commit_after>from __future__ import print_function
import random
import csv
from PIL import Image, ImageDraw

class Table(object):
    def __init__(self, x, y, width, height):
        self.x = x
        self.y = y
        self.width = width
        self.height = height

    def get_bounding_points(self):
        x_0 = self.x - (self.width / 2)
        y_0 = self.y - (self.height / 2)
        x_1 = self.x + (self.width / 2)
        y_1 = self.y + (self.height / 2)
        return [x_0, y_0, x_1, y_1]

class Renderer(object):

    def __init__(self, mode="text", data_path=None):
        self.mode = mode
        self.data_path = data_path
        self.tables = list()
        self.colours = ["#F44336", # red
                        "#E91E63", # pink
                        "#9C27B0", # purple
                        "#673AB7", # deep purple
                        "#3F51B5", # indigo
                        "#2196F3", # blue
                        "#03A9F4", # light blue
                        "#00BCD4", # cyan
                        "#009688", # teal
                        "#4CAF50", # green
                        "#8BC34A", # light green
                        "#CDDC39", # lime
                        "#FFEB3B", # yellow
                        "#FFC107", # amber
                        "#FF9800", # orange
                        "#FF5722", # deep orange
                        "#795548", # brown
                        "#9E9E9E"] # grey

    def load_map(self, path):
        """
        Load a specific image to serve as the base map.
        """
        print("Loading map at %s" % (path))
        self.im = Image.open(path)
        self.draw = ImageDraw.Draw(self.im)
        print("Format: %s\nSize: %s\nMode: %s" %
              (self.im.format, self.im.size, self.im.mode))

    def load_data(self, path=None):
        """
        Load the restaurant data depending on the mode.
        """
        if self.mode == "text":
            self._load_text(path)
        elif self.mode == "db":
            return NotImplementedError

    def _load_text(self, path):
        with open(path, "rb") as file:
            reader = csv.reader(file, delimiter=",")
            for row in reader:
                t = Table(eval(row[0]),
                          eval(row[1]),
                          eval(row[2]),
                          eval(row[3]))
                self.tables.append(t)

    def _draw_table(self, table):
        rcolour = random.choice(self.colours)
        self.draw.rectangle(table.get_bounding_points(),
                            outline="#000000", fill=rcolour)
        return None

    def add_tables(self):
        """
        Add the tables to the map object.
        Returns True if at least one table has been added.
        """
        flag = False
        for t in self.tables:
            flag = True
            self._draw_table(t)
        return flag

    def save_map(self, path):
        self.im.save(path, "PNG")


def main():
    r = Renderer()
    r.load_map("../share/test_map.png")
    r.load_data("../share/data_table.txt")
    r.add_tables()
    r.save_map("../share/test_saved.png")



if __name__ == "__main__":
    main()
{'fraction_non_alphanumeric': 0.12786339025406082, 'fraction_numerical': 0.022490628904623073, 'mean_word_length': 1.5453100158982511, 'pattern_counts': {'":': 6, '<': 3, '<?xml version=': 0, '>': 3, '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': '6917184', 'n_tokens_mistral': 1557, 'n_tokens_neox': 1482, 'n_words': 361}