<?xml version="1.0" encoding="UTF-8" ?>
<rdf:RDF xmlns="http://usefulinc.com/ns/doap#" xmlns:foaf="http://xmlns.com/foaf/0.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"><Project><name>z3c.batching</name>
<shortdesc>Batching</shortdesc>
<description>This package provides simple sequence batching.


======================
Detailed Documentation
======================



.. contents::

Simple Batching
---------------

This module implements a simple batching mechanism that allows you to split a
large sequence into smaller batches. Let's start by creating a simple list,
which will be our full sequence:

Batch on empty root:

  &gt;&gt;&gt; from z3c.batching.batch import Batch
  &gt;&gt;&gt; batch = Batch([], size=3)
  &gt;&gt;&gt; len(batch)
  0
  &gt;&gt;&gt; batch.firstElement
  Traceback (most recent call last):
  ...
  IndexError: ...

  &gt;&gt;&gt; batch.lastElement
  Traceback (most recent call last):
  ...
  IndexError: ...

  &gt;&gt;&gt; batch[0]
  Traceback (most recent call last):
  ...
  IndexError: ...

  &gt;&gt;&gt; batch.next is None
  True

  &gt;&gt;&gt; batch.previous is None
  True


  &gt;&gt;&gt; sequence = ['one', 'two', 'three', 'four', 'five', 'six', 'seven',
  ...             'eight', 'nine', 'ten', 'eleven', 'twelve', 'thirteen']

We can now create a batch for this sequence. Let's make our batch size 3:

  &gt;&gt;&gt; batch = Batch(sequence, size=3)

The first argument to the batch is always the full sequence. If no start
element is specified, the batch starts at the first element:

  &gt;&gt;&gt; list(batch)
  ['one', 'two', 'three']

The start index is commonly specified in the constructor though:

  &gt;&gt;&gt; batch = Batch(sequence, start=6, size=3)
  &gt;&gt;&gt; list(batch)
  ['seven', 'eight', 'nine']

Note that the start is an index and starts at zero. If the start index is
greater than the largest index of the sequence, an index error is raised:

  &gt;&gt;&gt; Batch(sequence, start=15, size=3)
  Traceback (most recent call last):
  ...
  IndexError: start index key out of range

A batch implements the finite sequence interface and thus supports some
standard methods. For example, you can ask the batch for its length:

  &gt;&gt;&gt; len(batch)
  3

Note that the length returns the true size of the batch, not the size we asked
for:

  &gt;&gt;&gt; len(Batch(sequence, start=12, size=3))
  1

You can also get an element by index, which is relative to the batch:

  &gt;&gt;&gt; batch[0]
  'seven'
  &gt;&gt;&gt; batch[1]
  'eight'
  &gt;&gt;&gt; batch[2]
  'nine'

Slicing:

  &gt;&gt;&gt; batch[:1]
  ['seven']

  &gt;&gt;&gt; batch[1:2]
  ['eight']

  &gt;&gt;&gt; batch[1:]	
  ['eight', 'nine']

  &gt;&gt;&gt; batch[:]
  ['seven', 'eight', 'nine']

  &gt;&gt;&gt; batch[10:]
  []
  

If you ask for index that is out of range, an index error is raised:

  &gt;&gt;&gt; batch[3]
  Traceback (most recent call last):
  ...
  IndexError: batch index out of range

You can also iterate through the batch:

  &gt;&gt;&gt; iterator = iter(batch)
  &gt;&gt;&gt; iterator.next()
  'seven'
  &gt;&gt;&gt; iterator.next()
  'eight'
  &gt;&gt;&gt; iterator.next()
  'nine'

Batch also implement some of IReadSequence interface:

  &gt;&gt;&gt; 'eight' in batch
  True

  &gt;&gt;&gt; 'ten' in batch
  False

  &gt;&gt;&gt; batch == Batch(sequence, start=6, size=3)
  True

  &gt;&gt;&gt; batch != Batch(sequence, start=6, size=3)
  False

  &gt;&gt;&gt; batch != Batch(sequence, start=3, size=3)
  True

Besides all of those common API methods, there are several properties that were
designed to make your life simpler. The start and size are specified:

  &gt;&gt;&gt; batch.start
  6
  &gt;&gt;&gt; batch.size
  3

The end index of the batch is immediately computed:

  &gt;&gt;&gt; batch.end
  8

The UI often requires that the number of the batch and the total number of
batches is computed:

  &gt;&gt;&gt; batch.number
  3
  &gt;&gt;&gt; batch.total
  5

You can also ask for the next batch:

  &gt;&gt;&gt; batch.next
  &lt;Batch start=9, size=3&gt;

If the current batch is the last one, the next batch is None:

  &gt;&gt;&gt; Batch(sequence, start=12, size=3).next is None
  True

The previous batch shows the previous batch:

  &gt;&gt;&gt; batch.previous
  &lt;Batch start=3, size=3&gt;

If the current batch is the first one, the previous batch is None:

  &gt;&gt;&gt; Batch(sequence, start=0, size=3).previous is None
  True

The final two properties deal with the elements within the batch. They ask for
the first and last element of the batch:

  &gt;&gt;&gt; batch.firstElement
  'seven'

  &gt;&gt;&gt; batch.lastElement
  'nine'


Total batches:

  &gt;&gt;&gt; batch = Batch(sequence[:-1], size=3)
  &gt;&gt;&gt; batch.total
  4

We can have access to all batches:

  &gt;&gt;&gt; len(batch.batches)
  4

  &gt;&gt;&gt; batch.batches[0]
  &lt;Batch start=0, size=3&gt;

  &gt;&gt;&gt; batch.batches[3]
  &lt;Batch start=9, size=3&gt;

  &gt;&gt;&gt; batch.batches[4]
  Traceback (most recent call last):
  ...
  IndexError: ...

  &gt;&gt;&gt; batch.batches[-1]
  &lt;Batch start=9, size=3&gt;

  &gt;&gt;&gt; batch.batches[-2]
  &lt;Batch start=6, size=3&gt;

Slicing:

  &gt;&gt;&gt; batch.batches[:1]
  [&lt;Batch start=0, size=3&gt;]

  &gt;&gt;&gt; batch.batches[:]
  [&lt;Batch start=0, size=3&gt;, &lt;Batch start=3, size=3&gt;, &lt;Batch start=6, size=3&gt;, &lt;Batch start=9, size=3&gt;]

  &gt;&gt;&gt; batch.batches[1:2]
  [&lt;Batch start=3, size=3&gt;]

  &gt;&gt;&gt; batch.batches[1:]
  [&lt;Batch start=3, size=3&gt;, &lt;Batch start=6, size=3&gt;, &lt;Batch start=9, size=3&gt;]

  &gt;&gt;&gt; batch.batches[10:]
  []

  &gt;&gt;&gt; batch.batches[2:50]
  [&lt;Batch start=6, size=3&gt;, &lt;Batch start=9, size=3&gt;]

Batch neighbourhood of a large batch list
-----------------------------------------

When the full list of batches is too large to be displayed in a user interface,
we want to display only a subset of all the batches.
A helper function is provided for that purpose:

First build a large sequence of batches (or anything else):

  &gt;&gt;&gt; batches = range(100)

Then extract only the first and last items, as well as the neighbourhood of the
46th item (index = 45). We want 3 neighbours at the left, 5 at the right:

  &gt;&gt;&gt; from z3c.batching.batch import first_neighbours_last
  &gt;&gt;&gt; first_neighbours_last(batches, 45, 3, 5)
  [0, None, 42, 43, 44, 45, 46, 47, 48, 49, 50, None, 99]

'None' can be used to display a separator in a user interface (see z3c.table) 



=======
CHANGES
=======

1.1.0 (2008-11-12)
------------------

- Added a function to build a small neighbourhood list of the current batch,
  from a large batch list. (extracted from z3c.table)

- Really fixed the bug with batches slicing

1.0.1 (2008-09-09)
------------------

- Fixed bug with batches slicing.


1.0.0 (2008-02-18)
------------------

- Initial release.</description>
<homepage rdf:resource="http://pypi.python.org/pypi/z3c.batching" />
<maintainer><foaf:Person><foaf:name>Zope Corporation and Contributors</foaf:name>
<foaf:mbox_sha1sum>2a5d53de05a9e41953ed1826447b07e9a7fa1525</foaf:mbox_sha1sum></foaf:Person></maintainer>
<release><Version><revision>1.1.0</revision></Version></release>
</Project></rdf:RDF>