diff options
author | Michael Elkins <melkins@tislabs.com> | 2012-11-08 04:25:58 +0000 |
---|---|---|
committer | Michael Elkins <melkins@tislabs.com> | 2012-11-08 04:25:58 +0000 |
commit | 0451518869110ec13b8e7485700b0e37cc142c6b (patch) | |
tree | 84765d93e23369bb2cdb593db7323bfe74fa12f7 | |
parent | 1a7b180f631932b5306b1c5a95e289a907ab6881 (diff) |
Avoid specifying the keyword parameters min= and max= in rpki.gui.app.range_list.RangeList. This is used with the rpki.resource_set classes, and the API changed from min,max to range_min,range_max.
svn path=/branches/tk274/; revision=4813
-rwxr-xr-x | rpkid/rpki/gui/app/range_list.py | 90 |
1 files changed, 49 insertions, 41 deletions
diff --git a/rpkid/rpki/gui/app/range_list.py b/rpkid/rpki/gui/app/range_list.py index fcfcfc24..21fd1f29 100755 --- a/rpkid/rpki/gui/app/range_list.py +++ b/rpkid/rpki/gui/app/range_list.py @@ -17,6 +17,7 @@ __version__ = '$Id$' import bisect import unittest + class RangeList(list): """A sorted list of ranges, which automatically merges adjacent ranges. @@ -36,35 +37,39 @@ class RangeList(list): # upper bound j = bisect.bisect_right(keys, v.max, lo=i) - # if the max value for the previous item is greater than v.min, include the previous item in the range to replace - # and use its min value. also include the previous item if the max value is 1 less than the min value for the - # inserted item - if i > 0 and self[i-1].max >= v.min - 1: + # if the max value for the previous item is greater than v.min, include + # the previous item in the range to replace and use its min value. + # also include the previous item if the max value is 1 less than the + # min value for the inserted item + if i > 0 and self[i - 1].max >= v.min - 1: i = i - 1 vmin = self[i].min else: vmin = v.min - # if the max value for the previous item is greater than the max value for the new item, use the previous item's max - if j > 0 and self[j-1].max > v.max: - vmax = self[j-1].max + # if the max value for the previous item is greater than the max value + # for the new item, use the previous item's max + if j > 0 and self[j - 1].max > v.max: + vmax = self[j - 1].max else: vmax = v.max - # if the max value for the new item is 1 less than the min value for the next item, combine into a single item - if j < len(self) and vmax+1 == self[j].min: + # if the max value for the new item is 1 less than the min value for + # the next item, combine into a single item + if j < len(self) and vmax + 1 == self[j].min: vmax = self[j].max - j = j+1 + j = j + 1 # replace the range with a new object covering the entire range - self[i:j] = [v.__class__(min=vmin, max=vmax)] + self[i:j] = [v.__class__(vmin, vmax)] def extend(self, args): for x in args: self.append(x) def difference(self, other): - """Return a RangeList object which contains ranges in this object which are not in "other".""" + """Return a RangeList object which contains ranges in this object which + are not in "other".""" it = iter(other) try: @@ -85,27 +90,30 @@ class RangeList(list): try: while xmin <= x.max: if xmin < cur.min: - r.append(x.__class__(min=V(xmin), - max=V(min(x.max,cur.min-1)))) - xmin = cur.max+1 + r.append(x.__class__(V(xmin), + V(min(x.max, cur.min - 1)))) + xmin = cur.max + 1 elif xmin == cur.min: - xmin = cur.max+1 - else: # xmin > cur.min + xmin = cur.max + 1 + else: # xmin > cur.min if xmin <= cur.max: - xmin = cur.max+1 - else: # xmin > cur.max + xmin = cur.max + 1 + else: # xmin > cur.max cur = it.next() except StopIteration: - r.append(x.__class__(min=V(xmin), max=x.max)) + r.append(x.__class__(V(xmin), x.max)) return r + class TestRangeList(unittest.TestCase): class MinMax(object): - def __init__(self, min, max): - self.min = min - self.max = max + datum_type = int + + def __init__(self, range_min, range_max): + self.min = range_min + self.max = range_max def __str__(self): return '(%d, %d)' % (self.min, self.max) @@ -117,12 +125,12 @@ class TestRangeList(unittest.TestCase): return self.min == other.min and self.max == other.max def setUp(self): - self.v1 = TestRangeList.MinMax(1,2) - self.v2 = TestRangeList.MinMax(4,5) - self.v3 = TestRangeList.MinMax(7,8) - self.v4 = TestRangeList.MinMax(3,4) - self.v5 = TestRangeList.MinMax(2,3) - self.v6 = TestRangeList.MinMax(1,10) + self.v1 = TestRangeList.MinMax(1, 2) + self.v2 = TestRangeList.MinMax(4, 5) + self.v3 = TestRangeList.MinMax(7, 8) + self.v4 = TestRangeList.MinMax(3, 4) + self.v5 = TestRangeList.MinMax(2, 3) + self.v6 = TestRangeList.MinMax(1, 10) def test_empty_append(self): s = RangeList() @@ -161,14 +169,14 @@ class TestRangeList(unittest.TestCase): s.append(self.v1) s.append(self.v5) self.assertTrue(len(s) == 1) - self.assertEqual(s[0], TestRangeList.MinMax(1,3)) + self.assertEqual(s[0], TestRangeList.MinMax(1, 3)) def test_combine_range(self): s = RangeList() s.append(self.v1) s.append(self.v4) self.assertTrue(len(s) == 1) - self.assertEqual(s[0], TestRangeList.MinMax(1,4)) + self.assertEqual(s[0], TestRangeList.MinMax(1, 4)) def test_append_subset(self): s = RangeList() @@ -189,7 +197,7 @@ class TestRangeList(unittest.TestCase): s.append(self.v4) s.append(self.v1) self.assertTrue(len(s) == 1) - self.assertEqual(s[0], TestRangeList.MinMax(1,4)) + self.assertEqual(s[0], TestRangeList.MinMax(1, 4)) def test_append_aggregate(self): s = RangeList() @@ -213,31 +221,31 @@ class TestRangeList(unittest.TestCase): def test_diff_middle(self): s1 = RangeList([self.v6]) s2 = RangeList([self.v3]) - self.assertEqual(s1.difference(s2), RangeList([TestRangeList.MinMax(1,6), TestRangeList.MinMax(9, 10)])) + self.assertEqual(s1.difference(s2), RangeList([TestRangeList.MinMax(1, 6), TestRangeList.MinMax(9, 10)])) def test_diff_overlap(self): s1 = RangeList([self.v2]) s2 = RangeList([self.v4]) - self.assertEqual(s1.difference(s2), RangeList([TestRangeList.MinMax(5,5)])) + self.assertEqual(s1.difference(s2), RangeList([TestRangeList.MinMax(5, 5)])) def test_diff_overlap2(self): s1 = RangeList([self.v2]) s2 = RangeList([self.v4]) - self.assertEqual(s2.difference(s1), RangeList([TestRangeList.MinMax(3,3)])) + self.assertEqual(s2.difference(s1), RangeList([TestRangeList.MinMax(3, 3)])) def test_diff_multi(self): - s1 = RangeList([TestRangeList.MinMax(1,2), TestRangeList.MinMax(4,5)]) - s2 = RangeList([TestRangeList.MinMax(4,4)]) - self.assertEqual(s1.difference(s2), RangeList([TestRangeList.MinMax(1,2), TestRangeList.MinMax(5,5)])) + s1 = RangeList([TestRangeList.MinMax(1, 2), TestRangeList.MinMax(4, 5)]) + s2 = RangeList([TestRangeList.MinMax(4, 4)]) + self.assertEqual(s1.difference(s2), RangeList([TestRangeList.MinMax(1, 2), TestRangeList.MinMax(5, 5)])) def test_diff_multi_overlap(self): - s1 = RangeList([TestRangeList.MinMax(1,2), TestRangeList.MinMax(3,4)]) - s2 = RangeList([TestRangeList.MinMax(2,3)]) + s1 = RangeList([TestRangeList.MinMax(1, 2), TestRangeList.MinMax(3, 4)]) + s2 = RangeList([TestRangeList.MinMax(2, 3)]) self.assertEqual(s1.difference(s2), RangeList([TestRangeList.MinMax(1,1), TestRangeList.MinMax(4,4)])) def test_diff_multi_overlap2(self): s1 = RangeList([TestRangeList.MinMax(1,2), TestRangeList.MinMax(3,4), TestRangeList.MinMax(6,7)]) - s2 = RangeList([TestRangeList.MinMax(2,3), TestRangeList.MinMax(6,6)]) + s2 = RangeList([TestRangeList.MinMax(2, 3), TestRangeList.MinMax(6, 6)]) self.assertEqual(s1.difference(s2), RangeList([TestRangeList.MinMax(1,1), TestRangeList.MinMax(4,4), TestRangeList.MinMax(7,7)])) if __name__ == '__main__': |