1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
21
22 """Given a sequence to serve, this class dishes them out in chunks
23 or one at a time. This was written originally to reduce redundant
24 code. Used only by class Syncer.
25
26 This class was written so that a chunk can be snagged and
27 processed. If the processing fails, one can tell the class to only
28 present one at a time until that chunk is complete... and then
29 resume normal operations.
30
31 For example: the sequence can be a bunch of packageIds. We try to
32 process all data that corresponds to some chunk of those package
33 ids. If that process fails, we go back and reprocess that chunk,
34 one at a time, until it is complete (identifying precisely which
35 package broke). Then we resume processing a chunk at a time.
36
37 """
38
39 DIVISOR = 10
40 NEVER_LESS_THAN = 10
41 NEVER_MORE_THAN = 50
42
43 - def __init__(self, seq, divisor=None, neverlessthan=None, nevermorethan=None):
44 """Constructor.
45
46 Arguments:
47
48 seq - any sequence to server chunks of
49 divisor - the chunk size: 10 would be 1/10th of the seq length
50 neverlessthan - chunk-size is never less than this (unless out
51 of data).
52 nevermorethan - chunk-size if never more than this number.
53
54 """
55
56 divisor = divisor or self.DIVISOR
57 neverlessthan = neverlessthan or self.NEVER_LESS_THAN
58 nevermorethan = nevermorethan or self.NEVER_MORE_THAN
59
60 self.seq = seq
61 self.chunksize = min(max(len(seq) / divisor, neverlessthan),
62 nevermorethan)
63 self.oneYN = 0
64 self.alwaysOneYN = 0
65 self.returnedChunksize = 0
66 self.chunk = self.seq[:self.chunksize]
67
69 """fetch a chunk from the sequence.
70 Does not refresh the chunk until you self.clearChunk()"""
71
72 if not self.chunk:
73 self.chunk = self.seq[:self.chunksize]
74 if self.oneYN or self.alwaysOneYN:
75 _chunk = self.chunk[:1]
76 else:
77 _chunk = self.chunk
78 self.returnedChunksize = len(_chunk)
79 return _chunk
80
82 """zero the self.chunk you were working with."""
83
84 del self.chunk[:self.returnedChunksize]
85 del self.seq[:self.returnedChunksize]
86 if not self.chunk:
87 self.oneYN = 0
88
91