From 1327e4182fae4fe97e30314d0652932ce18fba9c Mon Sep 17 00:00:00 2001 From: Benjamin Dauvergne Date: Tue, 17 Mar 2015 12:31:19 +0100 Subject: [PATCH 1/4] utils: add an helper function to cut an iterable as batch iterable of fixed sizes --- src/authentic2/utils.py | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/src/authentic2/utils.py b/src/authentic2/utils.py index 1ec4299..3739703 100644 --- a/src/authentic2/utils.py +++ b/src/authentic2/utils.py @@ -1,15 +1,16 @@ import random import time import logging import urllib import six import urlparse from functools import wraps +from itertools import islice, chain from importlib import import_module from django.conf import settings from django.http import HttpResponseRedirect, HttpResponsePermanentRedirect from django.core.exceptions import ImproperlyConfigured from django.core import urlresolvers from django.http.request import QueryDict @@ -434,8 +435,17 @@ def attribute_values_to_identifier(values): def csrf_token_check(request, form): '''Check a request for CSRF cookie validation, and add an error to the form if check fails. ''' if form.is_valid() and not getattr(request, 'csrf_processing_done', False): msg = _('The form was out of date, please try again.') form._errors[forms.forms.NON_FIELD_ERRORS] = ErrorList([msg]) + +def batch(iterable, size): + '''Batch an iterable as an iterable of iterables of at most size element + long. + ''' + sourceiter = iter(iterable) + while True: + batchiter = islice(sourceiter, size) + yield chain([batchiter.next()], batchiter) -- 1.9.1