From a76d9e084fe715abea6b6cf77b486b496ec03f5e Mon Sep 17 00:00:00 2001 From: Brian Warner Date: Sat, 12 Aug 2017 21:21:04 -0700 Subject: [PATCH] dictutil: remove unused NumDict --- src/allmydata/test/test_util.py | 119 +---------------------- src/allmydata/util/dictutil.py | 164 +------------------------------- 2 files changed, 2 insertions(+), 281 deletions(-) diff --git a/src/allmydata/test/test_util.py b/src/allmydata/test/test_util.py index 81e1d2ef9..da8d4eb09 100644 --- a/src/allmydata/test/test_util.py +++ b/src/allmydata/test/test_util.py @@ -1,7 +1,7 @@ def foo(): pass # keep the line number constant -import os, time, sys, itertools, random +import os, time, sys import yaml from StringIO import StringIO from datetime import timedelta @@ -1409,13 +1409,10 @@ class DictUtil(unittest.TestCase): def test_all(self): self._help_test_eq_but_notis(dictutil.UtilDict) - self._help_test_eq_but_notis(dictutil.NumDict) self._help_test_eq_but_notis(dictutil.ValueOrderedDict) self._help_test_nonempty_dict(dictutil.UtilDict) - self._help_test_nonempty_dict(dictutil.NumDict) self._help_test_nonempty_dict(dictutil.ValueOrderedDict) self._help_test_eq_but_notis(dictutil.UtilDict) - self._help_test_eq_but_notis(dictutil.NumDict) self._help_test_eq_but_notis(dictutil.ValueOrderedDict) def test_dict_of_sets(self): @@ -1526,120 +1523,6 @@ class DictUtil(unittest.TestCase): self.failUnless(x in [(1, "b"), (2, "a")]) self.failUnlessRaises(KeyError, d.popitem) - def test_numdict(self): - d = dictutil.NumDict({"a": 1, "b": 2}) - - d.add_num("a", 10, 5) - d.add_num("c", 20, 5) - d.add_num("d", 30) - self.failUnlessEqual(d, {"a": 11, "b": 2, "c": 25, "d": 30}) - - d.subtract_num("a", 10) - d.subtract_num("e", 10) - d.subtract_num("f", 10, 15) - self.failUnlessEqual(d, {"a": 1, "b": 2, "c": 25, "d": 30, - "e": -10, "f": 5}) - - self.failUnlessEqual(d.sum(), sum([1, 2, 25, 30, -10, 5])) - - d = dictutil.NumDict() - d.inc("a") - d.inc("a") - d.inc("b", 5) - self.failUnlessEqual(d, {"a": 2, "b": 6}) - d.dec("a") - d.dec("c") - d.dec("d", 5) - self.failUnlessEqual(d, {"a": 1, "b": 6, "c": -1, "d": 4}) - self.failUnlessEqual(d.items_sorted_by_key(), - [("a", 1), ("b", 6), ("c", -1), ("d", 4)]) - self.failUnlessEqual(d.items_sorted_by_value(), - [("c", -1), ("a", 1), ("d", 4), ("b", 6)]) - self.failUnlessEqual(d.item_with_largest_value(), ("b", 6)) - - # to get full coverage of item_with_largest_value(), we need to - # exercise two situations: the first value (in iteritems() order) is - # larger than the second, and vice versa. Since iteration is not - # deterministic, we need to try a bunch of random dictionaries to - # exercise this - r = random.Random(0) # consistent seed - count = itertools.count() - found = set() - while count.next() < 1000: - a = r.randrange(100) - b = r.randrange(100) - larger = ("a",a) if a > b else ("b",b) - if a == b: - continue - d0 = dictutil.NumDict() - d0.add_num("a", a) - d0.add_num("b", b) - self.failUnlessEqual(d0, {"a": a, "b": b}) - items = list(d0.d.iteritems()) - if items[0][1] > items[1][1]: - found.add("first-larger") - else: - found.add("first-smaller") - self.failUnlessEqual(d0.item_with_largest_value(), larger) - if found == set(["first-larger", "first-smaller"]): - break - else: - self.fail("unable to exercise all cases of item_with_largest_value") - - d = dictutil.NumDict({"a": 1, "b": 2}) - self.failUnlessIn(repr(d), ("{'a': 1, 'b': 2}", - "{'b': 2, 'a': 1}")) - self.failUnless("a" in d) - - d2 = dictutil.NumDict({"c": 3, "d": 4}) - self.failUnless(d != d2) - self.failUnless(d2 > d) - self.failUnless(d2 >= d) - self.failUnless(d <= d2) - self.failUnless(d < d2) - self.failUnlessEqual(d["a"], 1) - self.failUnlessEqual(sorted(list([k for k in d])), ["a","b"]) - def eq(a, b): - return a == b - self.failUnlessRaises(TypeError, eq, d, "not a dict") - - d3 = d.copy() - self.failUnlessEqual(d, d3) - self.failUnless(isinstance(d3, dictutil.NumDict)) - - d4 = d.fromkeys(["a","b"], 5) - self.failUnlessEqual(d4, {"a": 5, "b": 5}) - - self.failUnlessEqual(d.get("a"), 1) - self.failUnlessEqual(d.get("c"), 0) - self.failUnlessEqual(d.get("c", 5), 5) - self.failUnlessEqual(sorted(list(d.items())), - [("a", 1), ("b", 2)]) - self.failUnlessEqual(sorted(list(d.iteritems())), - [("a", 1), ("b", 2)]) - self.failUnlessEqual(sorted(d.keys()), ["a", "b"]) - self.failUnlessEqual(sorted(d.values()), [1, 2]) - self.failUnless(d.has_key("a")) - self.failIf(d.has_key("c")) - - x = d.setdefault("c", 3) - self.failUnlessEqual(x, 3) - self.failUnlessEqual(d["c"], 3) - x = d.setdefault("c", 5) - self.failUnlessEqual(x, 3) - self.failUnlessEqual(d["c"], 3) - del d["c"] - - x = d.popitem() - self.failUnless(x in [("a", 1), ("b", 2)]) - x = d.popitem() - self.failUnless(x in [("a", 1), ("b", 2)]) - self.failUnlessRaises(KeyError, d.popitem) - - d.update({"c": 3}) - d.update({"c": 4, "d": 5}) - self.failUnlessEqual(d, {"c": 4, "d": 5}) - def test_del_if_present(self): d = {1: "a", 2: "b"} dictutil.del_if_present(d, 1) diff --git a/src/allmydata/util/dictutil.py b/src/allmydata/util/dictutil.py index 6715f47ae..fc2cdd445 100644 --- a/src/allmydata/util/dictutil.py +++ b/src/allmydata/util/dictutil.py @@ -2,7 +2,7 @@ Tools to mess with dicts. """ -import copy, operator +import operator from bisect import bisect_left, insort_left from allmydata.util.assertutil import _assert, precondition @@ -176,168 +176,6 @@ class UtilDict: def values(self, *args, **kwargs): return self.d.values(*args, **kwargs) -class NumDict: - def __init__(self, initialdict={}): - self.d = copy.deepcopy(initialdict) - - def add_num(self, key, val, default=0): - """ - If the key doesn't appear in self then it is created with value default - (before addition). - """ - self.d[key] = self.d.get(key, default) + val - - def subtract_num(self, key, val, default=0): - self.d[key] = self.d.get(key, default) - val - - def sum(self): - """ - @return: the sum of all values - """ - return reduce(operator.__add__, self.d.values()) - - def inc(self, key, default=0): - """ - Increment the value associated with key in dict. If there is no such - key, then one will be created with initial value 0 (before inc() -- - therefore value 1 after inc). - """ - self.add_num(key, 1, default) - - def dec(self, key, default=0): - """ - Decrement the value associated with key in dict. If there is no such - key, then one will be created with initial value 0 (before dec() -- - therefore value -1 after dec). - """ - self.subtract_num(key, 1, default) - - def items_sorted_by_value(self): - """ - @return a sequence of (key, value,) pairs sorted according to value - """ - l = [(x[1], x[0],) for x in self.d.iteritems()] - l.sort() - return [(x[1], x[0],) for x in l] - - def item_with_largest_value(self): - it = self.d.iteritems() - (winner, winnerval,) = it.next() - try: - while True: - n, nv = it.next() - if nv > winnerval: - winner = n - winnerval = nv - except StopIteration: - pass - return (winner, winnerval,) - - def items_sorted_by_key(self): - """ - @return a sequence of (key, value,) pairs sorted according to key - """ - l = self.d.items() - l.sort() - return l - - def __repr__(self, *args, **kwargs): - return self.d.__repr__(*args, **kwargs) - - def __str__(self, *args, **kwargs): - return self.d.__str__(*args, **kwargs) - - def __contains__(self, *args, **kwargs): - return self.d.__contains__(*args, **kwargs) - - def __len__(self, *args, **kwargs): - return self.d.__len__(*args, **kwargs) - - def __cmp__(self, other): - try: - return self.d.__cmp__(other) - except TypeError, le: - # maybe we should look for a .d member in other. I know this is insanely kludgey, but the Right Way To Do It is for dict.__cmp__ to use structural typing ("duck typing") - try: - return self.d.__cmp__(other.d) - except: - raise le - - def __eq__(self, *args, **kwargs): - return self.d.__eq__(*args, **kwargs) - - def __ne__(self, *args, **kwargs): - return self.d.__ne__(*args, **kwargs) - - def __gt__(self, *args, **kwargs): - return self.d.__gt__(*args, **kwargs) - - def __ge__(self, *args, **kwargs): - return self.d.__ge__(*args, **kwargs) - - def __le__(self, *args, **kwargs): - return self.d.__le__(*args, **kwargs) - - def __lt__(self, *args, **kwargs): - return self.d.__lt__(*args, **kwargs) - - def __getitem__(self, *args, **kwargs): - return self.d.__getitem__(*args, **kwargs) - - def __setitem__(self, *args, **kwargs): - return self.d.__setitem__(*args, **kwargs) - - def __delitem__(self, *args, **kwargs): - return self.d.__delitem__(*args, **kwargs) - - def __iter__(self, *args, **kwargs): - return self.d.__iter__(*args, **kwargs) - - def clear(self, *args, **kwargs): - return self.d.clear(*args, **kwargs) - - def copy(self, *args, **kwargs): - return self.__class__(self.d.copy(*args, **kwargs)) - - def fromkeys(self, *args, **kwargs): - return self.__class__(self.d.fromkeys(*args, **kwargs)) - - def get(self, key, default=0): - return self.d.get(key, default) - - def has_key(self, *args, **kwargs): - return self.d.has_key(*args, **kwargs) - - def items(self, *args, **kwargs): - return self.d.items(*args, **kwargs) - - def iteritems(self, *args, **kwargs): - return self.d.iteritems(*args, **kwargs) - - def iterkeys(self, *args, **kwargs): - return self.d.iterkeys(*args, **kwargs) - - def itervalues(self, *args, **kwargs): - return self.d.itervalues(*args, **kwargs) - - def keys(self, *args, **kwargs): - return self.d.keys(*args, **kwargs) - - def pop(self, *args, **kwargs): - return self.d.pop(*args, **kwargs) - - def popitem(self, *args, **kwargs): - return self.d.popitem(*args, **kwargs) - - def setdefault(self, *args, **kwargs): - return self.d.setdefault(*args, **kwargs) - - def update(self, *args, **kwargs): - return self.d.update(*args, **kwargs) - - def values(self, *args, **kwargs): - return self.d.values(*args, **kwargs) - def del_if_present(d, k): if d.has_key(k): del d[k]