Skip to content

Instantly share code, notes, and snippets.

@mohitranka
Created July 7, 2009 18:18
Show Gist options
  • Save mohitranka/142259 to your computer and use it in GitHub Desktop.
Save mohitranka/142259 to your computer and use it in GitHub Desktop.
class ReversiableItertor:
"""
An iterator, having the previous function
"""
def __init__(self,data):
"""
"""
self.data = data
self.index=-1
def __setPosition(self,position):
"""
"""
self.index = position
def getCurrentPosition(self):
"""
"""
return self.index
def __iter__(self):
return self
def previous(self,n=1):
"""
Returns the previous nth element from the current position,
None if current position is beyond list lower limit
"""
prev_pos = self.index - n
if prev_pos >= 0:
self.index = prev_pos
return self.data[self.index]
else:
return None
def next(self,n=1):
"""
Returns the next nth element from the current position,
None if current position is beyond list upper limit
"""
next_pos = self.index + n
if next_pos < len(self.data):
self.index = next_pos
return self.data[self.index]
else:
return None
def getPositionToken(self,position):
"""
"""
if position < len(self.data):
self.__setPosition(position)
return self.data[position]
else:
return None
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment