1 ##############################################################################
2 #
3 # Copyright (c) 2001 Zope Corporation and Contributors. All Rights Reserved.
4 #
5 # This software is subject to the provisions of the Zope Public License,
6 # Version 2.0 (ZPL). A copy of the ZPL should accompany this distribution.
7 # THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED
8 # WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
9 # WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
10 # FOR A PARTICULAR PURPOSE
11 #
12 ##############################################################################
13 __doc__='''Batch class, for iterating over a sequence in batches
15 $Id: Batch.py,v 1.3 2004-02-11 23:55:09 richard Exp $'''
16 __docformat__ = 'restructuredtext'
17 __version__='$Revision: 1.3 $'[11:-2]
19 class LazyPrevBatch:
20 def __of__(self, parent):
21 return Batch(parent._sequence, parent._size,
22 parent.first - parent._size + parent.overlap, 0,
23 parent.orphan, parent.overlap)
25 class LazyNextBatch:
26 def __of__(self, parent):
27 try: parent._sequence[parent.end]
28 except IndexError: return None
29 return Batch(parent._sequence, parent._size,
30 parent.end - parent.overlap, 0,
31 parent.orphan, parent.overlap)
33 class LazySequenceLength:
34 def __of__(self, parent):
35 parent.sequence_length = l = len(parent._sequence)
36 return l
38 class Batch:
39 """Create a sequence batch"""
40 __allow_access_to_unprotected_subobjects__ = 1
42 previous = LazyPrevBatch()
43 next = LazyNextBatch()
44 sequence_length = LazySequenceLength()
46 def __init__(self, sequence, size, start=0, end=0,
47 orphan=0, overlap=0):
48 '''Encapsulate "sequence" in batches of "size".
50 Arguments: "start" and "end" are 0-based indexes into the
51 sequence. If the next batch would contain no more than
52 "orphan" elements, it is combined with the current batch.
53 "overlap" is the number of elements shared by adjacent
54 batches. If "size" is not specified, it is computed from
55 "start" and "end". If "size" is 0, it is the length of
56 the sequence. Failing that, it is 7.
58 Attributes: Note that the "start" attribute, unlike the
59 argument, is a 1-based index (I know, lame). "first" is the
60 0-based index. "length" is the actual number of elements in
61 the batch.
63 "sequence_length" is the length of the original, unbatched, sequence
64 '''
66 start = start + 1
68 start,end,sz = opt(start,end,size,orphan,sequence)
70 self._sequence = sequence
71 self.size = sz
72 self._size = size
73 self.start = start
74 self.end = end
75 self.orphan = orphan
76 self.overlap = overlap
77 self.first = max(start - 1, 0)
78 self.length = self.end - self.first
79 if self.first == 0:
80 self.previous = None
83 def __getitem__(self, index):
84 if index < 0:
85 if index + self.end < self.first: raise IndexError, index
86 return self._sequence[index + self.end]
88 if index >= self.length: raise IndexError, index
89 return self._sequence[index + self.first]
91 def __len__(self):
92 return self.length
94 def opt(start,end,size,orphan,sequence):
95 if size < 1:
96 if size == 0:
97 size=len(sequence)
98 elif start > 0 and end > 0 and end >= start:
99 size=end+1-start
100 else: size=7
102 if start > 0:
104 try: sequence[start-1]
105 except IndexError: start=len(sequence)
107 if end > 0:
108 if end < start: end=start
109 else:
110 end=start+size-1
111 try: sequence[end+orphan-1]
112 except IndexError: end=len(sequence)
113 elif end > 0:
114 try: sequence[end-1]
115 except IndexError: end=len(sequence)
116 start=end+1-size
117 if start - 1 < orphan: start=1
118 else:
119 start=1
120 end=start+size-1
121 try: sequence[end+orphan-1]
122 except IndexError: end=len(sequence)
123 return start,end,size