Skip to content

Commit

Permalink
add new iterator
Browse files Browse the repository at this point in the history
  • Loading branch information
fgrunewald committed May 6, 2024
1 parent 823fcec commit 9b91d31
Showing 1 changed file with 14 additions and 13 deletions.
27 changes: 14 additions & 13 deletions cgsmiles/read_fragments.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,26 +12,27 @@ class PeekIter(object):
advancing the actual iter.
"""
def __init__(self, collection):
self.collection = collection
self.index = 0
self.collection = iter(collection)
self._peek = None

def __next__(self):
try:
result = self.collection[self.index]
self.index += 1
except IndexError:
raise StopIteration
return result
if self._peek:
item = self._peek
self._peek = None
else:
item = next(self.collection)
return item

def peek(self):
if self._peek:
return self._peek
try:
result = self.collection[self.index]
except IndexError:
return ""
return result
self._peek = next(self)
except StopIteration:
self._peek = None
return self._peek

def __iter__(self):
self.index = 0
return self


Expand Down

0 comments on commit 9b91d31

Please sign in to comment.