Mercurial > p > roundup > code
annotate roundup/cgi/ZTUtils/Batch.py @ 5695:3e1b66c4e1e2
Update docs. Correct errors reported by setup.py build_docs. Add rest
interface and link to rest doc to features page. Add link to xmlrpc
doc to features page. Add rest doc to index. Update rest doc,
hopefully clarify confusing use of parameters in patch action
section. Fix code examples in "Adding new rest endpoints" section. Fix
example adding import of exception.
| author | John Rouillard <rouilj@ieee.org> |
|---|---|
| date | Sun, 07 Apr 2019 20:17:52 -0400 |
| parents | 35ea9b1efc14 |
| children |
| rev | line source |
|---|---|
| 1049 | 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 | |
|
4570
6e3e4f24c753
Remove keyword expansions from CVS. All regression tests passed afterwards.
Eric S. Raymond <esr@thyrsus.com>
parents:
4532
diff
changeset
|
15 ''' |
|
2005
fc52d57c6c3e
documentation cleanup
Richard Jones <richard@users.sourceforge.net>
parents:
1120
diff
changeset
|
16 __docformat__ = 'restructuredtext' |
| 1049 | 17 |
| 18 class LazyPrevBatch: | |
| 19 def __of__(self, parent): | |
|
5087
39af8a0f3446
Applied patch attached to issue2550723. Problem in index page
John Rouillard <rouilj@ieee.org>
parents:
4570
diff
changeset
|
20 return Batch(parent._sequence, parent.size, |
| 1049 | 21 parent.first - parent._size + parent.overlap, 0, |
| 22 parent.orphan, parent.overlap) | |
| 23 | |
| 24 class LazyNextBatch: | |
| 25 def __of__(self, parent): | |
| 26 try: parent._sequence[parent.end] | |
| 27 except IndexError: return None | |
|
5087
39af8a0f3446
Applied patch attached to issue2550723. Problem in index page
John Rouillard <rouilj@ieee.org>
parents:
4570
diff
changeset
|
28 return Batch(parent._sequence, parent.size, |
| 1049 | 29 parent.end - parent.overlap, 0, |
| 30 parent.orphan, parent.overlap) | |
| 31 | |
| 32 class LazySequenceLength: | |
| 33 def __of__(self, parent): | |
| 34 parent.sequence_length = l = len(parent._sequence) | |
| 35 return l | |
| 36 | |
| 37 class Batch: | |
| 38 """Create a sequence batch""" | |
| 39 __allow_access_to_unprotected_subobjects__ = 1 | |
| 40 | |
| 41 previous = LazyPrevBatch() | |
| 42 next = LazyNextBatch() | |
| 43 sequence_length = LazySequenceLength() | |
| 44 | |
| 45 def __init__(self, sequence, size, start=0, end=0, | |
| 46 orphan=0, overlap=0): | |
| 47 '''Encapsulate "sequence" in batches of "size". | |
| 48 | |
| 49 Arguments: "start" and "end" are 0-based indexes into the | |
| 50 sequence. If the next batch would contain no more than | |
| 51 "orphan" elements, it is combined with the current batch. | |
| 52 "overlap" is the number of elements shared by adjacent | |
| 53 batches. If "size" is not specified, it is computed from | |
|
4532
6b8abdc4bb22
issue2550678: Allow pagesize=-1 which returns all results.
Bernhard Reiter <Bernhard.Reiter@intevation.de>
parents:
2005
diff
changeset
|
54 "start" and "end". If "size" is 0, it is the length of |
|
6b8abdc4bb22
issue2550678: Allow pagesize=-1 which returns all results.
Bernhard Reiter <Bernhard.Reiter@intevation.de>
parents:
2005
diff
changeset
|
55 the sequence. Failing that, it is 7. |
| 1049 | 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 | |
|
5087
39af8a0f3446
Applied patch attached to issue2550723. Problem in index page
John Rouillard <rouilj@ieee.org>
parents:
4570
diff
changeset
|
63 |
|
39af8a0f3446
Applied patch attached to issue2550723. Problem in index page
John Rouillard <rouilj@ieee.org>
parents:
4570
diff
changeset
|
64 Note: "_size" is the "actual" size used to perform batch calulcations, |
|
39af8a0f3446
Applied patch attached to issue2550723. Problem in index page
John Rouillard <rouilj@ieee.org>
parents:
4570
diff
changeset
|
65 while "size" is the "representative" size. (ie. a "special value" of |
|
39af8a0f3446
Applied patch attached to issue2550723. Problem in index page
John Rouillard <rouilj@ieee.org>
parents:
4570
diff
changeset
|
66 "size" used by the templates may translate to a different value for |
|
39af8a0f3446
Applied patch attached to issue2550723. Problem in index page
John Rouillard <rouilj@ieee.org>
parents:
4570
diff
changeset
|
67 "_size" which is used internally for batch calculations). |
| 1049 | 68 ''' |
| 69 | |
| 70 start = start + 1 | |
| 71 | |
| 72 start,end,sz = opt(start,end,size,orphan,sequence) | |
| 73 | |
| 74 self._sequence = sequence | |
|
5087
39af8a0f3446
Applied patch attached to issue2550723. Problem in index page
John Rouillard <rouilj@ieee.org>
parents:
4570
diff
changeset
|
75 self.size = size |
|
39af8a0f3446
Applied patch attached to issue2550723. Problem in index page
John Rouillard <rouilj@ieee.org>
parents:
4570
diff
changeset
|
76 self._size = sz |
| 1049 | 77 self.start = start |
| 78 self.end = end | |
| 79 self.orphan = orphan | |
| 80 self.overlap = overlap | |
| 81 self.first = max(start - 1, 0) | |
| 82 self.length = self.end - self.first | |
| 83 if self.first == 0: | |
| 84 self.previous = None | |
| 85 | |
| 86 | |
| 87 def __getitem__(self, index): | |
| 88 if index < 0: | |
|
5378
35ea9b1efc14
Python 3 preparation: "raise" syntax.
Joseph Myers <jsm@polyomino.org.uk>
parents:
5087
diff
changeset
|
89 if index + self.end < self.first: raise IndexError(index) |
| 1049 | 90 return self._sequence[index + self.end] |
| 91 | |
|
5378
35ea9b1efc14
Python 3 preparation: "raise" syntax.
Joseph Myers <jsm@polyomino.org.uk>
parents:
5087
diff
changeset
|
92 if index >= self.length: raise IndexError(index) |
|
1120
c26471971d18
Exposed the Batch mechanism through the top-level "utils" variable.
Richard Jones <richard@users.sourceforge.net>
parents:
1049
diff
changeset
|
93 return self._sequence[index + self.first] |
| 1049 | 94 |
| 95 def __len__(self): | |
| 96 return self.length | |
| 97 | |
| 98 def opt(start,end,size,orphan,sequence): | |
| 99 if size < 1: | |
|
4532
6b8abdc4bb22
issue2550678: Allow pagesize=-1 which returns all results.
Bernhard Reiter <Bernhard.Reiter@intevation.de>
parents:
2005
diff
changeset
|
100 if size == 0: |
|
6b8abdc4bb22
issue2550678: Allow pagesize=-1 which returns all results.
Bernhard Reiter <Bernhard.Reiter@intevation.de>
parents:
2005
diff
changeset
|
101 size=len(sequence) |
|
6b8abdc4bb22
issue2550678: Allow pagesize=-1 which returns all results.
Bernhard Reiter <Bernhard.Reiter@intevation.de>
parents:
2005
diff
changeset
|
102 elif start > 0 and end > 0 and end >= start: |
| 1049 | 103 size=end+1-start |
| 104 else: size=7 | |
| 105 | |
| 106 if start > 0: | |
| 107 | |
| 108 try: sequence[start-1] | |
| 109 except IndexError: start=len(sequence) | |
| 110 | |
| 111 if end > 0: | |
| 112 if end < start: end=start | |
| 113 else: | |
| 114 end=start+size-1 | |
| 115 try: sequence[end+orphan-1] | |
| 116 except IndexError: end=len(sequence) | |
| 117 elif end > 0: | |
| 118 try: sequence[end-1] | |
| 119 except IndexError: end=len(sequence) | |
| 120 start=end+1-size | |
| 121 if start - 1 < orphan: start=1 | |
| 122 else: | |
| 123 start=1 | |
| 124 end=start+size-1 | |
| 125 try: sequence[end+orphan-1] | |
| 126 except IndexError: end=len(sequence) | |
| 127 return start,end,size |
