From 2c9fb4f295f46ce83645dd8c1ae5c424dc4e430a Mon Sep 17 00:00:00 2001 From: "Christopher K. Hoadley" Date: Tue, 31 Dec 2019 21:18:49 -0600 Subject: [PATCH] Change SitesInformation() to use a generator when iterating thru the sites. This avoids the problem of the state (i.e. self.__iteration_index) getting corrupted if any of the methods of a given object needed to iterate for their own purposes while a caller was already iterating thru the same object. The code is also much simpler to follow. --- sherlock/sites.py | 28 ++-------------------------- 1 file changed, 2 insertions(+), 26 deletions(-) diff --git a/sherlock/sites.py b/sherlock/sites.py index ad20a66e..d843613a 100644 --- a/sherlock/sites.py +++ b/sherlock/sites.py @@ -191,9 +191,6 @@ class SitesInformation(): f"Missing attribute {str(error)}." ) - #Initialize state if anyone iterates over this object. - self.__iteration_index = 0 - return def site_name_list(self, popularity_rank=False): @@ -234,30 +231,9 @@ class SitesInformation(): Return Value: Iterator for sites object. """ - return self - - def __next__(self): - """Next Method For Object. - - Keyword Arguments: - self -- This object. - - Return Value: - Returns individual site from beginning of self.sites dictionary - to the end. - Raises StopIteration when all sites have been passed. - """ - - if self.__iteration_index >= len(self.sites): - #Finished with iteration. - self.__iteration_index = 0 - raise StopIteration - else: - #Retrieve the next site from the ordered dictionary. - site = self.sites[list(self.sites)[self.__iteration_index]] - self.__iteration_index += 1 - return site + for site_name in self.sites: + yield self.sites[site_name] def __len__(self): """Length For Object.