[Checkins] SVN: z3c.batching/tags/1.1.0/ Tag for version 1.1.0
Christophe Combelles
ccomb at free.fr
Sun Oct 12 10:46:39 EDT 2008
Log message for revision 92078:
Tag for version 1.1.0
Changed:
A z3c.batching/tags/1.1.0/
D z3c.batching/tags/1.1.0/CHANGES.txt
A z3c.batching/tags/1.1.0/CHANGES.txt
D z3c.batching/tags/1.1.0/setup.py
A z3c.batching/tags/1.1.0/setup.py
D z3c.batching/tags/1.1.0/src/z3c/batching/README.txt
A z3c.batching/tags/1.1.0/src/z3c/batching/README.txt
D z3c.batching/tags/1.1.0/src/z3c/batching/batch.py
A z3c.batching/tags/1.1.0/src/z3c/batching/batch.py
D z3c.batching/tags/1.1.0/src/z3c/batching/tests.py
A z3c.batching/tags/1.1.0/src/z3c/batching/tests.py
-=-
Copied: z3c.batching/tags/1.1.0 (from rev 92030, z3c.batching/trunk)
Property changes on: z3c.batching/tags/1.1.0
___________________________________________________________________
Name: svn:mergeinfo
+
Deleted: z3c.batching/tags/1.1.0/CHANGES.txt
===================================================================
--- z3c.batching/trunk/CHANGES.txt 2008-10-10 23:25:42 UTC (rev 92030)
+++ z3c.batching/tags/1.1.0/CHANGES.txt 2008-10-12 14:46:38 UTC (rev 92078)
@@ -1,14 +0,0 @@
-=======
-CHANGES
-=======
-
-1.0.1 (2008-09-09)
-------------------
-
-- Fixed bug with batches slicing.
-
-
-1.0.0 (2008-02-18)
-------------------
-
-- Initial release.
Copied: z3c.batching/tags/1.1.0/CHANGES.txt (from rev 92077, z3c.batching/trunk/CHANGES.txt)
===================================================================
--- z3c.batching/tags/1.1.0/CHANGES.txt (rev 0)
+++ z3c.batching/tags/1.1.0/CHANGES.txt 2008-10-12 14:46:38 UTC (rev 92078)
@@ -0,0 +1,22 @@
+=======
+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.
Deleted: z3c.batching/tags/1.1.0/setup.py
===================================================================
--- z3c.batching/trunk/setup.py 2008-10-10 23:25:42 UTC (rev 92030)
+++ z3c.batching/tags/1.1.0/setup.py 2008-10-12 14:46:38 UTC (rev 92078)
@@ -1,61 +0,0 @@
-##############################################################################
-#
-# Copyright (c) 2007 Zope Corporation and Contributors.
-# All Rights Reserved.
-#
-# This software is subject to the provisions of the Zope Public License,
-# Version 2.1 (ZPL). A copy of the ZPL should accompany this distribution.
-# THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED
-# WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
-# WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
-# FOR A PARTICULAR PURPOSE.
-#
-##############################################################################
-"""Setup for z3c.batching package
-
-$Id$
-"""
-import os
-from setuptools import setup, find_packages
-
-def read(*rnames):
- return open(os.path.join(os.path.dirname(__file__), *rnames)).read()
-
-setup(name='z3c.batching',
- version = '1.1.0dev',
- author='Zope Corporation and Contributors',
- author_email='zope3-dev at zope.org',
- description='Batching',
- long_description=(
- read('README.txt')
- + '\n\n' +
- 'Detailed Dcoumentation\n' +
- '======================\n'
- + '\n\n' +
- read('src', 'z3c', 'batching', 'README.txt')
- + '\n\n' +
- read('CHANGES.txt')
- ),
- keywords = "zope3 batching",
- classifiers = [
- 'Development Status :: 5 - Production/Stable',
- 'Environment :: Web Environment',
- 'Intended Audience :: Developers',
- 'License :: OSI Approved :: Zope Public License',
- 'Programming Language :: Python',
- 'Natural Language :: English',
- 'Operating System :: OS Independent',
- 'Topic :: Internet :: WWW/HTTP',
- 'Framework :: Zope3'],
- url='http://cheeseshop.python.org/pypi/z3c.batching',
- license='ZPL 2.1',
- packages=find_packages('src'),
- package_dir = {'': 'src'},
- namespace_packages=['z3c'],
- install_requires = ['setuptools',
- 'zope.interface',
- 'zope.schema',
- ],
- include_package_data = True,
- zip_safe = False,
- )
Copied: z3c.batching/tags/1.1.0/setup.py (from rev 92077, z3c.batching/trunk/setup.py)
===================================================================
--- z3c.batching/tags/1.1.0/setup.py (rev 0)
+++ z3c.batching/tags/1.1.0/setup.py 2008-10-12 14:46:38 UTC (rev 92078)
@@ -0,0 +1,62 @@
+##############################################################################
+#
+# Copyright (c) 2007 Zope Corporation and Contributors.
+# All Rights Reserved.
+#
+# This software is subject to the provisions of the Zope Public License,
+# Version 2.1 (ZPL). A copy of the ZPL should accompany this distribution.
+# THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED
+# WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
+# WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
+# FOR A PARTICULAR PURPOSE.
+#
+##############################################################################
+"""Setup for z3c.batching package
+
+$Id$
+"""
+import os
+from setuptools import setup, find_packages
+
+def read(*rnames):
+ return open(os.path.join(os.path.dirname(__file__), *rnames)).read()
+
+setup(name='z3c.batching',
+ version = '1.1.0',
+ author='Zope Corporation and Contributors',
+ author_email='zope-dev at zope.org',
+ description='Batching',
+ long_description=(
+ read('README.txt')
+ + '\n\n'
+ + '======================\n'
+ + 'Detailed Documentation\n'
+ + '======================\n'
+ + '\n\n'
+ + read('src', 'z3c', 'batching', 'README.txt')
+ + '\n\n'
+ + read('CHANGES.txt')
+ ),
+ keywords = "zope3 batching",
+ classifiers = [
+ 'Development Status :: 5 - Production/Stable',
+ 'Environment :: Web Environment',
+ 'Intended Audience :: Developers',
+ 'License :: OSI Approved :: Zope Public License',
+ 'Programming Language :: Python',
+ 'Natural Language :: English',
+ 'Operating System :: OS Independent',
+ 'Topic :: Internet :: WWW/HTTP',
+ 'Framework :: Zope3'],
+ url='http://pypi.python.org/pypi/z3c.batching',
+ license='ZPL 2.1',
+ packages=find_packages('src'),
+ package_dir = {'': 'src'},
+ namespace_packages=['z3c'],
+ install_requires = ['setuptools',
+ 'zope.interface',
+ 'zope.schema',
+ ],
+ include_package_data = True,
+ zip_safe = False,
+ )
Deleted: z3c.batching/tags/1.1.0/src/z3c/batching/README.txt
===================================================================
--- z3c.batching/trunk/src/z3c/batching/README.txt 2008-10-10 23:25:42 UTC (rev 92030)
+++ z3c.batching/tags/1.1.0/src/z3c/batching/README.txt 2008-10-12 14:46:38 UTC (rev 92078)
@@ -1,231 +0,0 @@
-===============
-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
-
- >>> from z3c.batching.batch import Batch
- >>> batch = Batch([], size=3)
- >>> len(batch)
- 0
- >>> batch.firstElement
- Traceback (most recent call last):
- ...
- IndexError: ...
-
- >>> batch.lastElement
- Traceback (most recent call last):
- ...
- IndexError: ...
-
- >>> batch[0]
- Traceback (most recent call last):
- ...
- IndexError: ...
-
- >>> batch.next is None
- True
-
- >>> batch.previous is None
- True
-
-
- >>> 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:
-
- >>> 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:
-
- >>> list(batch)
- ['one', 'two', 'three']
-
-The start index is commonly specified in the constructor though:
-
- >>> batch = Batch(sequence, start=6, size=3)
- >>> 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:
-
- >>> 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:
-
- >>> len(batch)
- 3
-
-Note that the length returns the true size of the batch, not the size we asked
-for:
-
- >>> len(Batch(sequence, start=12, size=3))
- 1
-
-You can also get an element by index, which is relative to the batch:
-
- >>> batch[0]
- 'seven'
- >>> batch[1]
- 'eight'
- >>> batch[2]
- 'nine'
-
-Slicing
-
- >>> batch[:1]
- ['seven']
-
- >>> batch[1:2]
- ['eight']
-
- >>> batch[1:]
- ['eight', 'nine']
-
- >>> batch[:]
- ['seven', 'eight', 'nine']
-
- >>> batch[10:]
- []
-
-
-If you ask for index that is out of range, an index error is raised:
-
- >>> batch[3]
- Traceback (most recent call last):
- ...
- IndexError: batch index out of range
-
-You can also iterate through the batch:
-
- >>> iterator = iter(batch)
- >>> iterator.next()
- 'seven'
- >>> iterator.next()
- 'eight'
- >>> iterator.next()
- 'nine'
-
-Batch also implement some of IReadSequence interface
-
- >>> 'eight' in batch
- True
-
- >>> 'ten' in batch
- False
-
- >>> batch == Batch(sequence, start=6, size=3)
- True
-
- >>> batch != Batch(sequence, start=6, size=3)
- False
-
- >>> 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:
-
- >>> batch.start
- 6
- >>> batch.size
- 3
-
-The end index of the batch is immediately computed:
-
- >>> batch.end
- 8
-
-The UI often requires that the number of the btach and the total number of
-batches is computed:
-
- >>> batch.number
- 3
- >>> batch.total
- 5
-
-You can also ask for the next batch:
-
- >>> batch.next
- <Batch start=9, size=3>
-
-If the current batch is the last one, the next batch is None:
-
- >>> Batch(sequence, start=12, size=3).next is None
- True
-
-The previous batch shows the previous batch:
-
- >>> batch.previous
- <Batch start=3, size=3>
-
-If the current batch is the first one, the previous batch is None:
-
- >>> 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:
-
- >>> batch.firstElement
- 'seven'
-
- >>> batch.lastElement
- 'nine'
-
-
-Total batches
-
- >>> batch = Batch(sequence[:-1], size=3)
- >>> batch.total
- 4
-
-We can get access to all batches
-
- >>> len(batch.batches)
- 4
-
- >>> batch.batches[0]
- <Batch start=0, size=3>
-
- >>> batch.batches[3]
- <Batch start=9, size=3>
-
- >>> batch.batches[4]
- Traceback (most recent call last):
- ...
- IndexError: ...
-
- >>> batch.batches[-1]
- <Batch start=9, size=3>
-
- >>> batch.batches[-2]
- <Batch start=6, size=3>
-
-Slicing
-
- >>> batch.batches[:1]
- [<Batch start=0, size=3>]
-
- >>> batch.batches[:]
- [<Batch start=0, size=3>, <Batch start=3, size=3>, <Batch start=6, size=3>, <Batch start=9, size=3>]
-
- >>> batch.batches[1:2]
- [<Batch start=3, size=3>]
-
- >>> batch.batches[1:]
- [<Batch start=3, size=3>, <Batch start=6, size=3>, <Batch start=9, size=3>]
-
- >>> batch.batches[10:]
- []
Copied: z3c.batching/tags/1.1.0/src/z3c/batching/README.txt (from rev 92072, z3c.batching/trunk/src/z3c/batching/README.txt)
===================================================================
--- z3c.batching/tags/1.1.0/src/z3c/batching/README.txt (rev 0)
+++ z3c.batching/tags/1.1.0/src/z3c/batching/README.txt 2008-10-12 14:46:38 UTC (rev 92078)
@@ -0,0 +1,257 @@
+
+.. 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:
+
+ >>> from z3c.batching.batch import Batch
+ >>> batch = Batch([], size=3)
+ >>> len(batch)
+ 0
+ >>> batch.firstElement
+ Traceback (most recent call last):
+ ...
+ IndexError: ...
+
+ >>> batch.lastElement
+ Traceback (most recent call last):
+ ...
+ IndexError: ...
+
+ >>> batch[0]
+ Traceback (most recent call last):
+ ...
+ IndexError: ...
+
+ >>> batch.next is None
+ True
+
+ >>> batch.previous is None
+ True
+
+
+ >>> 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:
+
+ >>> 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:
+
+ >>> list(batch)
+ ['one', 'two', 'three']
+
+The start index is commonly specified in the constructor though:
+
+ >>> batch = Batch(sequence, start=6, size=3)
+ >>> 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:
+
+ >>> 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:
+
+ >>> len(batch)
+ 3
+
+Note that the length returns the true size of the batch, not the size we asked
+for:
+
+ >>> len(Batch(sequence, start=12, size=3))
+ 1
+
+You can also get an element by index, which is relative to the batch:
+
+ >>> batch[0]
+ 'seven'
+ >>> batch[1]
+ 'eight'
+ >>> batch[2]
+ 'nine'
+
+Slicing:
+
+ >>> batch[:1]
+ ['seven']
+
+ >>> batch[1:2]
+ ['eight']
+
+ >>> batch[1:]
+ ['eight', 'nine']
+
+ >>> batch[:]
+ ['seven', 'eight', 'nine']
+
+ >>> batch[10:]
+ []
+
+
+If you ask for index that is out of range, an index error is raised:
+
+ >>> batch[3]
+ Traceback (most recent call last):
+ ...
+ IndexError: batch index out of range
+
+You can also iterate through the batch:
+
+ >>> iterator = iter(batch)
+ >>> iterator.next()
+ 'seven'
+ >>> iterator.next()
+ 'eight'
+ >>> iterator.next()
+ 'nine'
+
+Batch also implement some of IReadSequence interface:
+
+ >>> 'eight' in batch
+ True
+
+ >>> 'ten' in batch
+ False
+
+ >>> batch == Batch(sequence, start=6, size=3)
+ True
+
+ >>> batch != Batch(sequence, start=6, size=3)
+ False
+
+ >>> 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:
+
+ >>> batch.start
+ 6
+ >>> batch.size
+ 3
+
+The end index of the batch is immediately computed:
+
+ >>> batch.end
+ 8
+
+The UI often requires that the number of the batch and the total number of
+batches is computed:
+
+ >>> batch.number
+ 3
+ >>> batch.total
+ 5
+
+You can also ask for the next batch:
+
+ >>> batch.next
+ <Batch start=9, size=3>
+
+If the current batch is the last one, the next batch is None:
+
+ >>> Batch(sequence, start=12, size=3).next is None
+ True
+
+The previous batch shows the previous batch:
+
+ >>> batch.previous
+ <Batch start=3, size=3>
+
+If the current batch is the first one, the previous batch is None:
+
+ >>> 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:
+
+ >>> batch.firstElement
+ 'seven'
+
+ >>> batch.lastElement
+ 'nine'
+
+
+Total batches:
+
+ >>> batch = Batch(sequence[:-1], size=3)
+ >>> batch.total
+ 4
+
+We can have access to all batches:
+
+ >>> len(batch.batches)
+ 4
+
+ >>> batch.batches[0]
+ <Batch start=0, size=3>
+
+ >>> batch.batches[3]
+ <Batch start=9, size=3>
+
+ >>> batch.batches[4]
+ Traceback (most recent call last):
+ ...
+ IndexError: ...
+
+ >>> batch.batches[-1]
+ <Batch start=9, size=3>
+
+ >>> batch.batches[-2]
+ <Batch start=6, size=3>
+
+Slicing:
+
+ >>> batch.batches[:1]
+ [<Batch start=0, size=3>]
+
+ >>> batch.batches[:]
+ [<Batch start=0, size=3>, <Batch start=3, size=3>, <Batch start=6, size=3>, <Batch start=9, size=3>]
+
+ >>> batch.batches[1:2]
+ [<Batch start=3, size=3>]
+
+ >>> batch.batches[1:]
+ [<Batch start=3, size=3>, <Batch start=6, size=3>, <Batch start=9, size=3>]
+
+ >>> batch.batches[10:]
+ []
+
+ >>> batch.batches[2:50]
+ [<Batch start=6, size=3>, <Batch start=9, size=3>]
+
+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):
+
+ >>> 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:
+
+ >>> from z3c.batching.batch import first_neighbours_last
+ >>> 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)
+
Deleted: z3c.batching/tags/1.1.0/src/z3c/batching/batch.py
===================================================================
--- z3c.batching/trunk/src/z3c/batching/batch.py 2008-10-10 23:25:42 UTC (rev 92030)
+++ z3c.batching/tags/1.1.0/src/z3c/batching/batch.py 2008-10-12 14:46:38 UTC (rev 92078)
@@ -1,179 +0,0 @@
-##############################################################################
-#
-# Copyright (c) 2003-2007 Zope Corporation and Contributors.
-# All Rights Reserved.
-#
-# This software is subject to the provisions of the Zope Public License,
-# Version 2.0 (ZPL). A copy of the ZPL should accompany this distribution.
-# THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED
-# WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
-# WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
-# FOR A PARTICULAR PURPOSE.
-#
-##############################################################################
-"""Batching Implementation
-
-$Id$
-"""
-__docformat__ = 'restructuredtext'
-
-import zope.interface
-from zope.schema.fieldproperty import FieldProperty
-from zope.interface.common.sequence import IFiniteSequence
-
-from z3c.batching import interfaces
-
-
-class Batch(object):
- zope.interface.implements(interfaces.IBatch)
-
- start = FieldProperty(interfaces.IBatch['start'])
- size = FieldProperty(interfaces.IBatch['size'])
- end = FieldProperty(interfaces.IBatch['end'])
-
- def __init__(self, sequence, start=0, size=20, batches=None):
- self.sequence = sequence
-
- length = len(sequence)
- self._length = length
-
- # See interfaces.IBatch
- self.start = start
- if length == 0:
- self.start = -1
- elif start >= length:
- raise IndexError('start index key out of range')
-
- # See interfaces.IBatch
- self.size = size
- self._trueSize = size
-
- if start + size >= length:
- self._trueSize = length - start
-
- # See interfaces.IBatch
- if length == 0:
- self.end = -1
- else:
- self.end = start + self._trueSize - 1
-
- if batches is None:
- batches = Batches(self)
-
- self.batches = batches
-
- @property
- def index(self):
- return self.start / self.size
-
- @property
- def number(self):
- """See interfaces.IBatch"""
- return self.start / self.size + 1
-
- @property
- def total(self):
- """See interfaces.IBatch"""
- total = self._length / self.size
- if self._length % self.size:
- total += 1
- return total
-
- @property
- def next(self):
- try:
- return self.batches[self.index + 1]
- except IndexError:
- return None
-
- @property
- def previous(self):
- idx = self.index - 1
- if idx >= 0:
- return self.batches[idx]
- return None
-
- @property
- def firstElement(self):
- """See interfaces.IBatch"""
- return self.sequence[self.start]
-
- @property
- def lastElement(self):
- """See interfaces.IBatch"""
- return self.sequence[self.end]
-
- def __getitem__(self, key):
- """See zope.interface.common.sequence.IMinimalSequence"""
- if key >= self._trueSize:
- raise IndexError('batch index out of range')
- return self.sequence[self.start+key]
-
- def __iter__(self):
- """See zope.interface.common.sequence.IMinimalSequence"""
- return iter(self.sequence[self.start: self.end+1])
-
- def __len__(self):
- """See zope.interface.common.sequence.IFiniteSequence"""
- return self._trueSize
-
- def __contains__(self, item):
- for i in self:
- if item == i:
- return True
- else:
- return False
-
- def __getslice__(self, i, j):
- if j > self.end:
- j = self._trueSize
-
- return [self[idx] for idx in range(i, j)]
-
- def __eq__(self, other):
- return ((self.size, self.start, self.sequence) ==
- (other.size, other.start, other.sequence))
-
- def __ne__(self, other):
- return not self.__eq__(other)
-
- def __nonzero__(self):
- return self._trueSize != 0
-
- def __repr__(self):
- return '<%s start=%i, size=%i>' % (
- self.__class__.__name__, self.start, self.size)
-
-
-class Batches(object):
- zope.interface.implements(IFiniteSequence)
-
- def __init__(self, batch):
- self.size = batch.size
- self.total = batch.total
- self.sequence = batch.sequence
-
- self._batches = {batch.index: batch}
-
- def __len__(self):
- return self.total
-
- def __getitem__(self, key):
- if key not in self._batches:
- if key < 0:
- key = self.total + key
-
- batch = Batch(
- self.sequence, key*self.size, self.size, self)
- self._batches[batch.index] = batch
-
- try:
- return self._batches[key]
- except KeyError:
- raise IndexError(key)
-
- def __getslice__(self, i, j):
- if j > self.total:
- j = self.total-1
-
- return [self[idx] for idx in range(i, j)]
Copied: z3c.batching/tags/1.1.0/src/z3c/batching/batch.py (from rev 92073, z3c.batching/trunk/src/z3c/batching/batch.py)
===================================================================
--- z3c.batching/tags/1.1.0/src/z3c/batching/batch.py (rev 0)
+++ z3c.batching/tags/1.1.0/src/z3c/batching/batch.py 2008-10-12 14:46:38 UTC (rev 92078)
@@ -0,0 +1,306 @@
+##############################################################################
+#
+# Copyright (c) 2003-2007 Zope Corporation and Contributors.
+# All Rights Reserved.
+#
+# This software is subject to the provisions of the Zope Public License,
+# Version 2.0 (ZPL). A copy of the ZPL should accompany this distribution.
+# THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED
+# WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
+# WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
+# FOR A PARTICULAR PURPOSE.
+#
+##############################################################################
+"""Batching Implementation
+
+$Id$
+"""
+__docformat__ = 'restructuredtext'
+
+import zope.interface
+from zope.schema.fieldproperty import FieldProperty
+from zope.interface.common.sequence import IFiniteSequence
+
+from z3c.batching import interfaces
+
+
+class Batch(object):
+ """Batch implementation. See IBatch"""
+
+ zope.interface.implements(interfaces.IBatch)
+
+ start = FieldProperty(interfaces.IBatch['start'])
+ size = FieldProperty(interfaces.IBatch['size'])
+ end = FieldProperty(interfaces.IBatch['end'])
+
+ def __init__(self, sequence, start=0, size=20, batches=None):
+ self.sequence = sequence
+
+ length = len(sequence)
+ self._length = length
+
+ # See interfaces.IBatch
+ self.start = start
+ if length == 0:
+ self.start = -1
+ elif start >= length:
+ raise IndexError('start index key out of range')
+
+ # See interfaces.IBatch
+ self.size = size
+ self._trueSize = size
+
+ if start + size >= length:
+ self._trueSize = length - start
+
+ # See interfaces.IBatch
+ if length == 0:
+ self.end = -1
+ else:
+ self.end = start + self._trueSize - 1
+
+ if batches is None:
+ batches = Batches(self)
+
+ self.batches = batches
+
+ @property
+ def index(self):
+ return self.start / self.size
+
+ @property
+ def number(self):
+ """See interfaces.IBatch"""
+ return self.start / self.size + 1
+
+ @property
+ def total(self):
+ """See interfaces.IBatch"""
+ total = self._length / self.size
+ if self._length % self.size:
+ total += 1
+ return total
+
+ @property
+ def next(self):
+ try:
+ return self.batches[self.index + 1]
+ except IndexError:
+ return None
+
+ @property
+ def previous(self):
+ idx = self.index - 1
+ if idx >= 0:
+ return self.batches[idx]
+ return None
+
+ @property
+ def firstElement(self):
+ """See interfaces.IBatch"""
+ return self.sequence[self.start]
+
+ @property
+ def lastElement(self):
+ """See interfaces.IBatch"""
+ return self.sequence[self.end]
+
+ def __getitem__(self, key):
+ """See zope.interface.common.sequence.IMinimalSequence"""
+ if key >= self._trueSize:
+ raise IndexError('batch index out of range')
+ return self.sequence[self.start+key]
+
+ def __iter__(self):
+ """See zope.interface.common.sequence.IMinimalSequence"""
+ return iter(self.sequence[self.start: self.end+1])
+
+ def __len__(self):
+ """See zope.interface.common.sequence.IFiniteSequence"""
+ return self._trueSize
+
+ def __contains__(self, item):
+ for i in self:
+ if item == i:
+ return True
+ else:
+ return False
+
+ def __getslice__(self, i, j):
+ if j > self.end:
+ j = self._trueSize
+
+ return [self[idx] for idx in range(i, j)]
+
+ def __eq__(self, other):
+ return ((self.size, self.start, self.sequence) ==
+ (other.size, other.start, other.sequence))
+
+ def __ne__(self, other):
+ return not self.__eq__(other)
+
+ def __nonzero__(self):
+ return self._trueSize != 0
+
+ def __repr__(self):
+ return '<%s start=%i, size=%i>' % (
+ self.__class__.__name__, self.start, self.size)
+
+
+class Batches(object):
+ """A sequence object representing all the batches.
+ Used by a Batch.
+ """
+
+ zope.interface.implements(IFiniteSequence)
+
+ def __init__(self, batch):
+ self.size = batch.size
+ self.total = batch.total
+ self.sequence = batch.sequence
+
+ self._batches = {batch.index: batch}
+
+ def __len__(self):
+ return self.total
+
+ def __getitem__(self, key):
+ if key not in self._batches:
+ if key < 0:
+ key = self.total + key
+
+ batch = Batch(
+ self.sequence, key*self.size, self.size, self)
+ self._batches[batch.index] = batch
+
+ try:
+ return self._batches[key]
+ except KeyError:
+ raise IndexError(key)
+
+ def __getslice__(self, i, j):
+ j = min(j, self.total)
+ return [self[idx] for idx in range(i, j)]
+
+
+def first_neighbours_last(batches, currentBatchIdx, nb_left, nb_right):
+ """Build a sublist from a large batch list.
+
+ This is used to display batch links for a large table.
+
+ arguments:
+ * batches: a large sequence (may be a batches as well)
+ * currentBatchIdx: index of the current batch or item
+ * nb_left: number of neighbours before the current batch
+ * 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 z3c.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
+ firstIdx = 0
+ lastIdx = len(batches) - 1
+ assert(currentBatchIdx >= 0 and currentBatchIdx <= lastIdx)
+ assert(nb_left >= 0 and nb_right >= 0)
+ prevIdx = currentBatchIdx - nb_left
+ nextIdx = currentBatchIdx + 1
+ firstBatch = batches[0]
+ lastBatch = batches[len(batches)-1]
+
+ # add first batch
+ if firstIdx < currentBatchIdx:
+ sublist.append(firstBatch)
+
+ # there must probably be space
+ if firstIdx + 1 < prevIdx:
+ # we skip batches between first batch and first previous batch
+ sublist.append(None)
+
+ # add previous batches
+ for i in range(prevIdx, prevIdx + nb_left):
+ if firstIdx < i:
+ # append previous batches
+ sublist.append(batches[i])
+
+ # add current batch
+ sublist.append(batches[currentBatchIdx])
+
+ # add next batches
+ for i in range(nextIdx, nextIdx + nb_right):
+ if i < lastIdx:
+ # append previous batch
+ sublist.append(batches[i])
+
+ # there must probably be space
+ if nextIdx + nb_right < lastIdx:
+ # we skip batches between last batch and last next batch
+ sublist.append(None)
+
+ # add last batch
+ if currentBatchIdx < lastIdx:
+ sublist.append(lastBatch)
+ return sublist
+
Deleted: z3c.batching/tags/1.1.0/src/z3c/batching/tests.py
===================================================================
--- z3c.batching/trunk/src/z3c/batching/tests.py 2008-10-10 23:25:42 UTC (rev 92030)
+++ z3c.batching/tags/1.1.0/src/z3c/batching/tests.py 2008-10-12 14:46:38 UTC (rev 92078)
@@ -1,29 +0,0 @@
-##############################################################################
-#
-# Copyright (c) 2006 Lovely Systems and Contributors.
-# All Rights Reserved.
-#
-# This software is subject to the provisions of the Zope Public License,
-# Version 2.1 (ZPL). A copy of the ZPL should accompany this distribution.
-# THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED
-# WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
-# WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
-# FOR A PARTICULAR PURPOSE.
-#
-##############################################################################
-"""Tag test setup
-
-$Id$
-"""
-__docformat__ = "reStructuredText"
-
-import doctest, unittest
-
-
-def test_suite():
-
- return unittest.TestSuite((
- doctest.DocFileSuite(
- 'README.txt',
- optionflags=doctest.NORMALIZE_WHITESPACE|doctest.ELLIPSIS),
- ))
Copied: z3c.batching/tags/1.1.0/src/z3c/batching/tests.py (from rev 92061, z3c.batching/trunk/src/z3c/batching/tests.py)
===================================================================
--- z3c.batching/tags/1.1.0/src/z3c/batching/tests.py (rev 0)
+++ z3c.batching/tags/1.1.0/src/z3c/batching/tests.py 2008-10-12 14:46:38 UTC (rev 92078)
@@ -0,0 +1,33 @@
+##############################################################################
+#
+# Copyright (c) 2006 Lovely Systems and Contributors.
+# All Rights Reserved.
+#
+# This software is subject to the provisions of the Zope Public License,
+# Version 2.1 (ZPL). A copy of the ZPL should accompany this distribution.
+# THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED
+# WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
+# WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
+# FOR A PARTICULAR PURPOSE.
+#
+##############################################################################
+"""Tag test setup
+
+$Id$
+"""
+__docformat__ = "reStructuredText"
+
+import doctest, unittest
+from z3c.batching import batch
+
+def test_suite():
+
+ return unittest.TestSuite((
+ doctest.DocFileSuite(
+ 'README.txt',
+ optionflags=doctest.NORMALIZE_WHITESPACE|doctest.ELLIPSIS
+ ),
+ doctest.DocTestSuite(batch,
+ optionflags=doctest.NORMALIZE_WHITESPACE|doctest.ELLIPSIS
+ )
+ ))
More information about the Checkins
mailing list