# HG changeset patch # User Paul Boddie # Date 1484515054 -3600 # Node ID 5969ebcb3cc11e5dd6c2f7c157238b2785962a1e # Parent dde129595d21816bcfb3b00a7f0d08e6e78ec527 Added clear and __delitem__ methods to the dictionary implementation. diff -r dde129595d21 -r 5969ebcb3cc1 lib/__builtins__/dict.py --- a/lib/__builtins__/dict.py Sun Jan 15 18:59:16 2017 +0100 +++ b/lib/__builtins__/dict.py Sun Jan 15 22:17:34 2017 +0100 @@ -33,8 +33,7 @@ "Initialise the dictionary." - self.size = 0 - self.buckets = self._get_buckets(args is not None and len(args) / 2 or 0) + self.clear() if args is not None: for key, value in args: @@ -80,6 +79,20 @@ return buckets + def _get_entry(self, key): + + "Return the index and entry index as a tuple for 'key'." + + # Find an index identifying the bucket involved. + + index = self._get_index(key) + + # Find the entry index within the bucket of the key. + + i = self._find_entry(key, index) + + return index, i + def _get_index(self, key): "Check 'key' and return an index or raise TypeError." @@ -119,13 +132,7 @@ "Set in the 'buckets' an item having the given 'key' and 'value'." - # Find an index identifying the bucket involved. - - index = self._get_index(key) - - # Find the entry index within the bucket of the key. - - i = self._find_entry(key, index) + index, i = self._get_entry(key) # With no existing entry, append to the bucket. @@ -138,6 +145,32 @@ else: buckets[index][i] = key, value + # Public special methods. + + def __delitem__(self, key): + + "Remove the entry associated with the given 'key' from this dictionary." + + index, i = self._get_entry(key) + + if index is None or i is None: + raise KeyError, key + + del self.buckets[index][i] + self.size -= 1 + + def __getitem__(self, key): + + "Return the value associated with 'key' from the dictionary." + + return self.get(key, self.MISSING) + + def __iter__(self): + + "Return an iterator." + + return itemiterator(self.keys()) + def __setitem__(self, key, value): "Set a mapping from 'key' to 'value' in the dictionary." @@ -149,13 +182,14 @@ self._setitem(self.buckets, key, value) - def __delitem__(self, key, value): pass + # Public conventional methods. - def __getitem__(self, key): + def clear(self): - "Return the value associated with 'key' from the dictionary." + "Reset the dictionary to an empty state." - return self.get(key, self.MISSING) + self.size = 0 + self.buckets = self._get_buckets(0) def get(self, key, default=None): @@ -164,13 +198,7 @@ the dictionary, 'default' will be returned instead. """ - # Find an index identifying the bucket involved. - - index = self._get_index(key) - - # Find the entry index within the bucket of the key. - - i = self._find_entry(key, index) + index, i = self._get_entry(key) # With no entry index, either raise an exception or return the default. @@ -185,9 +213,11 @@ else: return self.buckets[index][i][1] - def clear(self): pass + def has_key(self, key): - def has_key(self): pass + "Return whether the given 'key' is used with this dictionary." + + return self.get(key) and True or False def keys(self): @@ -198,15 +228,6 @@ l.append(key) return l - def values(self): - - "Return the values in this dictionary." - - l = [] - for key, value in self.items(): - l.append(value) - return l - def items(self): "Return the items, each being a (key, value) tuple, in this dictionary." @@ -217,12 +238,16 @@ return l def setdefault(self, key, value): pass + def update(self, other): pass - def __iter__(self): + def values(self): + + "Return the values in this dictionary." - "Return an iterator." - - return itemiterator(self.keys()) + l = [] + for key, value in self.items(): + l.append(value) + return l # vim: tabstop=4 expandtab shiftwidth=4 diff -r dde129595d21 -r 5969ebcb3cc1 tests/dict.py --- a/tests/dict.py Sun Jan 15 18:59:16 2017 +0100 +++ b/tests/dict.py Sun Jan 15 22:17:34 2017 +0100 @@ -18,6 +18,8 @@ print "d[30]: key not found", exc.key print d.get(30) # None print d.get(30, "c") # c +print d.has_key(20) # True +print d.has_key(30) # False l = f(d) print "# l: ", @@ -60,3 +62,17 @@ print d[[1, 2]] except TypeError: print "d[[1, 2]]: key not appropriate" + +# Attempt to remove items. + +del d[20] +print d.has_key(20) # False +try: + del d[30] # should fail with an exception +except KeyError, exc: + print "del d[30]: key not found", exc.key + +# Clear the dictionary. + +d.clear() +print d # {}