Mike Orr avatar Mike Orr committed ecc9572

Bugfix in distribute() found by Randy Syring.

Comments (0)

Files changed (3)

 WebHelpers ChangeLog
 
 0.6.3 (tip)
+* Bugfix in distribute() found by Randy Syring.
 
 0.6.2 (10/2/2008)
 * nl2br() and format-paragraphs were not literal-safe.

tests/test_containers.py

 import copy
 import tempfile
 
-from nose.tools import assert_equal, assert_raises
+from nose.tools import eq_, raises
 
 from webhelpers.containers import DumbObject
 from webhelpers.containers import defaultdict as webhelpers_containers_defaultdict
+from webhelpers.containers import distribute
 
 # Tests from Python 2.5 test_defaultdict_defaultdict.py, as this is just a 2.4 backport
 # anyway
 
 def test_defaultdict_basic():
     d1 = webhelpers_containers_defaultdict()
-    assert_equal(d1.default_factory, None)
+    eq_(d1.default_factory, None)
     d1.default_factory = list
     d1[12].append(42)
-    assert_equal(d1, {12: [42]})
+    eq_(d1, {12: [42]})
     d1[12].append(24)
-    assert_equal(d1, {12: [42, 24]})
+    eq_(d1, {12: [42, 24]})
     d1[13]
     d1[14]
-    assert_equal(d1, {12: [42, 24], 13: [], 14: []})
+    eq_(d1, {12: [42, 24], 13: [], 14: []})
     assert d1[12] is not d1[13] is not d1[14]
     d2 = webhelpers_containers_defaultdict(list, foo=1, bar=2)
-    assert_equal(d2.default_factory, list)
-    assert_equal(d2, {"foo": 1, "bar": 2})
-    assert_equal(d2["foo"], 1)
-    assert_equal(d2["bar"], 2)
-    assert_equal(d2[42], [])
+    eq_(d2.default_factory, list)
+    eq_(d2, {"foo": 1, "bar": 2})
+    eq_(d2["foo"], 1)
+    eq_(d2["bar"], 2)
+    eq_(d2[42], [])
     assert "foo" in d2
     assert "foo" in d2.keys()
     assert "bar" in d2
     assert 12 not in d2
     assert 12 not in d2.keys()
     d2.default_factory = None
-    assert_equal(d2.default_factory, None)
+    eq_(d2.default_factory, None)
     try:
         d2[15]
     except KeyError, err:
-        assert_equal(err.args, (15,))
+        eq_(err.args, (15,))
     else:
         message = "d2[15] didn't raise KeyError"
         raise AssertionError(message)
 
 def test_defaultdict_missing():
     d1 = webhelpers_containers_defaultdict()
-    assert_raises(KeyError, d1.__missing__, 42)
+    try:
+        d1.__missing__(42)
+    except KeyError:
+        pass
+    else:
+        raise AssertionError("d1.__missing__ did not raise KeyError")
     d1.default_factory = list
-    assert_equal(d1.__missing__(42), [])
+    eq_(d1.__missing__(42), [])
 
 def test_defaultdict_repr():
     d1 = webhelpers_containers_defaultdict()
-    assert_equal(d1.default_factory, None)
-    assert_equal(repr(d1), "defaultdict(None, {})")
+    eq_(d1.default_factory, None)
+    eq_(repr(d1), "defaultdict(None, {})")
     d1[11] = 41
-    assert_equal(repr(d1), "defaultdict(None, {11: 41})")
+    eq_(repr(d1), "defaultdict(None, {11: 41})")
 
 def test_defaultdict_repr_2():
     def foo(): return 43
     d3 = webhelpers_containers_defaultdict(foo)
     assert d3.default_factory is foo
     d3[13]
-    assert_equal(repr(d3), "defaultdict(%s, {13: 43})" % repr(foo))
+    eq_(repr(d3), "defaultdict(%s, {13: 43})" % repr(foo))
 
 def test_defaultdict_print():
     d1 = webhelpers_containers_defaultdict()
             print >>f, d1
             print >>f, d2
             f.seek(0)
-            assert_equal(f.readline(), repr(d1) + "\n")
-            assert_equal(f.readline(), repr(d2) + "\n")
+            eq_(f.readline(), repr(d1) + "\n")
+            eq_(f.readline(), repr(d2) + "\n")
         finally:
             f.close()
     finally:
 def test_defaultdict_copy():
     d1 = webhelpers_containers_defaultdict()
     d2 = d1.copy()
-    assert_equal(type(d2), webhelpers_containers_defaultdict)
-    assert_equal(d2.default_factory, None)
-    assert_equal(d2, {})
+    eq_(type(d2), webhelpers_containers_defaultdict)
+    eq_(d2.default_factory, None)
+    eq_(d2, {})
     d1.default_factory = list
     d3 = d1.copy()
-    assert_equal(type(d3), webhelpers_containers_defaultdict)
-    assert_equal(d3.default_factory, list)
-    assert_equal(d3, {})
+    eq_(type(d3), webhelpers_containers_defaultdict)
+    eq_(d3.default_factory, list)
+    eq_(d3, {})
     d1[42]
     d4 = d1.copy()
-    assert_equal(type(d4), webhelpers_containers_defaultdict)
-    assert_equal(d4.default_factory, list)
-    assert_equal(d4, {42: []})
+    eq_(type(d4), webhelpers_containers_defaultdict)
+    eq_(d4.default_factory, list)
+    eq_(d4, {42: []})
     d4[12]
-    assert_equal(d4, {42: [], 12: []})
+    eq_(d4, {42: [], 12: []})
 
 def test_defaultdict_shallow_copy():
     d1 = webhelpers_containers_defaultdict(foobar, {1: 1})
     d2 = copy.copy(d1)
-    assert_equal(d2.default_factory, foobar)
-    assert_equal(d2, d1)
+    eq_(d2.default_factory, foobar)
+    eq_(d2, d1)
     d1.default_factory = list
     d2 = copy.copy(d1)
-    assert_equal(d2.default_factory, list)
-    assert_equal(d2, d1)
+    eq_(d2.default_factory, list)
+    eq_(d2, d1)
 
 def test_defaultdict_deep_copy():
     d1 = webhelpers_containers_defaultdict(foobar, {1: [1]})
     d2 = copy.deepcopy(d1)
-    assert_equal(d2.default_factory, foobar)
-    assert_equal(d2, d1)
+    eq_(d2.default_factory, foobar)
+    eq_(d2, d1)
     assert d1[1] is not d2[1]
     d1.default_factory = list
     d2 = copy.deepcopy(d1)
-    assert_equal(d2.default_factory, list)
-    assert_equal(d2, d1)
+    eq_(d2.default_factory, list)
+    eq_(d2, d1)
 
+def test_distribute():
+        food = ["apple", "banana", "carrot", "daikon", "egg", "fish", "gelato", "honey"]
+        eq_(distribute(food, 3, "H", ""), [['apple', 'banana', 'carrot'], ['daikon', 'egg', 'fish'], ['gelato', 'honey', '']])
+        eq_(distribute(food, 3, "V", ""), [['apple', 'daikon', 'gelato'], ['banana', 'egg', 'honey'], ['carrot', 'fish', '']])
+        eq_(distribute(food, 2, "H", ""), [['apple', 'banana'], ['carrot', 'daikon'], ['egg', 'fish'], ['gelato', 'honey']])
+        eq_(distribute(food, 2, "V", ""), [['apple', 'egg'], ['banana', 'fish'], ['carrot', 'gelato'], ['daikon', 'honey']])
+
+def test_distribute_with_extra():
+        food = ["apple", "banana", "carrot", "daikon", "egg", "fish", "gelato", "honey", "EXTRA"]
+        eq_(distribute(food, 3, "H", ""), [['apple', 'banana', 'carrot'], ['daikon', 'egg', 'fish'], ['gelato', 'honey', 'EXTRA']])
+        eq_(distribute(food, 3, "V", ""), [['apple', 'daikon', 'gelato'], ['banana', 'egg', 'honey'], ['carrot', 'fish', 'EXTRA']])
+        eq_(distribute(food, 2, "H", ""), [['apple', 'banana'], ['carrot', 'daikon'], ['egg', 'fish'], ['gelato', 'honey'], ['EXTRA', '']])
+        eq_(distribute(food, 2, "V", ""), [['apple', 'fish'], ['banana', 'gelato'], ['carrot', 'honey'], ['daikon', 'EXTRA'], ['egg', '']])

webhelpers/containers.py

         if remainder:
             rows += 1
         table = [[fill] * columns for x in range(rows)]
+        #print table
         for i, elm in enumerate(lis):
-            col, row = divmod(i, columns)
+            col, row = divmod(i, rows)
             #print "i=%d, row=%d, col=%d, element=%r" % (i, row, col, elm)
             table[row][col] = elm
         return table
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.