Show More
@@ -59,17 +59,19 b' else:' | |||||
59 | class Map(object): |
|
59 | class Map(object): | |
60 | """A class for partitioning a sequence using a map.""" |
|
60 | """A class for partitioning a sequence using a map.""" | |
61 |
|
61 | |||
62 | def getPartition(self, seq, p, q): |
|
62 | def getPartition(self, seq, p, q, n=None): | |
63 |
"""Returns the pth partition of q partitions of seq. |
|
63 | """Returns the pth partition of q partitions of seq. | |
64 |
|
|
64 | ||
|
65 | The length can be specified as `n`, | |||
|
66 | otherwise it is the value of `len(seq)` | |||
|
67 | """ | |||
|
68 | n = len(seq) if n is None else n | |||
65 | # Test for error conditions here |
|
69 | # Test for error conditions here | |
66 | if p<0 or p>=q: |
|
70 | if p<0 or p>=q: | |
67 | print "No partition exists." |
|
71 | raise ValueError("must have 0 <= p <= q, but have p=%s,q=%s" % (p, q)) | |
68 | return |
|
|||
69 |
|
72 | |||
70 | N = len(seq) |
|
73 | remainder = n % q | |
71 |
|
|
74 | basesize = n // q | |
72 | basesize = N // q |
|
|||
73 |
|
75 | |||
74 | if p < remainder: |
|
76 | if p < remainder: | |
75 | low = p * (basesize + 1) |
|
77 | low = p * (basesize + 1) | |
@@ -104,19 +106,14 b' class Map(object):' | |||||
104 | return listOfPartitions |
|
106 | return listOfPartitions | |
105 |
|
107 | |||
106 | class RoundRobinMap(Map): |
|
108 | class RoundRobinMap(Map): | |
107 | """Partitions a sequence in a roun robin fashion. |
|
109 | """Partitions a sequence in a round robin fashion. | |
108 |
|
110 | |||
109 | This currently does not work! |
|
111 | This currently does not work! | |
110 | """ |
|
112 | """ | |
111 |
|
113 | |||
112 | def getPartition(self, seq, p, q): |
|
114 | def getPartition(self, seq, p, q, n=None): | |
113 | # if not isinstance(seq,(list,tuple)): |
|
115 | n = len(seq) if n is None else n | |
114 | # raise NotImplementedError("cannot RR partition type %s"%type(seq)) |
|
116 | return seq[p:n:q] | |
115 | return seq[p:len(seq):q] |
|
|||
116 | #result = [] |
|
|||
117 | #for i in range(p,len(seq),q): |
|
|||
118 | # result.append(seq[i]) |
|
|||
119 | #return result |
|
|||
120 |
|
117 | |||
121 | def joinPartitions(self, listOfPartitions): |
|
118 | def joinPartitions(self, listOfPartitions): | |
122 | testObject = listOfPartitions[0] |
|
119 | testObject = listOfPartitions[0] |
General Comments 0
You need to be logged in to leave comments.
Login now