new function 'limit' to limit the number of iterations

This commit is contained in:
Romain Bignon 2011-03-21 13:34:32 +01:00
commit 9e6153a16c

View file

@ -23,8 +23,8 @@ import traceback
import types
__all__ = ['get_backtrace', 'get_bytes_size', 'html2text', 'iter_fields',
'local2utc', 'to_unicode', 'utc2local']
__all__ = ['get_backtrace', 'get_bytes_size', 'html2text', 'iter_fields',
'local2utc', 'to_unicode', 'utc2local', 'limit']
def get_backtrace(empty="Empty backtrace."):
@ -107,3 +107,11 @@ def utc2local(date):
date = date.replace(tzinfo=tz.tzutc())
date = date.astimezone(tz.tzlocal())
return date
def limit(iterator, lim):
count = 0
iterator = iter(iterator)
while count < lim:
yield iterator.next()
count += 1
raise StopIteration()