# -*- coding: utf-8 -*-
""" This module contains utilities for creating batches of elements from an iterable, in order to import them"""

def run_in_batches(iterable, end_batch_callback=None, batch_size=2, batch_start=0, max_batches=None):	
    """ This function is a generator that creates batches of elements from an iterable. 
        The callback function is called when a) maximum batches are reached or b) iterable is exhausted, also for incomplete batches 
        """ 	
    c = 0
    ended = False
    for k in iterable:
        c = c + 1			
        current_batch, position_in_batch = divmod(c, batch_size)
        if position_in_batch == 0:
            current_batch = current_batch - 1						# last element of a batch has position=0, value=current_batch+1
        if current_batch >= batch_start and (max_batches is None or current_batch < batch_start + max_batches):
            ended = False
            yield k 
            if position_in_batch == 0: 							# ends completed batches
                end_batch_callback()
                ended = True
        if max_batches is not None and current_batch == batch_start + max_batches:					# if gets maximum allowed batches, break 
            break
    if ended is False:
        end_batch_callback()	



@@ -105,33 +105,280 @@
 able
-, in order to import them
+%0A    user can select the batch size, the maximum number of batches and the starting batch from which to start yielding elements%0A    soon it will be possible to randomly sample elements to create batches and to retrieve the last batch (in order to reproduce some process) %0A
 %22%22%22%0A
@@ -677,18 +677,14 @@
 sted
-, also for
+ (ends
  inc
@@ -698,17 +698,17 @@
  batches
- 
+)
 %0A       

add docstring