Resetting generator object in Python

Generators can’t be rewound. You have the following options: Run the generator function again, restarting the generation: y = FunctionWithYield() for x in y: print(x) y = FunctionWithYield() for x in y: print(x) Store the generator results in a data structure on memory or disk which you can iterate over again: y = list(FunctionWithYield()) for … Read more

Reading from a frequently updated file

I would recommend looking at David Beazley’s Generator Tricks for Python, especially Part 5: Processing Infinite Data. It will handle the Python equivalent of a tail -f logfile command in real-time. # follow.py # # Follow a file like tail -f. import time def follow(thefile): thefile.seek(0,2) while True: line = thefile.readline() if not line: time.sleep(0.1) … Read more

How do I know if a generator is empty from the start?

Suggestion: def peek(iterable): try: first = next(iterable) except StopIteration: return None return first, itertools.chain([first], iterable) Usage: res = peek(mysequence) if res is None: # sequence is empty. Do stuff. else: first, mysequence = res # Do something with first, maybe? # Then iterate over the sequence: for element in mysequence: # etc.

How to take the first N items from a generator or list? [duplicate]

Slicing a list top5 = array[:5] To slice a list, there’s a simple syntax: array[start:stop:step] You can omit any parameter. These are all valid: array[start:], array[:stop], array[::step] Slicing a generator import itertools top5 = itertools.islice(my_list, 5) # grab the first five elements You can’t slice a generator directly in Python. itertools.islice() will wrap an object … Read more

What can you use generator functions for?

Generators give you lazy evaluation. You use them by iterating over them, either explicitly with ‘for’ or implicitly by passing it to any function or construct that iterates. You can think of generators as returning multiple items, as if they return a list, but instead of returning them all at once they return them one-by-one, … Read more