# HG changeset patch # User Paul Boddie # Date 1480527898 -3600 # Node ID fff09c70f4894f2983e588bbe0b7896b2502aad5 # Parent 299f7c37ac6ea97d218eb66a63df0f93532c9cd9 Implemented the dictionary items method and expanded IndexError. Expanded the dictionary test program. diff -r 299f7c37ac6e -r fff09c70f489 lib/__builtins__/dict.py --- a/lib/__builtins__/dict.py Wed Nov 30 18:42:37 2016 +0100 +++ b/lib/__builtins__/dict.py Wed Nov 30 18:44:58 2016 +0100 @@ -110,7 +110,7 @@ if i is None: if default is self.MISSING: - raise KeyError + raise KeyError(key) else: return default @@ -134,7 +134,12 @@ return native._dict_values(self) - def items(self): pass + def items(self): + + "Return the items, each being a (key, value) tuple, in this dictionary." + + return zip([self.keys(), self.values()]) + def get(self, key): pass def setdefault(self, key, value): pass def update(self, other): pass diff -r 299f7c37ac6e -r fff09c70f489 lib/__builtins__/exception/base.py --- a/lib/__builtins__/exception/base.py Wed Nov 30 18:42:37 2016 +0100 +++ b/lib/__builtins__/exception/base.py Wed Nov 30 18:44:58 2016 +0100 @@ -26,7 +26,13 @@ def __init__(self, index): self.index = index -class KeyError(Exception): pass +class KeyError(Exception): + + "An error concerned with a dictionary key." + + def __init__(self, key): + self.key = key + class NotImplementedError(Exception): pass class StopIteration(Exception): pass diff -r 299f7c37ac6e -r fff09c70f489 tests/dict.py --- a/tests/dict.py Wed Nov 30 18:42:37 2016 +0100 +++ b/tests/dict.py Wed Nov 30 18:44:58 2016 +0100 @@ -1,21 +1,36 @@ def f(d): return d.keys() -#def g(d): -# for key, value in d.items(): -# return value +def g(d): + for key, value in d.items(): + return value d = {10 : "a", 20 : "b"} +print d[10] # a +print d[20] # b +try: + print d[30] # should fail with an exception +except KeyError, exc: + print "d[30]: key not found", exc.key + l = f(d) +print l print 10 in l # True print 20 in l # True print 30 in l # False l = d.values() +print l print "a" in l # True print "b" in l # True print "c" in l # False +v = g(d) # either "a" or "b" +print v +print v == "a" or v == "b" # True +print v == 10 or v == 20 # False -#v = g(d) # either "a" or "b" -#print v == "a" or v == "b" # True +l = d.items() +print l +print (10, "a") in l # True +print (10, "b") in l # False