def generate_slices(sliceable_set, batch_len=<NUM_LIT:1>, length=None, start_batch=<NUM_LIT:0>):
if length is None:<EOL><INDENT>try:<EOL><INDENT>length = sliceable_set.count()<EOL><DEDENT>except (IndexError, ValueError, AttributeError, TypeError):<EOL><INDENT>length = len(sliceable_set)<EOL><DEDENT><DEDENT>length = int(length)<EOL>for i in range(int(length / batch_len + <NUM_LIT:1>)):<EOL><INDENT>if i < start_batch:<EOL><INDENT>continue<EOL><DEDENT>start = i * batch_len<EOL>end = min((i + <NUM_LIT:1>) * batch_len, length)<EOL>if start != end:<EOL><INDENT>yield tuple(sliceable_set[start:end])<EOL><DEDENT><DEDENT>raise StopIteration<EOL>
Iterate through a sequence (or generator) in batches of length `batch_len`

    See Also:
      pug.dj.db.generate_queryset_batches

    References:
      http://stackoverflow.com/a/761125/623735

    Examples:
      >>  [batch for batch in generate_slices(range(7), 3)]
      [(0, 1, 2), (3, 4, 5), (6,)]
      >>  from django.contrib.auth.models import User, Permission
      >>  len(list(generate_slices(User.objects.all(), 2)))       == max(math.ceil(User.objects.count() / 2.), 1)
      True
      >>  len(list(generate_slices(Permission.objects.all(), 2))) == max(math.ceil(Permission.objects.count() / 2.), 1)
      True