[Checkins] SVN: z3c.batching/trunk/src/z3c/batching/ added slicing
to Batch and Batches
Kim Nikolay
fafhrd at datacom.kz
Tue Dec 4 03:06:58 EST 2007
Log message for revision 82116:
added slicing to Batch and Batches
Changed:
U z3c.batching/trunk/src/z3c/batching/README.txt
U z3c.batching/trunk/src/z3c/batching/batch.py
-=-
Modified: z3c.batching/trunk/src/z3c/batching/README.txt
===================================================================
--- z3c.batching/trunk/src/z3c/batching/README.txt 2007-12-04 05:07:48 UTC (rev 82115)
+++ z3c.batching/trunk/src/z3c/batching/README.txt 2007-12-04 08:06:58 UTC (rev 82116)
@@ -82,6 +82,24 @@
>>> batch[2]
'nine'
+Slicing
+
+ >>> batch[:1]
+ ['seven']
+
+ >>> batch[1:2]
+ ['eight']
+
+ >>> batch[1:]
+ ['eight', 'nine']
+
+ >>> batch[:]
+ ['seven', 'eight', 'nine']
+
+ >>> batch[10:]
+ []
+
+
If you ask for inex that is out of range, an index error is raised:
>>> batch[3]
@@ -194,3 +212,20 @@
>>> batch.batches[-2]
<Batch start=6, size=3>
+
+Slicing
+
+ >>> batch.batches[:1]
+ [<Batch start=0, size=3>]
+
+ >>> batch.batches[:]
+ [<Batch start=0, size=3>, <Batch start=3, size=3>, <Batch start=6, size=3>]
+
+ >>> batch.batches[1:2]
+ [<Batch start=3, size=3>]
+
+ >>> batch.batches[1:]
+ [<Batch start=3, size=3>, <Batch start=6, size=3>]
+
+ >>> batch.batches[10:]
+ []
Modified: z3c.batching/trunk/src/z3c/batching/batch.py
===================================================================
--- z3c.batching/trunk/src/z3c/batching/batch.py 2007-12-04 05:07:48 UTC (rev 82115)
+++ z3c.batching/trunk/src/z3c/batching/batch.py 2007-12-04 08:06:58 UTC (rev 82116)
@@ -124,6 +124,12 @@
else:
return False
+ def __getslice__(self, i, j):
+ if j > self.end:
+ j = self._trueSize
+
+ return [self[idx] for idx in range(i, j)]
+
def __eq__(self, other):
return ((self.size, self.start, self.sequence) ==
(other.size, other.start, other.sequence))
@@ -162,3 +168,9 @@
return self._batches[key]
except KeyError:
raise IndexError(key)
+
+ def __getslice__(self, i, j):
+ if j > self.total:
+ j = self.total-1
+
+ return [self[idx] for idx in range(i, j)]
More information about the Checkins
mailing list