Commits

Jason R. Coombs  committed 19f0826

Added itertools.is_empty

  • Participants
  • Parent commits 9dda183
  • Tags 5.5

Comments (0)

Files changed (2)

 Changes
 -------
 
+5.5
+~~~
+
+* Added `itertools.is_empty`.
+
 5.4
 ~~~
 

File jaraco/util/itertools.py

 import itertools
 import collections
 import math
+import functools
 
 from jaraco.util.numbers import ordinalth
+from .exceptions import throws_exception
 
 def make_rows(num_columns, seq):
 	"""
 	"""
 	return iter(tuple())
 
+def is_empty(iterable):
+	"""
+	Return whether the iterable is empty or not. Consumes at most one item
+	from the iterator to test.
+
+	>>> is_empty(xrange(0))
+	True
+	>>> is_empty(xrange(1))
+	False
+	"""
+	get_next = functools.partial(next, iter(iterable))
+	return throws_exception(get_next, StopIteration)
+
 class Reusable(object):
 	"""
 	An iterator that may be reset and reused.