1.1 --- a/lib/__builtins__/dict.py Wed Nov 30 18:42:37 2016 +0100
1.2 +++ b/lib/__builtins__/dict.py Wed Nov 30 18:44:58 2016 +0100
1.3 @@ -110,7 +110,7 @@
1.4
1.5 if i is None:
1.6 if default is self.MISSING:
1.7 - raise KeyError
1.8 + raise KeyError(key)
1.9 else:
1.10 return default
1.11
1.12 @@ -134,7 +134,12 @@
1.13
1.14 return native._dict_values(self)
1.15
1.16 - def items(self): pass
1.17 + def items(self):
1.18 +
1.19 + "Return the items, each being a (key, value) tuple, in this dictionary."
1.20 +
1.21 + return zip([self.keys(), self.values()])
1.22 +
1.23 def get(self, key): pass
1.24 def setdefault(self, key, value): pass
1.25 def update(self, other): pass
2.1 --- a/lib/__builtins__/exception/base.py Wed Nov 30 18:42:37 2016 +0100
2.2 +++ b/lib/__builtins__/exception/base.py Wed Nov 30 18:44:58 2016 +0100
2.3 @@ -26,7 +26,13 @@
2.4 def __init__(self, index):
2.5 self.index = index
2.6
2.7 -class KeyError(Exception): pass
2.8 +class KeyError(Exception):
2.9 +
2.10 + "An error concerned with a dictionary key."
2.11 +
2.12 + def __init__(self, key):
2.13 + self.key = key
2.14 +
2.15 class NotImplementedError(Exception): pass
2.16 class StopIteration(Exception): pass
2.17
3.1 --- a/tests/dict.py Wed Nov 30 18:42:37 2016 +0100
3.2 +++ b/tests/dict.py Wed Nov 30 18:44:58 2016 +0100
3.3 @@ -1,21 +1,36 @@
3.4 def f(d):
3.5 return d.keys()
3.6
3.7 -#def g(d):
3.8 -# for key, value in d.items():
3.9 -# return value
3.10 +def g(d):
3.11 + for key, value in d.items():
3.12 + return value
3.13
3.14 d = {10 : "a", 20 : "b"}
3.15 +print d[10] # a
3.16 +print d[20] # b
3.17 +try:
3.18 + print d[30] # should fail with an exception
3.19 +except KeyError, exc:
3.20 + print "d[30]: key not found", exc.key
3.21 +
3.22 l = f(d)
3.23 +print l
3.24 print 10 in l # True
3.25 print 20 in l # True
3.26 print 30 in l # False
3.27
3.28 l = d.values()
3.29 +print l
3.30 print "a" in l # True
3.31 print "b" in l # True
3.32 print "c" in l # False
3.33
3.34 +v = g(d) # either "a" or "b"
3.35 +print v
3.36 +print v == "a" or v == "b" # True
3.37 +print v == 10 or v == 20 # False
3.38
3.39 -#v = g(d) # either "a" or "b"
3.40 -#print v == "a" or v == "b" # True
3.41 +l = d.items()
3.42 +print l
3.43 +print (10, "a") in l # True
3.44 +print (10, "b") in l # False