src/pyams_batching/batch.py
def first_neighbours_last(batches, current_batch_idx, nb_left, nb_right):
    """Build a sublist from a large batch list.

    This is used to display batch links for a large table.

    arguments:
     * :param batches: a large sequence (may be a batches as well)
     * :param current_batch_idx: index of the current batch or item
     * :param nb_left: number of neighbours before the current batch
     * :param nb_right: number of neighbours after the current batch

    The returned list gives:
     * the first batch
     * a None separator if necessary
     * left neighbours of the current batch
     * the current batch
     * right neighbours of the current batch
     * a None separator if necessary
     * the last batch

    Example:

      >>> from pyams_batching.batch import first_neighbours_last as f_n_l
      >>> batches = range(100) # it works with real batches as well

    We try to get subsets at different levels:

      >>> for i in range(0,6):
      ...    f_n_l(batches, i, 2, 2)
      [0, 1, 2, None, 99]
      [0, 1, 2, 3, None, 99]
      [0, 1, 2, 3, 4, None, 99]
      [0, 1, 2, 3, 4, 5, None, 99]
      [0, None, 2, 3, 4, 5, 6, None, 99]
      [0, None, 3, 4, 5, 6, 7, None, 99]

      >>> for i in range(93, 99):
      ...    f_n_l(batches, i, 2, 2)
      [0, None, 91, 92, 93, 94, 95, None, 99]
      [0, None, 92, 93, 94, 95, 96, None, 99]
      [0, None, 93, 94, 95, 96, 97, None, 99]
      [0, None, 94, 95, 96, 97, 98, 99]
      [0, None, 95, 96, 97, 98, 99]
      [0, None, 96, 97, 98, 99]

    Try with no previous and no next batch:

      >>> f_n_l(batches, 0, 0, 0)
      [0, None, 99]
      >>> f_n_l(batches, 1, 0, 0)
      [0, 1, None, 99]
      >>> f_n_l(batches, 2, 0, 0)
      [0, None, 2, None, 99]

    Try with only 1 previous and 1 next batch:

      >>> f_n_l(batches, 0, 1, 1)
      [0, 1, None, 99]
      >>> f_n_l(batches, 1, 1, 1)
      [0, 1, 2, None, 99]
      >>> f_n_l(batches, 2, 1, 1)
      [0, 1, 2, 3, None, 99]
      >>> f_n_l(batches, 3, 1, 1)
      [0, None, 2, 3, 4, None, 99]

    Try with incoherent values:

      >>> f_n_l(batches, 0, -4, -10)
      Traceback (most recent call last):
      ...
      AssertionError
      >>> f_n_l(batches, 2000, 3, 3)
      Traceback (most recent call last):
      ...
      AssertionError
    """
    sublist = []
    # setup some batches and indexes
    first_idx = 0
    last_idx = len(batches) - 1
    assert 0 <= current_batch_idx <= last_idx
    assert nb_left >= 0 and nb_right >= 0
    prev_idx = current_batch_idx - nb_left
    next_idx = current_batch_idx + 1
    first_batch = batches[0]
    last_batch = batches[last_idx]

    # add first batch
    if first_idx < current_batch_idx:
        sublist.append(first_batch)

    # there must probably be space
    if first_idx + 1 < prev_idx:
        # we skip batches between first batch and first previous batch
        sublist.append(None)

    # add previous batches
    for i in range(prev_idx, prev_idx + nb_left):
        if first_idx < i:
            # append previous batches
            sublist.append(batches[i])

    # add current batch
    sublist.append(batches[current_batch_idx])

    # add next batches
    for i in range(next_idx, next_idx + nb_right):
        if i < last_idx:
            # append previous batch
            sublist.append(batches[i])

    # there must probably be space
    if next_idx + nb_right < last_idx:
        # we skip batches between last batch and last next batch
        sublist.append(None)

    # add last batch
    if current_batch_idx < last_idx:
        sublist.append(last_batch)
    return sublist
Build a sublist from a large batch list.

    This is used to display batch links for a large table.

    arguments:
     * :param batches: a large sequence (may be a batches as well)
     * :param current_batch_idx: index of the current batch or item
     * :param nb_left: number of neighbours before the current batch
     * :param nb_right: number of neighbours after the current batch

    The returned list gives:
     * the first batch
     * a None separator if necessary
     * left neighbours of the current batch
     * the current batch
     * right neighbours of the current batch
     * a None separator if necessary
     * the last batch

    Example:

      >>> from pyams_batching.batch import first_neighbours_last as f_n_l
      >>> batches = range(100) # it works with real batches as well

    We try to get subsets at different levels:

      >>> for i in range(0,6):
      ...    f_n_l(batches, i, 2, 2)
      [0, 1, 2, None, 99]
      [0, 1, 2, 3, None, 99]
      [0, 1, 2, 3, 4, None, 99]
      [0, 1, 2, 3, 4, 5, None, 99]
      [0, None, 2, 3, 4, 5, 6, None, 99]
      [0, None, 3, 4, 5, 6, 7, None, 99]

      >>> for i in range(93, 99):
      ...    f_n_l(batches, i, 2, 2)
      [0, None, 91, 92, 93, 94, 95, None, 99]
      [0, None, 92, 93, 94, 95, 96, None, 99]
      [0, None, 93, 94, 95, 96, 97, None, 99]
      [0, None, 94, 95, 96, 97, 98, 99]
      [0, None, 95, 96, 97, 98, 99]
      [0, None, 96, 97, 98, 99]

    Try with no previous and no next batch:

      >>> f_n_l(batches, 0, 0, 0)
      [0, None, 99]
      >>> f_n_l(batches, 1, 0, 0)
      [0, 1, None, 99]
      >>> f_n_l(batches, 2, 0, 0)
      [0, None, 2, None, 99]

    Try with only 1 previous and 1 next batch:

      >>> f_n_l(batches, 0, 1, 1)
      [0, 1, None, 99]
      >>> f_n_l(batches, 1, 1, 1)
      [0, 1, 2, None, 99]
      >>> f_n_l(batches, 2, 1, 1)
      [0, 1, 2, 3, None, 99]
      >>> f_n_l(batches, 3, 1, 1)
      [0, None, 2, 3, 4, None, 99]

    Try with incoherent values:

      >>> f_n_l(batches, 0, -4, -10)
      Traceback (most recent call last):
      ...
      AssertionError
      >>> f_n_l(batches, 2000, 3, 3)
      Traceback (most recent call last):
      ...
      AssertionError
    
Build a sublist from a large batch list.
This is used to display batch links for a large table.



The returned list gives:
the first batch
a None separator if necessary
left neighbours of the current batch
the current batch
right neighbours of the current batch
a None separator if necessary
the last batch
Build a sublist from a large batch list . This is used to display batch links for a large table . The returned list gives : the first batch a None separator if necessary left neighbours of the current batch the current batch right neighbours of the current batch a None separator if necessary the last batch
def first_neighbours_last(batches, current_batch_idx, nb_left, nb_right):
    sublist = []
    first_idx = 0
    last_idx = len(batches) - 1
    assert 0 <= current_batch_idx <= last_idx
    assert nb_left >= 0 and nb_right >= 0
    prev_idx = current_batch_idx - nb_left
    next_idx = current_batch_idx + 1
    first_batch = batches[0]
    last_batch = batches[last_idx]
    if first_idx < current_batch_idx:
        sublist.append(first_batch)
    if first_idx + 1 < prev_idx:
        sublist.append(None)
    for i in range(prev_idx, prev_idx + nb_left):
        if first_idx < i:
            sublist.append(batches[i])
    sublist.append(batches[current_batch_idx])
    for i in range(next_idx, next_idx + nb_right):
        if i < last_idx:
            sublist.append(batches[i])
    if next_idx + nb_right < last_idx:
        sublist.append(None)
    if current_batch_idx < last_idx:
        sublist.append(last_batch)
    return sublist
def first_neighbours_last ( batches , current_batch_idx , nb_left , nb_right ) : sublist = [ ] first_idx = 0 last_idx = len ( batches ) - 1 assert 0 <= current_batch_idx <= last_idx assert nb_left >= 0 and nb_right >= 0 prev_idx = current_batch_idx - nb_left next_idx = current_batch_idx + 1 first_batch = batches [ 0 ] last_batch = batches [ last_idx ] if first_idx < current_batch_idx : sublist . append ( first_batch ) if first_idx + 1 < prev_idx : sublist . append ( None ) for i in range ( prev_idx , prev_idx + nb_left ) : if first_idx < i : sublist . append ( batches [ i ] ) sublist . append ( batches [ current_batch_idx ] ) for i in range ( next_idx , next_idx + nb_right ) : if i < last_idx : sublist . append ( batches [ i ] ) if next_idx + nb_right < last_idx : sublist . append ( None ) if current_batch_idx < last_idx : sublist . append ( last_batch ) return sublist
Build a sublist from a large batch list.
Build a sublist from a large batch list .
"""Build a sublist from a large batch list.

    This is used to display batch links for a large table.

    arguments:
     * :param batches: a large sequence (may be a batches as well)
     * :param current_batch_idx: index of the current batch or item
     * :param nb_left: number of neighbours before the current batch
     * :param nb_right: number of neighbours after the current batch

    The returned list gives:
     * the first batch
     * a None separator if necessary
     * left neighbours of the current batch
     * the current batch
     * right neighbours of the current batch
     * a None separator if necessary
     * the last batch

    Example:

      >>> from pyams_batching.batch import first_neighbours_last as f_n_l
      >>> batches = range(100) # it works with real batches as well

    We try to get subsets at different levels:

      >>> for i in range(0,6):
      ...    f_n_l(batches, i, 2, 2)
      [0, 1, 2, None, 99]
      [0, 1, 2, 3, None, 99]
      [0, 1, 2, 3, 4, None, 99]
      [0, 1, 2, 3, 4, 5, None, 99]
      [0, None, 2, 3, 4, 5, 6, None, 99]
      [0, None, 3, 4, 5, 6, 7, None, 99]

      >>> for i in range(93, 99):
      ...    f_n_l(batches, i, 2, 2)
      [0, None, 91, 92, 93, 94, 95, None, 99]
      [0, None, 92, 93, 94, 95, 96, None, 99]
      [0, None, 93, 94, 95, 96, 97, None, 99]
      [0, None, 94, 95, 96, 97, 98, 99]
      [0, None, 95, 96, 97, 98, 99]
      [0, None, 96, 97, 98, 99]

    Try with no previous and no next batch:

      >>> f_n_l(batches, 0, 0, 0)
      [0, None, 99]
      >>> f_n_l(batches, 1, 0, 0)
      [0, 1, None, 99]
      >>> f_n_l(batches, 2, 0, 0)
      [0, None, 2, None, 99]

    Try with only 1 previous and 1 next batch:

      >>> f_n_l(batches, 0, 1, 1)
      [0, 1, None, 99]
      >>> f_n_l(batches, 1, 1, 1)
      [0, 1, 2, None, 99]
      >>> f_n_l(batches, 2, 1, 1)
      [0, 1, 2, 3, None, 99]
      >>> f_n_l(batches, 3, 1, 1)
      [0, None, 2, 3, 4, None, 99]

    Try with incoherent values:

      >>> f_n_l(batches, 0, -4, -10)
      Traceback (most recent call last):
      ...
      AssertionError
      >>> f_n_l(batches, 2000, 3, 3)
      Traceback (most recent call last):
      ...
      AssertionError
    """ # setup some batches and indexes # add first batch # there must probably be space # we skip batches between first batch and first previous batch # add previous batches # append previous batches # add current batch # add next batches # append previous batch # there must probably be space # we skip batches between last batch and last next batch # add last batch
batches
current_batch_idx
nb_left
nb_right
examples
>>> from pyams_batching.batch import first_neighbours_last as f_n_l
>>> batches = range(100) # it works with real batches as well
>>> from pyams_batching . batch import first_neighbours_last as f_n_l >>> batches = range ( 100 ) # it works with real batches as well