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.

pull/350/head
Christopher K. Hoadley 4 years ago
parent 1101af8132
commit 2c9fb4f295

@ -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.

Loading…
Cancel
Save