Added utils.iter.one, to grab one value from an iterable.

This commit is contained in:
Jeremy Fincher 2005-06-12 04:54:15 +00:00
parent 201d4acd26
commit b369039507
2 changed files with 13 additions and 0 deletions

View File

@ -148,6 +148,11 @@ def ilen(iterable):
i += 1
return i
def one(iterable):
for x in iterable:
return x
raise ValueError, 'Empty iterable: %r' % iterable
def startswith(long, short):
longI = iter(long)
shortI = iter(short)

View File

@ -384,6 +384,14 @@ class StrTest(SupyTestCase):
class IterTest(SupyTestCase):
def testOne(self):
L = range(3)
i = iter(L)
self.assertEqual(utils.iter.one(i), 0)
self.assertEqual(utils.iter.one(i), 1)
self.assertEqual(utils.iter.one(i), 2)
self.assertRaises(ValueError, utils.iter.one, i)
def testLimited(self):
L = range(10)
self.assertEqual([], list(utils.iter.limited(L, 0)))