<commit_before>from __future__ import print_function
from copy import deepcopy
from copy import copy
from random import randint
from random import choice
from datetime import time, timedelta
from structures import *
from constraint import *
import time as now

#import interface # uncomment to use export_schedule_xml 
import xml.etree.ElementTree as ET
import os.path


class SchedulerInitError(Exception):
    def __init__(self, value):
        self.value = value

    def __str__(self):
        return repr(self.value)


class BreedError(Exception):
    def __init__(self, value):
        self.value = value

    def __str__(self):
        return repr(self.value)


class FilterError(Exception):
    def __init__(self, value):
        self.value = value

    def __str__(self):
        return repr(self.value)


class MalformedTimeslotError(Exception):
    def __init__(self, value):
        self.value = value

    def __str__(self):
        return repr(self.value)



class Scheduler:

    """Schedules all courses for a week"""

    def __init__(self, courses, rooms, time_slots_mwf, time_slots_tr, time_slot_divide, test = False):
        if type(courses) == list:
            if len(courses) != 0:
                if not all(isinstance(each_course, Course) for \
                        each_course in courses):
                   raise SchedulerInitError("Courses - Not all of type Course") 
            else:
                raise SchedulerInitError("Courses - List has no elements")
        else:
            raise SchedulerInitError("Courses - Not a list")

        if type(rooms) == list:
            if len(rooms) != 0:
                if not all(isinstance(each_room, tuple) for each_room in rooms):
                    raise SchedulerInitError("Rooms - Not all tuple type")
                else:
                    for each_room in rooms:
                        if not all(isinstance(part, str) for part in each_room):
                            raise SchedulerInitError("Rooms - Not all parts are str type")
            else:
                 raise SchedulerInitError("Rooms - List has no elements")
        else:
            raise SchedulerInitError("Rooms - Not a list")

        if type(time_slots_mwf) == list:
            if len(time_slots_mwf) != 0:
                if not all(isinstance(each_slot, str) for \
                        each_slot in time_slots_mwf):
                    raise SchedulerInitError("Time Slot MWF - \
                            Not all string type")
            else:
                 raise SchedulerInitError("Time Slot MWF -\
                         List has no elements")
        else:
            raise SchedulerInitError("Time Slot MWF - Not a list")

        if type(time_slots_tr) == list:
            if len(time_slots_tr) != 0:
                if not all(isinstance(each_slot, str) for \
                        each_slot in time_slots_tr):
                    raise SchedulerInitError("Time Slot TR - \
                            Not all string type")
            else:
                 raise SchedulerInitError("Time Slot TR -\
                         List has no elements")
        else:
            raise SchedulerInitError("Time Slot TR - Not a list")

        self.time_slots_mwf = time_slots_mwf
        self.time_slots_tr = time_slots_tr
        self.time_slots = time_slots_mwf + time_slots_tr
        if type(time_slot_divide) == int:
            if time_slot_divide < 0 and \
                time_slot_divide >= min(len(self.time_slots_mwf), 
                                        len(self.time_slots_tr)):
                raise SchedulerInitError("Time Slot Divide - Not valid number")
        else:
            raise SchedulerInitError("Time Slot Divide - Not valid number")

        self.slot_divide = time_slot_divide
        self.courses = courses
        self.rooms = rooms
        self.weeks = []

        self.constraints = []
        self.max_fitness = 0

        #Courses separated by credit hours
        self.separated = self.separate_by_credit(self.courses)

    def separate_by_credit(self, courses_list):
        """Groups the courses based on number of credit hours.
        IN: list of course objects
        OUT: dictionary with keys=credit hours and values=list of course objects"""
        courses_by_credits = {}
        for each_course in courses_list:
            # Case 1: New key-value pair (make new list)
            if each_course.credit not in courses_by_credits.keys():
                courses_by_credits[each_course.credit] = [each_course]
            # Case 2: Add to value's list for key
            else:
                courses_by_credits[each_course.credit].append(each_course)
        return courses_by_credits

    def add_constraint(self, name, weight, func, *args):
        """Adds an constraint to the schedule"""
        exists = False
        for constraint in globs.mainScheduler.constraints:
            if constraint.name == name:
                exists = True
        if not exists:
            self.constraints.append(Constraint(name, weight, func, *args))
            self.max_fitness += weight

    def clear_constraints(self):
        """Removes all constraints from list"""
        self.constraints = []
        self.max_fitness = 0
        
    def delete_list_constraints(self, items):
        """Removes some constraints from list"""<commit_msg>Fix call to globs.mainScheduler inside Scheduler class<commit_after>from __future__ import print_function
from copy import deepcopy
from copy import copy
from random import randint
from random import choice
from datetime import time, timedelta
from structures import *
from constraint import *
import time as now

#import interface # uncomment to use export_schedule_xml 
import xml.etree.ElementTree as ET
import os.path


class SchedulerInitError(Exception):
    def __init__(self, value):
        self.value = value

    def __str__(self):
        return repr(self.value)


class BreedError(Exception):
    def __init__(self, value):
        self.value = value

    def __str__(self):
        return repr(self.value)


class FilterError(Exception):
    def __init__(self, value):
        self.value = value

    def __str__(self):
        return repr(self.value)


class MalformedTimeslotError(Exception):
    def __init__(self, value):
        self.value = value

    def __str__(self):
        return repr(self.value)



class Scheduler:

    """Schedules all courses for a week"""

    def __init__(self, courses, rooms, time_slots_mwf, time_slots_tr, time_slot_divide, test = False):
        if type(courses) == list:
            if len(courses) != 0:
                if not all(isinstance(each_course, Course) for \
                        each_course in courses):
                   raise SchedulerInitError("Courses - Not all of type Course") 
            else:
                raise SchedulerInitError("Courses - List has no elements")
        else:
            raise SchedulerInitError("Courses - Not a list")

        if type(rooms) == list:
            if len(rooms) != 0:
                if not all(isinstance(each_room, tuple) for each_room in rooms):
                    raise SchedulerInitError("Rooms - Not all tuple type")
                else:
                    for each_room in rooms:
                        if not all(isinstance(part, str) for part in each_room):
                            raise SchedulerInitError("Rooms - Not all parts are str type")
            else:
                 raise SchedulerInitError("Rooms - List has no elements")
        else:
            raise SchedulerInitError("Rooms - Not a list")

        if type(time_slots_mwf) == list:
            if len(time_slots_mwf) != 0:
                if not all(isinstance(each_slot, str) for \
                        each_slot in time_slots_mwf):
                    raise SchedulerInitError("Time Slot MWF - \
                            Not all string type")
            else:
                 raise SchedulerInitError("Time Slot MWF -\
                         List has no elements")
        else:
            raise SchedulerInitError("Time Slot MWF - Not a list")

        if type(time_slots_tr) == list:
            if len(time_slots_tr) != 0:
                if not all(isinstance(each_slot, str) for \
                        each_slot in time_slots_tr):
                    raise SchedulerInitError("Time Slot TR - \
                            Not all string type")
            else:
                 raise SchedulerInitError("Time Slot TR -\
                         List has no elements")
        else:
            raise SchedulerInitError("Time Slot TR - Not a list")

        self.time_slots_mwf = time_slots_mwf
        self.time_slots_tr = time_slots_tr
        self.time_slots = time_slots_mwf + time_slots_tr
        if type(time_slot_divide) == int:
            if time_slot_divide < 0 and \
                time_slot_divide >= min(len(self.time_slots_mwf), 
                                        len(self.time_slots_tr)):
                raise SchedulerInitError("Time Slot Divide - Not valid number")
        else:
            raise SchedulerInitError("Time Slot Divide - Not valid number")

        self.slot_divide = time_slot_divide
        self.courses = courses
        self.rooms = rooms
        self.weeks = []

        self.constraints = []
        self.max_fitness = 0

        #Courses separated by credit hours
        self.separated = self.separate_by_credit(self.courses)

    def separate_by_credit(self, courses_list):
        """Groups the courses based on number of credit hours.
        IN: list of course objects
        OUT: dictionary with keys=credit hours and values=list of course objects"""
        courses_by_credits = {}
        for each_course in courses_list:
            # Case 1: New key-value pair (make new list)
            if each_course.credit not in courses_by_credits.keys():
                courses_by_credits[each_course.credit] = [each_course]
            # Case 2: Add to value's list for key
            else:
                courses_by_credits[each_course.credit].append(each_course)
        return courses_by_credits

    def add_constraint(self, name, weight, func, *args):
        """Adds an constraint to the schedule"""
        exists = False
        for constraint in self.constraints:
            if constraint.name == name:
                exists = True
        if not exists:
            self.constraints.append(Constraint(name, weight, func, *args))
            self.max_fitness += weight

    def clear_constraints(self):
        """Removes all constraints from list"""
        self.constraints = []
        self.max_fitness = 0
        
    def delete_list_constraints(self, items):
        """Removes some constraints from list"""
{'fraction_non_alphanumeric': 0.07548219584569733, 'fraction_numerical': 0.0016691394658753709, 'mean_word_length': 1.9140772764117806, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 4}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 6, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6051556', 'n_tokens_mistral': 3006, 'n_tokens_neox': 2855, 'n_words': 869}