Skip to main content

Cut sliceable objects into batches, eg for paging display on a website

Project description

Batcher
-------

Batches any sliceable object into batches::

>>> from batcher import Batcher
>>> items = list('ABCDEFGHIJ')
>>> batcher = Batcher(items, 4)
>>> # How many batches are available?
>>> len(batcher)
3
>>> # Get contents of the first batch
>>> list(batcher[0])
['A', 'B', 'C', 'D']

Batches look like lists but know about their context too::

>>> batch = batcher[1]
>>> batch
<Batch #1>
>>> batcher[1].previous
<Batch #0>
>>> batcher[1].next
<Batch #2>
>>> batcher[2].next
None

A common requirement in web applications is to implement a pager widget.
The range method can help when the user is viewing page **n** and we want to
display a range of pages centered on **n**, adjusting endpoints to keep
them in range::

>>> items = range(100)
>>> batcher = Batcher(items, 4)
>>> len(batcher)
25
>>> # The range of 5 batches centered on the given batch
>>> batcher[0].range(5)
[<Batch #0>, <Batch #1>, <Batch #2>, <Batch #3>, <Batch #4>]
>>> batcher[7].range(5)
[<Batch #5>, <Batch #6>, <Batch #7>, <Batch #8>, <Batch #9>]
>>> batcher[23].range(5)
[<Batch #20>, <Batch #21>, <Batch #22>, <Batch #23>, <Batch #24>]

If there aren't enough batches available, the entire batch will be returned::

>>> items = range(10)
>>> batcher = Batcher(items, 4)
>>> len(batcher)
3
>>> batcher[1].range(5)
[<Batch #0>, <Batch #1>, <Batch #2>]

Because batches use python's slicing API to retrieve data, we can
interrogate the slice object to find out the indices of the first and last
items in a batch, useful for showing data such as "Page 1 (items 1-10)"::

>>> items = range(25)
>>> batcher = Batcher(items, 10)
>>> batch = batcher[0]
>>> batch.slice
slice(0, 10, None)
>>> "Page %d (items %d-%d)" % (batch.index + 1, batch.slice.start + 1,
batch.slice.stop)
'Page 1 (items 1-10)'

Note how python's slice semantics mean that the indices are zero-based (so
we add 1 when formatting for display) and that the stop index of the slice
points to the item after the end of the series.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

Batcher-1.tar.gz (8.4 kB view details)

Uploaded Source

File details

Details for the file Batcher-1.tar.gz.

File metadata

  • Download URL: Batcher-1.tar.gz
  • Upload date:
  • Size: 8.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No

File hashes

Hashes for Batcher-1.tar.gz
Algorithm Hash digest
SHA256 d37043a8a7275e2f873361370f56258fb2434f780a4bf77cec6dbe83cc91c733
MD5 1bfe9c2ef06abbe5644c68ff9f52c23e
BLAKE2b-256 3929c5bb2e2f347bf67bccbea58d841fa7e1cfd5039060ed50709926fb994363

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page