def __init__(self):
     self.recommender = Recommender()
     self.sofas = defaultdict(lambda: None)
     self.first_recommandation = defaultdict(lambda: None)
     self.final_recommandation = defaultdict(lambda: None)
     self.ctrl_thread = False
Exemple #2
0
 def __init__(self):
     self.recommender = Recommender()
     self.sofas = defaultdict(lambda: None)
     self.first_recommandation = defaultdict(lambda: None)
     self.final_recommandation = defaultdict(lambda: None)
     self.ctrl_thread = False
class SofaManager():
    """Class to manage the existing Sofas."""
    genres = Genre.__genres__  
    class Duplicate(Exception):
        """To be raised when a Sofa already exists."""
        pass

    def __init__(self):
        self.recommender = Recommender()
        self.sofas = defaultdict(lambda: None)
        self.first_recommandation = defaultdict(lambda: None)
        self.final_recommandation = defaultdict(lambda: None)
        self.ctrl_thread = False
        
    def __enter__(self):
        self.start_ctrl_thread()
        
    def __exit__(self, type, value, tb):
        self.stop_ctrl_thread()
        
    def start_ctrl_thread(self, step=300, max_age=600):
        self.ctrl_thread = True
        def ctrl_thread():
            if self.ctrl_thread:
                for sname, sofa in list(self.sofas.items()):
                    if sofa:
                        age = dt.datetime.now() - sofa.last_update
                        if age.seconds > max_age:
                            self.delete_sofa(sname)
                    else:
                        self.delete_sofa(sname)
                timer = threading.Timer(step, ctrl_thread)
                timer.start()
        ctrl_thread()
                
    def stop_ctrl_thread(self):
        self.ctrl_thread = False
        
    def new_sofa(self, sname):
        if self.sofas[sname]:
            raise self.Duplicate('The sofa %s already exists' % sname)
        else:
            self.sofas[sname] = Sofa(sname)
            
    def add_preferences(self, uname, pwd, sname, pref_list):
        for c, p in zip(self.genres, pref_list):
            self.sofas[sname].preferences[c].append(int(p))
        s = self.sofas[sname].people
        s.add((uname, pwd))
        self.sofas[sname].people = s 
        
    def get_nb_people(self, sname):
        return self.sofas[sname].nb_people
    
    def recommend(self, sname):
        sofa = self.sofas[sname]
        ratings = zip(*[sofa.preferences[c] for c in self.genres])
        films = [AppFilm(f[0], f[1], f[2])
                 for f in self.recommender.recommend(ratings, sofa.people)]
        return films
     
    def send_first_recommandation(self, sname):
        self.first_recommandation[sname] = films = self.recommend(sname)
        for client in self.sofas[sname].waiting_clients:
            posters = ';'.join([f.poster for f in films])
            ids = ';'.join([str(f.id) for f in films])
            client.write('::'.join([posters, ids]))
        self.sofas[sname].waiting_clients = []

    def add_films_rating(self, sname, rating_list):
        for f, r in zip(self.first_recommandation[sname], rating_list):
            f.add_rating(r)
        self.sofas[sname].last_update = dt.datetime.now()

    def get_final_recommandation(self, sname, client):
        nb_people = self.sofas[sname].nb_people
        self.sofas[sname].waiting_clients.append(client)
        if self.first_recommandation[sname][0].nb_rating >= nb_people:
            result = self.first_recommandation[sname][0]
            for f in self.first_recommandation[sname][1:]:
                if f.rating > result.rating:
                    result = f
            self.final_recommandation[sname] = result
            for client in self.sofas[sname].waiting_clients:
                client.write(str(result.id) + ';' + result.poster)
            self.sofas[sname].waiting_clients = []
    
    def delete_sofa(self, sname):
        try: self.sofas.pop(sname)
        except KeyError: pass
        try: self.first_recommandation.pop(sname)
        except KeyError: pass
        try: self.final_recommandation.pop(sname)
        except KeyError: pass       
Exemple #4
0
class SofaManager():
    """Class to manage the existing Sofas."""
    genres = Genre.__genres__

    class Duplicate(Exception):
        """To be raised when a Sofa already exists."""
        pass

    def __init__(self):
        self.recommender = Recommender()
        self.sofas = defaultdict(lambda: None)
        self.first_recommandation = defaultdict(lambda: None)
        self.final_recommandation = defaultdict(lambda: None)
        self.ctrl_thread = False

    def __enter__(self):
        self.start_ctrl_thread()

    def __exit__(self, type, value, tb):
        self.stop_ctrl_thread()

    def start_ctrl_thread(self, step=300, max_age=600):
        self.ctrl_thread = True

        def ctrl_thread():
            if self.ctrl_thread:
                for sname, sofa in list(self.sofas.items()):
                    if sofa:
                        age = dt.datetime.now() - sofa.last_update
                        if age.seconds > max_age:
                            self.delete_sofa(sname)
                    else:
                        self.delete_sofa(sname)
                timer = threading.Timer(step, ctrl_thread)
                timer.start()

        ctrl_thread()

    def stop_ctrl_thread(self):
        self.ctrl_thread = False

    def new_sofa(self, sname):
        if self.sofas[sname]:
            raise self.Duplicate('The sofa %s already exists' % sname)
        else:
            self.sofas[sname] = Sofa(sname)

    def add_preferences(self, uname, pwd, sname, pref_list):
        for c, p in zip(self.genres, pref_list):
            self.sofas[sname].preferences[c].append(int(p))
        s = self.sofas[sname].people
        s.add((uname, pwd))
        self.sofas[sname].people = s

    def get_nb_people(self, sname):
        return self.sofas[sname].nb_people

    def recommend(self, sname):
        sofa = self.sofas[sname]
        ratings = zip(*[sofa.preferences[c] for c in self.genres])
        films = [
            AppFilm(f[0], f[1], f[2])
            for f in self.recommender.recommend(ratings, sofa.people)
        ]
        return films

    def send_first_recommandation(self, sname):
        self.first_recommandation[sname] = films = self.recommend(sname)
        for client in self.sofas[sname].waiting_clients:
            posters = ';'.join([f.poster for f in films])
            ids = ';'.join([str(f.id) for f in films])
            client.write('::'.join([posters, ids]))
        self.sofas[sname].waiting_clients = []

    def add_films_rating(self, sname, rating_list):
        for f, r in zip(self.first_recommandation[sname], rating_list):
            f.add_rating(r)
        self.sofas[sname].last_update = dt.datetime.now()

    def get_final_recommandation(self, sname, client):
        nb_people = self.sofas[sname].nb_people
        self.sofas[sname].waiting_clients.append(client)
        if self.first_recommandation[sname][0].nb_rating >= nb_people:
            result = self.first_recommandation[sname][0]
            for f in self.first_recommandation[sname][1:]:
                if f.rating > result.rating:
                    result = f
            self.final_recommandation[sname] = result
            for client in self.sofas[sname].waiting_clients:
                client.write(str(result.id) + ';' + result.poster)
            self.sofas[sname].waiting_clients = []

    def delete_sofa(self, sname):
        try:
            self.sofas.pop(sname)
        except KeyError:
            pass
        try:
            self.first_recommandation.pop(sname)
        except KeyError:
            pass
        try:
            self.final_recommandation.pop(sname)
        except KeyError:
            pass