dstl / Stone-Soup

A software project to provide the target tracking community with a framework for the development and testing of tracking algorithms.
https://stonesoup.rtfd.io
MIT License
384 stars 126 forks source link

When `iter` is called on an iterator (Tracker), it shouldn't reset according to Python standard #883

Open gawebb-dstl opened 8 months ago

gawebb-dstl commented 8 months ago

This issue rarely is a problem, but it is when using heapq. (See example below)

from stonesoup.tracker import Tracker
import heapq

class MyTracker(Tracker):
    a_list = [0, 1, 2, 3, 4, 5]

    def __iter__(self):
        self.list_iter = iter(self.a_list)

        return super().__iter__()

    def __next__(self):
        return next(self.list_iter) ** 2

    def tracks(self):
        return self.a_list

print(list(MyTracker()))
print(list(heapq.merge(MyTracker(), MyTracker())))

Expected output: [0, 1, 4, 9, 16, 25] [0, 0, 1, 1, 4, 4, 9, 9, 16, 16, 25, 25]

Actual output: [0, 1, 4, 9, 16, 25] [0, 0, 1, 1, 4, 4, 9, 9, 16, 16, 25, 25, 0, 1, 4, 9, 16, 25]