Listening for a particular pattern
<p>I use the following listener to listen to a particular pattern. I expects to find "hello" somewhere among the data being sent to it. The process method is called many times each second so there's no idea in saving <strong>all</strong> the data processed in an ever growing list. Is there a nicer way to do this?</p>

<pre><code>class Listener:
    def __init__(self, data_to_catch):
        self.data_to_catch = data_to_catch
        self.length_of_data_to_catch = len(data_to_catch)
        self.previous_data = ""
        self.found = False

    def process(self, data):
        if self.found:
            return

        self.previous_data = self.previous_data + data
        if not self.data_to_catch in self.previous_data:
            # see if some of the data to catch has arrived, if so keep it                
            for i in reversed(range(min(self.length_of_data_to_catch,len(self.previous_data)))):
                if self.data_to_catch[:i+1] == self.previous_data[-i-1:]:
                    return
            else:
                self.previous_data = ""
                return

        self.found = True
        print("found!")

l = Listener("hello")
l.process("he")
l.process("llh")
l.process("e")
l.process("ll")
l.process("he")
l.process("llohe")
</code></pre>
