[Zope3-checkins] CVS: Zope3/src/zope/app/batching - __init__.py:1.1 interfaces.py:1.1 tests.py:1.1

Philipp von Weitershausen philikon at philikon.de
Tue Mar 2 12:28:46 EST 2004


Update of /cvs-repository/Zope3/src/zope/app/batching
In directory cvs.zope.org:/tmp/cvs-serv24268/batching

Added Files:
	__init__.py interfaces.py tests.py 
Log Message:
Moved all code related to batching (interfaces, impl., tests) to the
zope.app.batching package.


=== Added File Zope3/src/zope/app/batching/__init__.py ===
##############################################################################
#
# Copyright (c) 2003 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 Support

$Id: __init__.py,v 1.1 2004/03/02 17:28:45 philikon Exp $
"""
from zope.interface import implements
from zope.app.batching.interfaces import IBatch

class Batch(object):
    implements(IBatch)

    def __init__(self, list, start=0, size=20):
        self.list = list
        self.start = start
        if len(list) == 0:
            self.start = -1
        elif start >= len(list):
            raise IndexError, 'start index key out of range'
        self.size = size
        self.trueSize = size
        if start+size >= len(list):
            self.trueSize = len(list)-start
        self.end = start+self.trueSize-1

    def __len__(self):
        return self.trueSize

    def __getitem__(self, key):
        if key >= self.trueSize:
            raise IndexError, 'batch index out of range'
        return self.list[self.start+key]

    def __iter__(self): 
        return iter(self.list[self.start:self.end+1])

    def __contains__(self, item):
        return item in self.__iter__()

    def nextBatch(self):
        start = self.start + self.size
        if start >= len(self.list):
            return None
        return Batch(self.list, start, self.size)
    
    def prevBatch(self):
        start = self.start - self.size
        if start < 0:
            return None
        return Batch(self.list, start, self.size)

    def first(self):
        return self.list[self.start]

    def last(self):
        return self.list[self.end]

    def total(self):
        return len(self.list)

    def startNumber(self):
        return self.start+1

    def endNumber(self):
        return self.end+1


=== Added File Zope3/src/zope/app/batching/interfaces.py ===
##############################################################################
#
# Copyright (c) 2003 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 Support

$Id: interfaces.py,v 1.1 2004/03/02 17:28:45 philikon Exp $
"""
from zope.interface.common.mapping import IItemMapping

class IBatch(IItemMapping):
    """A Batch represents a sub-list of the full enumeration.

    The Batch constructor takes a list (or any list-like object) of elements,
    a starting index and the size of the batch. From this information all
    other values are calculated.
    """

    def __len__():
        """Return the length of the batch. This might be different than the
        passed in batch size, since we could be at the end of the list and
        there are not enough elements left to fill the batch completely."""

    def __iter__(): 
        """Creates an iterator for the contents of the batch (not the entire
        list)."""

    def __contains__(key):
        """Checks whether the key (in our case an index) exists."""

    def nextBatch(self):
        """Return the next batch. If there is no next batch, return None."""
    
    def prevBatch(self):
        """Return the previous batch. If there is no previous batch, return
        None."""

    def first(self):
        """Return the first element of the batch."""

    def last(self):
        """Return the last element of the batch."""

    def total(self):
        """Return the length of the list (not the batch)."""

    def startNumber(self):
        """Give the start **number** of the batch, which is 1 more than the
        start index passed in."""

    def endNumber(self):
        """Give the end **number** of the batch, which is 1 more than the
        final index."""


=== Added File Zope3/src/zope/app/batching/tests.py ===
##############################################################################
#
# Copyright (c) 2003 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.
#
##############################################################################
"""Bug Tracker Mail Subscription and Mailer Tests

$Id: tests.py,v 1.1 2004/03/02 17:28:45 philikon Exp $
"""
import unittest

from zope.app.batching import Batch
from zope.app.batching.interfaces import IBatch

class BatchTest(unittest.TestCase):

    def getData(self):
        return ['one', 'two', 'three', 'four', 'five', 'six',
                'seven', 'eight', 'nine', 'ten']

    def test_Interface(self):
        self.failUnless(IBatch.isImplementedBy(Batch([], 0, 0)))

    def test_constructor(self):
        batch = Batch(self.getData(), 9, 3)
        self.assertRaises(IndexError, Batch, self.getData(), start=10, size=3)

    def test__len__(self):
        batch = Batch(self.getData(), 0, 3)
        self.assertEqual(len(batch), 3)
        batch = Batch(self.getData(), 9, 3)
        self.assertEqual(len(batch), 1)

    def test__getitem__(self):
        batch = Batch(self.getData(), 0, 3)
        self.assertEqual(batch[0], 'one')
        self.assertEqual(batch[1], 'two')
        self.assertEqual(batch[2], 'three')
        batch = Batch(self.getData(), 3, 3)
        self.assertEqual(batch[0], 'four')
        self.assertEqual(batch[1], 'five')
        self.assertEqual(batch[2], 'six')
        batch = Batch(self.getData(), 9, 3)
        self.assertRaises(IndexError, batch.__getitem__, 3)
        
    def test__iter__(self):
        batch = Batch(self.getData(), 0, 3)
        self.assertEqual(list(iter(batch)), ['one', 'two', 'three'])
        batch = Batch(self.getData(), 9, 3)
        self.assertEqual(list(iter(batch)), ['ten'])

    def test__contains__(self):
        batch = Batch(self.getData(), 0, 3)
        self.assert_(batch.__contains__('one'))
        self.assert_(batch.__contains__('two'))
        self.assert_(batch.__contains__('three'))
        self.assert_(not batch.__contains__('four'))
        batch = Batch(self.getData(), 6, 3)
        self.assert_(not batch.__contains__('one'))
        self.assert_(batch.__contains__('seven'))
        self.assert_(not batch.__contains__('ten'))

    def test_nextBatch(self):
        next = Batch(self.getData(), 0, 3).nextBatch()
        self.assertEqual(list(iter(next)), ['four', 'five', 'six'])
        nextnext = next.nextBatch()
        self.assertEqual(list(iter(nextnext)), ['seven', 'eight', 'nine'])
        next = Batch(self.getData(), 9, 3).nextBatch()
        self.assertEqual(next, None)

    def test_prevBatch(self):
        prev = Batch(self.getData(), 9, 3).prevBatch()
        self.assertEqual(list(iter(prev)), ['seven', 'eight', 'nine'])
        prevprev = prev.prevBatch()
        self.assertEqual(list(iter(prevprev)), ['four', 'five', 'six'])
        prev = Batch(self.getData(), 0, 3).prevBatch()
        self.assertEqual(prev, None)

    def test_batchRoundTrip(self):
        batch = Batch(self.getData(), 0, 3).nextBatch()
        self.assertEqual(list(iter(batch.nextBatch().prevBatch())),
                         list(iter(batch)))

    def test_first_last(self):
        batch = Batch(self.getData(), 0, 3)
        self.assertEqual(batch.first(), 'one')
        self.assertEqual(batch.last(), 'three')
        batch = Batch(self.getData(), 9, 3)
        self.assertEqual(batch.first(), 'ten')
        self.assertEqual(batch.last(), 'ten')
        
    def test_total(self):
        batch = Batch(self.getData(), 0, 3)
        self.assertEqual(batch.total(), 10)
        batch = Batch(self.getData(), 6, 3)
        self.assertEqual(batch.total(), 10)
    
    def test_startNumber(self):
        batch = Batch(self.getData(), 0, 3)
        self.assertEqual(batch.startNumber(), 1)
        batch = Batch(self.getData(), 9, 3)
        self.assertEqual(batch.startNumber(), 10)

    def test_endNumber(self):
        batch = Batch(self.getData(), 0, 3)
        self.assertEqual(batch.endNumber(), 3)
        batch = Batch(self.getData(), 9, 3)
        self.assertEqual(batch.endNumber(), 10)
        

def test_suite():
    return unittest.TestSuite((
        unittest.makeSuite(BatchTest),
        ))

if __name__ == '__main__':
    unittest.main()




More information about the Zope3-Checkins mailing list