changeset 242:f7a91de3091d

created
author 127.0.0.1
date Sun, 30 Jun 2013 11:07:31 +0200
parents 8db18f3c0fdd
children e99ad85d9e25
files ___%2Aslice__
diffstat 1 files changed, 11 insertions(+), 16 deletions(-) [+]
line wrap: on
line diff
--- a/___%2Aslice__	Sun Jun 30 11:07:06 2013 +0200
+++ b/___%2Aslice__	Sun Jun 30 11:07:31 2013 +0200
@@ -1,20 +1,15 @@
 {{{#!python
 thing[start:end:step]
 def get_slice(thing, start=None, end=None, step=None):
-    return thing.__getitem__(slice(start, end, step))
-}}}
-----
-{{{#!python
-def MyCollection(object):
-    def __init__(self):
-        self.items = [1, 2, 3, 4]
-
-    def __getitem__(self, index):
-        if isinstance(index, slice):
-            return [
-                self.items[i]
-                for i in
-                index.indices(len(self.items))
-            ]
-        return self.items[index]
+    if step is None and hasattr(thing.__class__, '__getslice__'):
+        if start is None:
+            start = 0
+        elif start < 0:
+            start += len(thing)
+        if end is None:
+            end = sys.maxint
+        elif end < 0:
+            end += len(thing)
+        return thing.__getslice__(start, end)
+    return thing[slice(start, end, step)]
 }}}
\ No newline at end of file