summaryrefslogtreecommitdiffstats
path: root/Lib
diff options
context:
space:
mode:
authorMichael W. Hudson <mwh@python.net>2002-06-11 10:55:12 (GMT)
committerMichael W. Hudson <mwh@python.net>2002-06-11 10:55:12 (GMT)
commit5efaf7eac8c1dbbf82a96dc5d9b87fddd5d17b58 (patch)
tree7358a0479ab98893ad5a5457ae0df328d0e60251 /Lib
parentf90ae20354ceb501f0ba0b6459df17f1a8005a47 (diff)
downloadcpython-5efaf7eac8c1dbbf82a96dc5d9b87fddd5d17b58.zip
cpython-5efaf7eac8c1dbbf82a96dc5d9b87fddd5d17b58.tar.gz
cpython-5efaf7eac8c1dbbf82a96dc5d9b87fddd5d17b58.tar.bz2
This is my nearly two year old patch
[ 400998 ] experimental support for extended slicing on lists somewhat spruced up and better tested than it was when I wrote it. Includes docs & tests. The whatsnew section needs expanding, and arrays should support extended slices -- later.
Diffstat (limited to 'Lib')
-rw-r--r--Lib/test/test_bool.py2
-rw-r--r--Lib/test/test_types.py72
2 files changed, 73 insertions, 1 deletions
diff --git a/Lib/test/test_bool.py b/Lib/test/test_bool.py
index 4a8bef1..cadf23a 100644
--- a/Lib/test/test_bool.py
+++ b/Lib/test/test_bool.py
@@ -219,7 +219,7 @@ veris(operator.isSequenceType(0), False)
veris(operator.isSequenceType([]), True)
veris(operator.contains([], 1), False)
veris(operator.contains([1], 1), True)
-veris(operator.isMappingType([]), False)
+veris(operator.isMappingType(1), False)
veris(operator.isMappingType({}), True)
veris(operator.lt(0, 0), False)
veris(operator.lt(0, 1), True)
diff --git a/Lib/test/test_types.py b/Lib/test/test_types.py
index bc13c5f..8452cec 100644
--- a/Lib/test/test_types.py
+++ b/Lib/test/test_types.py
@@ -188,6 +188,31 @@ else: raise TestFailed, 'in/not in string'
x = 'x'*103
if '%s!'%x != x+'!': raise TestFailed, 'nasty string formatting bug'
+#extended slices for strings
+a = '0123456789'
+vereq(a[::], a)
+vereq(a[::2], '02468')
+vereq(a[1::2], '13579')
+vereq(a[::-1],'9876543210')
+vereq(a[::-2], '97531')
+vereq(a[3::-2], '31')
+vereq(a[-100:100:], a)
+vereq(a[100:-100:-1], a[::-1])
+vereq(a[-100L:100L:2L], '02468')
+
+if have_unicode:
+ a = unicode('0123456789', 'ascii')
+ vereq(a[::], a)
+ vereq(a[::2], unicode('02468', 'ascii'))
+ vereq(a[1::2], unicode('13579', 'ascii'))
+ vereq(a[::-1], unicode('9876543210', 'ascii'))
+ vereq(a[::-2], unicode('97531', 'ascii'))
+ vereq(a[3::-2], unicode('31', 'ascii'))
+ vereq(a[-100:100:], a)
+ vereq(a[100:-100:-1], a[::-1])
+ vereq(a[-100L:100L:2L], unicode('02468', 'ascii'))
+
+
print '6.5.2 Tuples'
if len(()) != 0: raise TestFailed, 'len(())'
if len((1,)) != 1: raise TestFailed, 'len((1,))'
@@ -207,6 +232,19 @@ if x != (): raise TestFailed, 'tuple inplace add from () to () failed'
x += (1,)
if x != (1,): raise TestFailed, 'tuple resize from () failed'
+# extended slicing - subscript only for tuples
+a = (0,1,2,3,4)
+vereq(a[::], a)
+vereq(a[::2], (0,2,4))
+vereq(a[1::2], (1,3))
+vereq(a[::-1], (4,3,2,1,0))
+vereq(a[::-2], (4,2,0))
+vereq(a[3::-2], (3,1))
+vereq(a[-100:100:], a)
+vereq(a[100:-100:-1], a[::-1])
+vereq(a[-100L:100L:2L], (0,2,4))
+
+
print '6.5.3 Lists'
if len([]) != 0: raise TestFailed, 'len([])'
if len([1,]) != 1: raise TestFailed, 'len([1,])'
@@ -322,6 +360,40 @@ if a[ -pow(2,128L): 3 ] != [0,1,2]:
if a[ 3: pow(2,145L) ] != [3,4]:
raise TestFailed, "list slicing with too-large long integer"
+
+# extended slicing
+
+# subscript
+a = [0,1,2,3,4]
+vereq(a[::], a)
+vereq(a[::2], [0,2,4])
+vereq(a[1::2], [1,3])
+vereq(a[::-1], [4,3,2,1,0])
+vereq(a[::-2], [4,2,0])
+vereq(a[3::-2], [3,1])
+vereq(a[-100:100:], a)
+vereq(a[100:-100:-1], a[::-1])
+vereq(a[-100L:100L:2L], [0,2,4])
+# deletion
+del a[::2]
+vereq(a, [1,3])
+a = range(5)
+del a[1::2]
+vereq(a, [0,2,4])
+a = range(5)
+del a[1::-2]
+vereq(a, [0,2,3,4])
+# assignment
+a = range(10)
+a[::2] = [-1]*5
+vereq(a, [-1, 1, -1, 3, -1, 5, -1, 7, -1, 9])
+a = range(10)
+a[::-4] = [10]*3
+vereq(a, [0, 10, 2, 3, 4, 10, 6, 7, 8 ,10])
+a = range(4)
+a[::-1] = a
+vereq(a, [3, 2, 1, 0])
+
print '6.6 Mappings == Dictionaries'
d = {}
if d.keys() != []: raise TestFailed, '{}.keys()'