From dabfcbadf868d78de48ccbeaee52cfa028a1549d Mon Sep 17 00:00:00 2001 From: Ask Solem Date: Fri, 7 Jan 2011 11:08:58 +0100 Subject: [PATCH] connection.ensure_* interval arguments can now be float --- kombu/tests/test_utils.py | 9 --------- kombu/utils/__init__.py | 25 ++++++++++++++----------- 2 files changed, 14 insertions(+), 20 deletions(-) diff --git a/kombu/tests/test_utils.py b/kombu/tests/test_utils.py index 26daf6d9..7bb32210 100644 --- a/kombu/tests/test_utils.py +++ b/kombu/tests/test_utils.py @@ -122,15 +122,6 @@ class test_Misc(unittest.TestCase): u"bar": "bar"} self.assertTrue(f(**utils.kwdict(kw))) - def test_repeatlast(self): - x = [1, 2, 3, 4] - it = utils.repeatlast(x) - self.assertEqual(it.next(), 1) - self.assertEqual(it.next(), 2) - self.assertEqual(it.next(), 3) - self.assertEqual(it.next(), 4) - self.assertEqual(it.next(), 4) - class MyStringIO(StringIO): diff --git a/kombu/utils/__init__.py b/kombu/utils/__init__.py index ce765b9e..a59961c2 100644 --- a/kombu/utils/__init__.py +++ b/kombu/utils/__init__.py @@ -57,13 +57,16 @@ def maybe_list(v): return [v] -def repeatlast(it): - """Iterate over all elements in the iterator, and when its exhausted - yield the last value infinitely.""" - for item in it: - yield item - while 1: # pragma: no cover - yield item +def fxrange(start=1.0, stop=None, step=1.0, repeatlast=False): + cur = start * 1.0 + while 1: + if cur <= stop: + yield cur + cur += step + else: + if not repeatlast: + break + yield cur def retry_over_time(fun, catch, args=[], kwargs={}, errback=None, @@ -92,11 +95,11 @@ def retry_over_time(fun, catch, args=[], kwargs={}, errback=None, """ retries = 0 - interval_range = xrange(interval_start, - interval_max + interval_start, - interval_step) + interval_range = fxrange(interval_start, + interval_max + interval_start, + interval_step, repeatlast=True) - for retries, interval in enumerate(repeatlast(interval_range)): + for retries, interval in enumerate(interval_range): try: return fun(*args, **kwargs) except catch, exc: