Luke Plant avatar Luke Plant committed 8881afa

Made auto_ranges handle ints more gracefully

Comments (0)

Files changed (2)

django_easyfilters/ranges.py

 
     assert lower < upper
 
+    input_type = type(lower)
+
     # Convert to decimals.
     lower_d = Decimal(lower)
     upper_d = Decimal(upper)
         # If we are less than max_items, go with this.  (Note that smaller steps
         # are tried first).
         if num_steps <= max_items:
-            ranges = [(lower_r + c_step * i, lower_r + c_step * (i + 1))
-                      for i in xrange(num_steps)]
-            # make sure top item is rounded value
-            ranges[-1] = (ranges[-1][0],  upper_r)
+            ranges = []
+            for i in xrange(num_steps):
+                lower_i = input_type(lower_r + c_step * i)
+                upper_i = input_type(lower_r + c_step * (i + 1))
+                if i == num_steps - 1:
+                    # make sure top item is rounded value
+                    upper_i = input_type(upper_r)
+                ranges.append((lower_i, upper_i))
             return ranges
 
     assert False, "Can't find a candidate set of ranges, logic error"

django_easyfilters/tests/ranges.py

                          [(Decimal('3'), Decimal('4')),
                           (Decimal('4'), Decimal('5')),
                           (Decimal('5'), Decimal('6'))])
+
+    def test_auto_ranges_type(self):
+        """
+        auto_ranges should return the same type of thing it is passed
+        """
+        r = auto_ranges(1, 10, 10)
+        self.assertEqual(type(r[0][0]), int)
+
+        r2 = auto_ranges(Decimal('1'), Decimal('10'), 10)
+        self.assertEqual(type(r2[0][0]), Decimal)
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.