I have a 3rd party library with a parser that expects a callback class with a new_token
method. So far, my callback class and my token processing looks like this:
class MySink(object):
def __init__(self):
self.tokens = []
def new_token(self, token):
self.tokens.append(token)
sink = MySink()
p = ThirdPartyParser(sink)
p.parse("my_data_file")
for t in sink.tokens:
print t
The token list can get very long (leading to memory problems) so I'd like to turn MySink
into an iterable class where the tokens don't have to be stored in a list but are "pulled" on the fly and the parsing is stopped while the token is processed. Something like this:
class MyIterableSink(object): # incomplete!
def new_token(self, token):
# TODO:
# Store token for next iteration step
# halt execution like with 'yield', wait for next iteration step
sink = MyIterableSink()
p = ThirdPartyParser(sink)
p.parse("my_data_file")
for t in sink:
print t
How do I have to modify the MyIterableSink
class? Is something like this possible? I can't modify the parser class, only the callback class. I know I have to implement __iter__
and __next__
methods and use coroutines where probably the tokens are sent with the send
method but can't quite wrap my head around it. Any code examples would be appreciated.