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.1 2002-08-30 08:25:33 richard Exp $'''
16 __version__='$Revision: 1.1 $'[11:-2]
18 from ExtensionClass import Base
20 class LazyPrevBatch(Base):
21 def __of__(self, parent):
22 return Batch(parent._sequence, parent._size,
23 parent.first - parent._size + parent.overlap, 0,
24 parent.orphan, parent.overlap)
26 class LazyNextBatch(Base):
27 def __of__(self, parent):
28 try: parent._sequence[parent.end]
29 except IndexError: return None
30 return Batch(parent._sequence, parent._size,
31 parent.end - parent.overlap, 0,
32 parent.orphan, parent.overlap)
34 class LazySequenceLength(Base):
35 def __of__(self, parent):
36 parent.sequence_length = l = len(parent._sequence)
37 return l
39 class Batch(Base):
40 """Create a sequence batch"""
41 __allow_access_to_unprotected_subobjects__ = 1
43 previous = LazyPrevBatch()
44 next = LazyNextBatch()
45 sequence_length = LazySequenceLength()
47 def __init__(self, sequence, size, start=0, end=0,
48 orphan=0, overlap=0):
49 '''Encapsulate "sequence" in batches of "size".
51 Arguments: "start" and "end" are 0-based indexes into the
52 sequence. If the next batch would contain no more than
53 "orphan" elements, it is combined with the current batch.
54 "overlap" is the number of elements shared by adjacent
55 batches. If "size" is not specified, it is computed from
56 "start" and "end". 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 start > 0 and end > 0 and end >= start:
97 size=end+1-start
98 else: size=7
100 if start > 0:
102 try: sequence[start-1]
103 except IndexError: start=len(sequence)
105 if end > 0:
106 if end < start: end=start
107 else:
108 end=start+size-1
109 try: sequence[end+orphan-1]
110 except IndexError: end=len(sequence)
111 elif end > 0:
112 try: sequence[end-1]
113 except IndexError: end=len(sequence)
114 start=end+1-size
115 if start - 1 < orphan: start=1
116 else:
117 start=1
118 end=start+size-1
119 try: sequence[end+orphan-1]
120 except IndexError: end=len(sequence)
121 return start,end,size