Correction crash
[auf_roundup.git] / build / lib / roundup / cgi / ZTUtils / Batch.py
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
14
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]
18
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)
24
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)
32
33 class LazySequenceLength:
34 def __of__(self, parent):
35 parent.sequence_length = l = len(parent._sequence)
36 return l
37
38 class Batch:
39 """Create a sequence batch"""
40 __allow_access_to_unprotected_subobjects__ = 1
41
42 previous = LazyPrevBatch()
43 next = LazyNextBatch()
44 sequence_length = LazySequenceLength()
45
46 def __init__(self, sequence, size, start=0, end=0,
47 orphan=0, overlap=0):
48 '''Encapsulate "sequence" in batches of "size".
49
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". Failing that, it is 7.
56
57 Attributes: Note that the "start" attribute, unlike the
58 argument, is a 1-based index (I know, lame). "first" is the
59 0-based index. "length" is the actual number of elements in
60 the batch.
61
62 "sequence_length" is the length of the original, unbatched, sequence
63 '''
64
65 start = start + 1
66
67 start,end,sz = opt(start,end,size,orphan,sequence)
68
69 self._sequence = sequence
70 self.size = sz
71 self._size = size
72 self.start = start
73 self.end = end
74 self.orphan = orphan
75 self.overlap = overlap
76 self.first = max(start - 1, 0)
77 self.length = self.end - self.first
78 if self.first == 0:
79 self.previous = None
80
81
82 def __getitem__(self, index):
83 if index < 0:
84 if index + self.end < self.first: raise IndexError, index
85 return self._sequence[index + self.end]
86
87 if index >= self.length: raise IndexError, index
88 return self._sequence[index + self.first]
89
90 def __len__(self):
91 return self.length
92
93 def opt(start,end,size,orphan,sequence):
94 if size < 1:
95 if start > 0 and end > 0 and end >= start:
96 size=end+1-start
97 else: size=7
98
99 if start > 0:
100
101 try: sequence[start-1]
102 except IndexError: start=len(sequence)
103
104 if end > 0:
105 if end < start: end=start
106 else:
107 end=start+size-1
108 try: sequence[end+orphan-1]
109 except IndexError: end=len(sequence)
110 elif end > 0:
111 try: sequence[end-1]
112 except IndexError: end=len(sequence)
113 start=end+1-size
114 if start - 1 < orphan: start=1
115 else:
116 start=1
117 end=start+size-1
118 try: sequence[end+orphan-1]
119 except IndexError: end=len(sequence)
120 return start,end,size