<commit_before>import os
import pygame


class World:
    """Represents the game world and provides access to the game objects within it. The world file should be in the
    following format:

    * Each line contains either a world attribute, an object definition or an instance of an object.
    * The order does not matter, but readability is encouraged
    * The fields of each line are separated by a semicolon and must be in the correct order
    * A line starting (logically ending) with a new line or carriage return character is ignored
    * A line starting with two forward slashes is ignored
    * A world attribute starts with # and has the fields:
            0 : Attribute name
            1 : Value
    * An object definition starts with @ and has the fields:
            0 : Image filename, without extension
            1 : File extension, should be compatible with pygame's image module
            2 : Object name (identifier)
            3 : Human readable description and other information
    * An object instance starts with any character but @ and # and has the fields:
            0 : Object name
            1 : x coordinate, in units of tile size
            2 : y coordinate, in units of tile size
            3 : z index, no unit. This is not meant to be a third coordinate, but simply a means to sort tiles, so they
                are correctly displayed on top of each other
    """

    def __init__(self):
        self.objectImageMapping = []
        self.objects = []
        self.objectDef = []
        self.attributes = {}

    def loadWorldFromFile(self, filename):
        with open(filename, 'r') as file:
            for line in file:
                if line[0] == "\n" or line[0] == "\r":
                    continue
                elif line[0] == '/' and line[1] == '/':
                    continue
                elif line[0] == '#':
                    attr = World.parseAttributeFromString(line)
                    self.attributes[attr.name] = attr.value
                elif line[0] == '@':
                    self.objectDef.append(World.parseObjectDefFromString(line))
                else:
                    self.objects.append(World.parseObjectFromString(line))
        self.objectImageMapping = self.initImageMapping()
        sorted(self.objects, key=lambda obj: obj.z)

    @staticmethod
    def parseAttributeFromString(strLine):
        values = strLine[1:].split(';')
        return Attribute(values[0], values[1].rstrip())

    @staticmethod
    def parseObjectDefFromString(strLine):
        values = strLine[1:].split(';')
        return Definition(values[2], values[0], values[1], values[3])

    @staticmethod
    def parseObjectFromString(strLine):
        values = strLine.split(';')
        return Tile(values[0], int(values[1]), int(values[2]), int(values[3]))

    def initImageMapping(self):
        mapping = {}
        for definition in self.objectDef:
            mapping[definition.name] = pygame.image.load(
                'images/' + definition.imageName + '.' + definition.fileExtension
            )
        return mapping


class Attribute:

    def __init__(self, name, value):
        self.name = name
        self.value = value


class Definition:

    def __init__(self, name, imageName, fileExtension, description):
        self.name = name
        self.imageName = imageName
        self.fileExtension = fileExtension
        self.description = description


class Tile:

    def __init__(self, name, x, y, z):
        self.name = name
        self.x = x
        self.y = y
        self.z = z<commit_msg>Changed collection of object definitions of world class to use dictionary instead of list, with object names as key.<commit_after>import os
import pygame


class World:
    """Represents the game world and provides access to the game objects within it. The world file should be in the
    following format:

    * Each line contains either a world attribute, an object definition or an instance of an object.
    * The order does not matter, but readability is encouraged
    * The fields of each line are separated by a semicolon and must be in the correct order
    * A line starting (logically ending) with a new line or carriage return character is ignored
    * A line starting with two forward slashes is ignored
    * A world attribute starts with # and has the fields:
            0 : Attribute name
            1 : Value
    * An object definition starts with @ and has the fields:
            0 : Image filename, without extension
            1 : File extension, should be compatible with pygame's image module
            2 : Object name (identifier)
            3 : Human readable description and other information
    * An object instance starts with any character but @ and # and has the fields:
            0 : Object name
            1 : x coordinate, in units of tile size
            2 : y coordinate, in units of tile size
            3 : z index, no unit. This is not meant to be a third coordinate, but simply a means to sort tiles, so they
                are correctly displayed on top of each other
    """

    def __init__(self):
        self.objectImageMapping = {}
        self.objects = []
        self.objectDef = {}
        self.attributes = {}

    def loadWorldFromFile(self, filename):
        with open(filename, 'r') as file:
            for line in file:
                if line[0] == "\n" or line[0] == "\r":
                    continue
                elif line[0] == '/' and line[1] == '/':
                    continue
                elif line[0] == '#':
                    attr = World.parseAttributeFromString(line)
                    self.attributes[attr.name] = attr.value
                elif line[0] == '@':
                    objDef = World.parseObjectDefFromString(line)
                    self.objectDef[objDef.name] = objDef
                else:
                    self.objects.append(World.parseObjectFromString(line))
        self.objectImageMapping = self.initImageMapping()
        sorted(self.objects, key=lambda obj: obj.z)

    @staticmethod
    def parseAttributeFromString(strLine):
        values = strLine[1:].split(';')
        return Attribute(values[0], values[1].rstrip())

    @staticmethod
    def parseObjectDefFromString(strLine):
        values = strLine[1:].split(';')
        return Definition(values[2], values[0], values[1], values[3])

    @staticmethod
    def parseObjectFromString(strLine):
        values = strLine.split(';')
        return Tile(values[0], int(values[1]), int(values[2]), int(values[3]))

    def initImageMapping(self):
        mapping = {}
        for definition in self.objectDef.values():
            mapping[definition.name] = pygame.image.load(
                'images/' + definition.imageName + '.' + definition.fileExtension
            )
        return mapping


class Attribute:

    def __init__(self, name, value):
        self.name = name
        self.value = value


class Definition:

    def __init__(self, name, imageName, fileExtension, description):
        self.name = name
        self.imageName = imageName
        self.fileExtension = fileExtension
        self.description = description


class Tile:

    def __init__(self, name, x, y, z):
        self.name = name
        self.x = x
        self.y = y
        self.z = z
{'fraction_non_alphanumeric': 0.08697427521437322, 'fraction_numerical': 0.00762215870423302, 'mean_word_length': 2.121495327102804, 'pattern_counts': {'":': 2, '<': 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': '1098505', 'n_tokens_mistral': 1896, 'n_tokens_neox': 1826, 'n_words': 694}