Itertools

Heiko Wundram heikowu at ceosg.de
Tue Jul 29 21:56:47 CEST 2003


Just tested the code, amend as stated (remove the - lines, and add
the + lines):

--- START OF CODE ---

import copy

class IDup(object):
	def __init__(self,iterin):
		self.__iter = iterin
		self.__iterno = 0
		self.__iteritems = []
		self.__hasstopped = None

-	def registerIter():
+	def registerIter(self):
		iterno = self.__iterno
		self.__iterno += 1
		self.__iteritems.append([])
+		return iterno

	def getNext(self,iterno):
		if self.__iteritems[iterno]:
			iteritem = self.__iteritems[iterno].pop(0)
		elif self.__hasstopped is not None:
			raise self.__hasstopped
		else:
			try:
				iteritem = self.__iter.next()
			except StopIteration, e:
				self.__hasstopped = e
				raise
			for id, i in enumerate(self.__iteritems):
				if id <> iterno:
					i.append(copy.deepcopy(iteritem))
		return iteritem

class IDupped(object):
	def __init__(self,idup):
		self.__idup = idup
		self.__iterno = idup.registerIter()

	def next(self):
		return self.__idup.getNext(self.__iterno)

+	def __iter__(self):
+		return self

def isplit(iterin,splitno=2):
	idup = IDup(iterin)
	iduppeds = []
	for i in range(splitno):
		iduppeds.append(IDupped(idup))
	return tuple(iduppeds)

test = ["hello","how","are","you?"]
x, y = isplit(iter(test))

for i in x:
	print i
for i in y:
	print i

--- END OF CODE ---

This works as stated.

Heiko.






More information about the Python-list mailing list