Mercurial > p > roundup > code
annotate ZTUtils/Batch.py @ 1029:c3e391d9c4e9
more FieldStorage fun
| author | Richard Jones <richard@users.sourceforge.net> |
|---|---|
| date | Tue, 03 Sep 2002 07:42:38 +0000 |
| parents | 7fe79c67aaa9 |
| children |
| rev | line source |
|---|---|
|
983
7fe79c67aaa9
Adding ZTUtils to the dist
Richard Jones <richard@users.sourceforge.net>
parents:
diff
changeset
|
1 ############################################################################## |
|
7fe79c67aaa9
Adding ZTUtils to the dist
Richard Jones <richard@users.sourceforge.net>
parents:
diff
changeset
|
2 # |
|
7fe79c67aaa9
Adding ZTUtils to the dist
Richard Jones <richard@users.sourceforge.net>
parents:
diff
changeset
|
3 # Copyright (c) 2001 Zope Corporation and Contributors. All Rights Reserved. |
|
7fe79c67aaa9
Adding ZTUtils to the dist
Richard Jones <richard@users.sourceforge.net>
parents:
diff
changeset
|
4 # |
|
7fe79c67aaa9
Adding ZTUtils to the dist
Richard Jones <richard@users.sourceforge.net>
parents:
diff
changeset
|
5 # This software is subject to the provisions of the Zope Public License, |
|
7fe79c67aaa9
Adding ZTUtils to the dist
Richard Jones <richard@users.sourceforge.net>
parents:
diff
changeset
|
6 # Version 2.0 (ZPL). A copy of the ZPL should accompany this distribution. |
|
7fe79c67aaa9
Adding ZTUtils to the dist
Richard Jones <richard@users.sourceforge.net>
parents:
diff
changeset
|
7 # THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED |
|
7fe79c67aaa9
Adding ZTUtils to the dist
Richard Jones <richard@users.sourceforge.net>
parents:
diff
changeset
|
8 # WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED |
|
7fe79c67aaa9
Adding ZTUtils to the dist
Richard Jones <richard@users.sourceforge.net>
parents:
diff
changeset
|
9 # WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS |
|
7fe79c67aaa9
Adding ZTUtils to the dist
Richard Jones <richard@users.sourceforge.net>
parents:
diff
changeset
|
10 # FOR A PARTICULAR PURPOSE |
|
7fe79c67aaa9
Adding ZTUtils to the dist
Richard Jones <richard@users.sourceforge.net>
parents:
diff
changeset
|
11 # |
|
7fe79c67aaa9
Adding ZTUtils to the dist
Richard Jones <richard@users.sourceforge.net>
parents:
diff
changeset
|
12 ############################################################################## |
|
7fe79c67aaa9
Adding ZTUtils to the dist
Richard Jones <richard@users.sourceforge.net>
parents:
diff
changeset
|
13 __doc__='''Batch class, for iterating over a sequence in batches |
|
7fe79c67aaa9
Adding ZTUtils to the dist
Richard Jones <richard@users.sourceforge.net>
parents:
diff
changeset
|
14 |
|
7fe79c67aaa9
Adding ZTUtils to the dist
Richard Jones <richard@users.sourceforge.net>
parents:
diff
changeset
|
15 $Id: Batch.py,v 1.1 2002-08-30 08:25:33 richard Exp $''' |
|
7fe79c67aaa9
Adding ZTUtils to the dist
Richard Jones <richard@users.sourceforge.net>
parents:
diff
changeset
|
16 __version__='$Revision: 1.1 $'[11:-2] |
|
7fe79c67aaa9
Adding ZTUtils to the dist
Richard Jones <richard@users.sourceforge.net>
parents:
diff
changeset
|
17 |
|
7fe79c67aaa9
Adding ZTUtils to the dist
Richard Jones <richard@users.sourceforge.net>
parents:
diff
changeset
|
18 from ExtensionClass import Base |
|
7fe79c67aaa9
Adding ZTUtils to the dist
Richard Jones <richard@users.sourceforge.net>
parents:
diff
changeset
|
19 |
|
7fe79c67aaa9
Adding ZTUtils to the dist
Richard Jones <richard@users.sourceforge.net>
parents:
diff
changeset
|
20 class LazyPrevBatch(Base): |
|
7fe79c67aaa9
Adding ZTUtils to the dist
Richard Jones <richard@users.sourceforge.net>
parents:
diff
changeset
|
21 def __of__(self, parent): |
|
7fe79c67aaa9
Adding ZTUtils to the dist
Richard Jones <richard@users.sourceforge.net>
parents:
diff
changeset
|
22 return Batch(parent._sequence, parent._size, |
|
7fe79c67aaa9
Adding ZTUtils to the dist
Richard Jones <richard@users.sourceforge.net>
parents:
diff
changeset
|
23 parent.first - parent._size + parent.overlap, 0, |
|
7fe79c67aaa9
Adding ZTUtils to the dist
Richard Jones <richard@users.sourceforge.net>
parents:
diff
changeset
|
24 parent.orphan, parent.overlap) |
|
7fe79c67aaa9
Adding ZTUtils to the dist
Richard Jones <richard@users.sourceforge.net>
parents:
diff
changeset
|
25 |
|
7fe79c67aaa9
Adding ZTUtils to the dist
Richard Jones <richard@users.sourceforge.net>
parents:
diff
changeset
|
26 class LazyNextBatch(Base): |
|
7fe79c67aaa9
Adding ZTUtils to the dist
Richard Jones <richard@users.sourceforge.net>
parents:
diff
changeset
|
27 def __of__(self, parent): |
|
7fe79c67aaa9
Adding ZTUtils to the dist
Richard Jones <richard@users.sourceforge.net>
parents:
diff
changeset
|
28 try: parent._sequence[parent.end] |
|
7fe79c67aaa9
Adding ZTUtils to the dist
Richard Jones <richard@users.sourceforge.net>
parents:
diff
changeset
|
29 except IndexError: return None |
|
7fe79c67aaa9
Adding ZTUtils to the dist
Richard Jones <richard@users.sourceforge.net>
parents:
diff
changeset
|
30 return Batch(parent._sequence, parent._size, |
|
7fe79c67aaa9
Adding ZTUtils to the dist
Richard Jones <richard@users.sourceforge.net>
parents:
diff
changeset
|
31 parent.end - parent.overlap, 0, |
|
7fe79c67aaa9
Adding ZTUtils to the dist
Richard Jones <richard@users.sourceforge.net>
parents:
diff
changeset
|
32 parent.orphan, parent.overlap) |
|
7fe79c67aaa9
Adding ZTUtils to the dist
Richard Jones <richard@users.sourceforge.net>
parents:
diff
changeset
|
33 |
|
7fe79c67aaa9
Adding ZTUtils to the dist
Richard Jones <richard@users.sourceforge.net>
parents:
diff
changeset
|
34 class LazySequenceLength(Base): |
|
7fe79c67aaa9
Adding ZTUtils to the dist
Richard Jones <richard@users.sourceforge.net>
parents:
diff
changeset
|
35 def __of__(self, parent): |
|
7fe79c67aaa9
Adding ZTUtils to the dist
Richard Jones <richard@users.sourceforge.net>
parents:
diff
changeset
|
36 parent.sequence_length = l = len(parent._sequence) |
|
7fe79c67aaa9
Adding ZTUtils to the dist
Richard Jones <richard@users.sourceforge.net>
parents:
diff
changeset
|
37 return l |
|
7fe79c67aaa9
Adding ZTUtils to the dist
Richard Jones <richard@users.sourceforge.net>
parents:
diff
changeset
|
38 |
|
7fe79c67aaa9
Adding ZTUtils to the dist
Richard Jones <richard@users.sourceforge.net>
parents:
diff
changeset
|
39 class Batch(Base): |
|
7fe79c67aaa9
Adding ZTUtils to the dist
Richard Jones <richard@users.sourceforge.net>
parents:
diff
changeset
|
40 """Create a sequence batch""" |
|
7fe79c67aaa9
Adding ZTUtils to the dist
Richard Jones <richard@users.sourceforge.net>
parents:
diff
changeset
|
41 __allow_access_to_unprotected_subobjects__ = 1 |
|
7fe79c67aaa9
Adding ZTUtils to the dist
Richard Jones <richard@users.sourceforge.net>
parents:
diff
changeset
|
42 |
|
7fe79c67aaa9
Adding ZTUtils to the dist
Richard Jones <richard@users.sourceforge.net>
parents:
diff
changeset
|
43 previous = LazyPrevBatch() |
|
7fe79c67aaa9
Adding ZTUtils to the dist
Richard Jones <richard@users.sourceforge.net>
parents:
diff
changeset
|
44 next = LazyNextBatch() |
|
7fe79c67aaa9
Adding ZTUtils to the dist
Richard Jones <richard@users.sourceforge.net>
parents:
diff
changeset
|
45 sequence_length = LazySequenceLength() |
|
7fe79c67aaa9
Adding ZTUtils to the dist
Richard Jones <richard@users.sourceforge.net>
parents:
diff
changeset
|
46 |
|
7fe79c67aaa9
Adding ZTUtils to the dist
Richard Jones <richard@users.sourceforge.net>
parents:
diff
changeset
|
47 def __init__(self, sequence, size, start=0, end=0, |
|
7fe79c67aaa9
Adding ZTUtils to the dist
Richard Jones <richard@users.sourceforge.net>
parents:
diff
changeset
|
48 orphan=0, overlap=0): |
|
7fe79c67aaa9
Adding ZTUtils to the dist
Richard Jones <richard@users.sourceforge.net>
parents:
diff
changeset
|
49 '''Encapsulate "sequence" in batches of "size". |
|
7fe79c67aaa9
Adding ZTUtils to the dist
Richard Jones <richard@users.sourceforge.net>
parents:
diff
changeset
|
50 |
|
7fe79c67aaa9
Adding ZTUtils to the dist
Richard Jones <richard@users.sourceforge.net>
parents:
diff
changeset
|
51 Arguments: "start" and "end" are 0-based indexes into the |
|
7fe79c67aaa9
Adding ZTUtils to the dist
Richard Jones <richard@users.sourceforge.net>
parents:
diff
changeset
|
52 sequence. If the next batch would contain no more than |
|
7fe79c67aaa9
Adding ZTUtils to the dist
Richard Jones <richard@users.sourceforge.net>
parents:
diff
changeset
|
53 "orphan" elements, it is combined with the current batch. |
|
7fe79c67aaa9
Adding ZTUtils to the dist
Richard Jones <richard@users.sourceforge.net>
parents:
diff
changeset
|
54 "overlap" is the number of elements shared by adjacent |
|
7fe79c67aaa9
Adding ZTUtils to the dist
Richard Jones <richard@users.sourceforge.net>
parents:
diff
changeset
|
55 batches. If "size" is not specified, it is computed from |
|
7fe79c67aaa9
Adding ZTUtils to the dist
Richard Jones <richard@users.sourceforge.net>
parents:
diff
changeset
|
56 "start" and "end". Failing that, it is 7. |
|
7fe79c67aaa9
Adding ZTUtils to the dist
Richard Jones <richard@users.sourceforge.net>
parents:
diff
changeset
|
57 |
|
7fe79c67aaa9
Adding ZTUtils to the dist
Richard Jones <richard@users.sourceforge.net>
parents:
diff
changeset
|
58 Attributes: Note that the "start" attribute, unlike the |
|
7fe79c67aaa9
Adding ZTUtils to the dist
Richard Jones <richard@users.sourceforge.net>
parents:
diff
changeset
|
59 argument, is a 1-based index (I know, lame). "first" is the |
|
7fe79c67aaa9
Adding ZTUtils to the dist
Richard Jones <richard@users.sourceforge.net>
parents:
diff
changeset
|
60 0-based index. "length" is the actual number of elements in |
|
7fe79c67aaa9
Adding ZTUtils to the dist
Richard Jones <richard@users.sourceforge.net>
parents:
diff
changeset
|
61 the batch. |
|
7fe79c67aaa9
Adding ZTUtils to the dist
Richard Jones <richard@users.sourceforge.net>
parents:
diff
changeset
|
62 |
|
7fe79c67aaa9
Adding ZTUtils to the dist
Richard Jones <richard@users.sourceforge.net>
parents:
diff
changeset
|
63 "sequence_length" is the length of the original, unbatched, sequence |
|
7fe79c67aaa9
Adding ZTUtils to the dist
Richard Jones <richard@users.sourceforge.net>
parents:
diff
changeset
|
64 ''' |
|
7fe79c67aaa9
Adding ZTUtils to the dist
Richard Jones <richard@users.sourceforge.net>
parents:
diff
changeset
|
65 |
|
7fe79c67aaa9
Adding ZTUtils to the dist
Richard Jones <richard@users.sourceforge.net>
parents:
diff
changeset
|
66 start = start + 1 |
|
7fe79c67aaa9
Adding ZTUtils to the dist
Richard Jones <richard@users.sourceforge.net>
parents:
diff
changeset
|
67 |
|
7fe79c67aaa9
Adding ZTUtils to the dist
Richard Jones <richard@users.sourceforge.net>
parents:
diff
changeset
|
68 start,end,sz = opt(start,end,size,orphan,sequence) |
|
7fe79c67aaa9
Adding ZTUtils to the dist
Richard Jones <richard@users.sourceforge.net>
parents:
diff
changeset
|
69 |
|
7fe79c67aaa9
Adding ZTUtils to the dist
Richard Jones <richard@users.sourceforge.net>
parents:
diff
changeset
|
70 self._sequence = sequence |
|
7fe79c67aaa9
Adding ZTUtils to the dist
Richard Jones <richard@users.sourceforge.net>
parents:
diff
changeset
|
71 self.size = sz |
|
7fe79c67aaa9
Adding ZTUtils to the dist
Richard Jones <richard@users.sourceforge.net>
parents:
diff
changeset
|
72 self._size = size |
|
7fe79c67aaa9
Adding ZTUtils to the dist
Richard Jones <richard@users.sourceforge.net>
parents:
diff
changeset
|
73 self.start = start |
|
7fe79c67aaa9
Adding ZTUtils to the dist
Richard Jones <richard@users.sourceforge.net>
parents:
diff
changeset
|
74 self.end = end |
|
7fe79c67aaa9
Adding ZTUtils to the dist
Richard Jones <richard@users.sourceforge.net>
parents:
diff
changeset
|
75 self.orphan = orphan |
|
7fe79c67aaa9
Adding ZTUtils to the dist
Richard Jones <richard@users.sourceforge.net>
parents:
diff
changeset
|
76 self.overlap = overlap |
|
7fe79c67aaa9
Adding ZTUtils to the dist
Richard Jones <richard@users.sourceforge.net>
parents:
diff
changeset
|
77 self.first = max(start - 1, 0) |
|
7fe79c67aaa9
Adding ZTUtils to the dist
Richard Jones <richard@users.sourceforge.net>
parents:
diff
changeset
|
78 self.length = self.end - self.first |
|
7fe79c67aaa9
Adding ZTUtils to the dist
Richard Jones <richard@users.sourceforge.net>
parents:
diff
changeset
|
79 if self.first == 0: |
|
7fe79c67aaa9
Adding ZTUtils to the dist
Richard Jones <richard@users.sourceforge.net>
parents:
diff
changeset
|
80 self.previous = None |
|
7fe79c67aaa9
Adding ZTUtils to the dist
Richard Jones <richard@users.sourceforge.net>
parents:
diff
changeset
|
81 |
|
7fe79c67aaa9
Adding ZTUtils to the dist
Richard Jones <richard@users.sourceforge.net>
parents:
diff
changeset
|
82 |
|
7fe79c67aaa9
Adding ZTUtils to the dist
Richard Jones <richard@users.sourceforge.net>
parents:
diff
changeset
|
83 def __getitem__(self, index): |
|
7fe79c67aaa9
Adding ZTUtils to the dist
Richard Jones <richard@users.sourceforge.net>
parents:
diff
changeset
|
84 if index < 0: |
|
7fe79c67aaa9
Adding ZTUtils to the dist
Richard Jones <richard@users.sourceforge.net>
parents:
diff
changeset
|
85 if index + self.end < self.first: raise IndexError, index |
|
7fe79c67aaa9
Adding ZTUtils to the dist
Richard Jones <richard@users.sourceforge.net>
parents:
diff
changeset
|
86 return self._sequence[index + self.end] |
|
7fe79c67aaa9
Adding ZTUtils to the dist
Richard Jones <richard@users.sourceforge.net>
parents:
diff
changeset
|
87 |
|
7fe79c67aaa9
Adding ZTUtils to the dist
Richard Jones <richard@users.sourceforge.net>
parents:
diff
changeset
|
88 if index >= self.length: raise IndexError, index |
|
7fe79c67aaa9
Adding ZTUtils to the dist
Richard Jones <richard@users.sourceforge.net>
parents:
diff
changeset
|
89 return self._sequence[index+self.first] |
|
7fe79c67aaa9
Adding ZTUtils to the dist
Richard Jones <richard@users.sourceforge.net>
parents:
diff
changeset
|
90 |
|
7fe79c67aaa9
Adding ZTUtils to the dist
Richard Jones <richard@users.sourceforge.net>
parents:
diff
changeset
|
91 def __len__(self): |
|
7fe79c67aaa9
Adding ZTUtils to the dist
Richard Jones <richard@users.sourceforge.net>
parents:
diff
changeset
|
92 return self.length |
|
7fe79c67aaa9
Adding ZTUtils to the dist
Richard Jones <richard@users.sourceforge.net>
parents:
diff
changeset
|
93 |
|
7fe79c67aaa9
Adding ZTUtils to the dist
Richard Jones <richard@users.sourceforge.net>
parents:
diff
changeset
|
94 def opt(start,end,size,orphan,sequence): |
|
7fe79c67aaa9
Adding ZTUtils to the dist
Richard Jones <richard@users.sourceforge.net>
parents:
diff
changeset
|
95 if size < 1: |
|
7fe79c67aaa9
Adding ZTUtils to the dist
Richard Jones <richard@users.sourceforge.net>
parents:
diff
changeset
|
96 if start > 0 and end > 0 and end >= start: |
|
7fe79c67aaa9
Adding ZTUtils to the dist
Richard Jones <richard@users.sourceforge.net>
parents:
diff
changeset
|
97 size=end+1-start |
|
7fe79c67aaa9
Adding ZTUtils to the dist
Richard Jones <richard@users.sourceforge.net>
parents:
diff
changeset
|
98 else: size=7 |
|
7fe79c67aaa9
Adding ZTUtils to the dist
Richard Jones <richard@users.sourceforge.net>
parents:
diff
changeset
|
99 |
|
7fe79c67aaa9
Adding ZTUtils to the dist
Richard Jones <richard@users.sourceforge.net>
parents:
diff
changeset
|
100 if start > 0: |
|
7fe79c67aaa9
Adding ZTUtils to the dist
Richard Jones <richard@users.sourceforge.net>
parents:
diff
changeset
|
101 |
|
7fe79c67aaa9
Adding ZTUtils to the dist
Richard Jones <richard@users.sourceforge.net>
parents:
diff
changeset
|
102 try: sequence[start-1] |
|
7fe79c67aaa9
Adding ZTUtils to the dist
Richard Jones <richard@users.sourceforge.net>
parents:
diff
changeset
|
103 except IndexError: start=len(sequence) |
|
7fe79c67aaa9
Adding ZTUtils to the dist
Richard Jones <richard@users.sourceforge.net>
parents:
diff
changeset
|
104 |
|
7fe79c67aaa9
Adding ZTUtils to the dist
Richard Jones <richard@users.sourceforge.net>
parents:
diff
changeset
|
105 if end > 0: |
|
7fe79c67aaa9
Adding ZTUtils to the dist
Richard Jones <richard@users.sourceforge.net>
parents:
diff
changeset
|
106 if end < start: end=start |
|
7fe79c67aaa9
Adding ZTUtils to the dist
Richard Jones <richard@users.sourceforge.net>
parents:
diff
changeset
|
107 else: |
|
7fe79c67aaa9
Adding ZTUtils to the dist
Richard Jones <richard@users.sourceforge.net>
parents:
diff
changeset
|
108 end=start+size-1 |
|
7fe79c67aaa9
Adding ZTUtils to the dist
Richard Jones <richard@users.sourceforge.net>
parents:
diff
changeset
|
109 try: sequence[end+orphan-1] |
|
7fe79c67aaa9
Adding ZTUtils to the dist
Richard Jones <richard@users.sourceforge.net>
parents:
diff
changeset
|
110 except IndexError: end=len(sequence) |
|
7fe79c67aaa9
Adding ZTUtils to the dist
Richard Jones <richard@users.sourceforge.net>
parents:
diff
changeset
|
111 elif end > 0: |
|
7fe79c67aaa9
Adding ZTUtils to the dist
Richard Jones <richard@users.sourceforge.net>
parents:
diff
changeset
|
112 try: sequence[end-1] |
|
7fe79c67aaa9
Adding ZTUtils to the dist
Richard Jones <richard@users.sourceforge.net>
parents:
diff
changeset
|
113 except IndexError: end=len(sequence) |
|
7fe79c67aaa9
Adding ZTUtils to the dist
Richard Jones <richard@users.sourceforge.net>
parents:
diff
changeset
|
114 start=end+1-size |
|
7fe79c67aaa9
Adding ZTUtils to the dist
Richard Jones <richard@users.sourceforge.net>
parents:
diff
changeset
|
115 if start - 1 < orphan: start=1 |
|
7fe79c67aaa9
Adding ZTUtils to the dist
Richard Jones <richard@users.sourceforge.net>
parents:
diff
changeset
|
116 else: |
|
7fe79c67aaa9
Adding ZTUtils to the dist
Richard Jones <richard@users.sourceforge.net>
parents:
diff
changeset
|
117 start=1 |
|
7fe79c67aaa9
Adding ZTUtils to the dist
Richard Jones <richard@users.sourceforge.net>
parents:
diff
changeset
|
118 end=start+size-1 |
|
7fe79c67aaa9
Adding ZTUtils to the dist
Richard Jones <richard@users.sourceforge.net>
parents:
diff
changeset
|
119 try: sequence[end+orphan-1] |
|
7fe79c67aaa9
Adding ZTUtils to the dist
Richard Jones <richard@users.sourceforge.net>
parents:
diff
changeset
|
120 except IndexError: end=len(sequence) |
|
7fe79c67aaa9
Adding ZTUtils to the dist
Richard Jones <richard@users.sourceforge.net>
parents:
diff
changeset
|
121 return start,end,size |
