Domanda Python esercizio pattern chain

jr_sottomajor

Utente Silver
2 Luglio 2017
96
33
4
79
Ciao stavo risolvendo questo esercizio python sul pattern chain of responsibility, questa è la traccia:
Schermata 2021-10-27 alle 17.06.26.png


Questo è il mio codice:
Python:
def coroutine(f):
    def wrapper(*args, **kwargs):
        generator = f(*args, **kwargs)
        next(generator)
        return generator
    return wrapper

@coroutine
def gestore_ag(successor = None):
    while True:
        stringa = (yield)
        if stringa[0].isalpha() and (stringa[0] >= 'a' and stringa [0] <= 'g'):
            print("Richiesta {} gestita da gestore_ag".format(stringa))
        elif successor is not None:
            successor.send(stringa)


@coroutine
def gestore_hn(successor = None):
    while True:
        stringa = (yield)
        if stringa[0].isalpha() and (stringa[0] >= 'h' and stringa[0] <= 'n'):
            print("Richiesta {} gestita da gestore_hn".format(stringa))
        elif successor is not None:
            successor.send(stringa)

@coroutine
def gestore_distr(successor = None):
    while True:
        stringa = (yield)
        if not stringa[0].isalpha():
            print("Richiesta {} gestita da gestore_distr: uso improprio della catena di gestori".format(stringa))
            exit()
            

@coroutine
def gestoreDiDefault(successor = None):
    while True:
        stringa = (yield)
        if stringa[0].isalpha() and ((stringa[0] >= 'n' and stringa[0] <= 'z') or (stringa[0] >= 'A' and stringa[0] <= 'Z')):
            print("Messaggio da gestoreDiDefault: non è stato possibile gestire la richiesta {}".format(stringa))
        elif successor is not None:
            successor.send(stringa)

def main():
    pipeline = gestore_ag(gestore_hn(gestoreDiDefault(gestore_distr())))
    l = ["ciao", "laura", "zei", "HAHHAHA", "2sei"]
    for i in l:
        pipeline.send(i)

if __name__ == "__main__":
    main()

Come posso gestire l'ultimo punto "se ad un certo punto un gestore manda la richiesta al suo successore ed il successore smette di funzionare ecc. ecc."? Non capisco come poter fare il controllo.. grazie in anticipo