diff --git a/kombu/connection.py b/kombu/connection.py index c8a47aa5..74a5b4a4 100644 --- a/kombu/connection.py +++ b/kombu/connection.py @@ -28,7 +28,7 @@ from .log import get_logger from .transport import get_transport_cls, supports_librabbitmq from .utils import cached_property, retry_over_time, shufflecycle from .utils.compat import OrderedDict, get_errno -from .utils.functional import promise +from .utils.functional import lazy from .utils.url import parse_url __all__ = ['Connection', 'ConnectionPool', 'ChannelPool'] @@ -898,7 +898,7 @@ class Resource(object): try: R = self.prepare(R) except BaseException: - if isinstance(R, promise): + if isinstance(R, lazy): # no evaluated yet, just put it back self._resource.put_nowait(R) else: @@ -1043,7 +1043,7 @@ class ConnectionPool(Resource): conn = self.new() conn.connect() else: - conn = promise(self.new) + conn = lazy(self.new) self._resource.put_nowait(conn) def prepare(self, resource): @@ -1062,14 +1062,14 @@ class ChannelPool(Resource): preload=preload) def new(self): - return promise(self.connection.channel) + return lazy(self.connection.channel) def setup(self): channel = self.new() if self.limit: for i in range(self.limit): self._resource.put_nowait( - i < self.preload and channel() or promise(channel)) + i < self.preload and channel() or lazy(channel)) def prepare(self, channel): if isinstance(channel, Callable): diff --git a/kombu/log.py b/kombu/log.py index 6312a0d1..5ef816d9 100644 --- a/kombu/log.py +++ b/kombu/log.py @@ -9,7 +9,7 @@ from logging.handlers import WatchedFileHandler from .five import string_t from .utils import cached_property from .utils.encoding import safe_repr, safe_str -from .utils.functional import maybe_promise +from .utils.functional import maybe_evaluate __all__ = ['LogMixin', 'LOG_LEVELS', 'get_loglevel', 'setup_logging'] @@ -89,7 +89,7 @@ class LogMixin(object): if self.logger.isEnabledFor(severity): log = self.logger.log if len(args) > 1 and isinstance(args[0], string_t): - expand = [maybe_promise(arg) for arg in args[1:]] + expand = [maybe_evaluate(arg) for arg in args[1:]] return log(severity, self.annotate(args[0].replace('%r', '%s')), *list(safeify_format(args[0], expand)), **kwargs) diff --git a/kombu/pools.py b/kombu/pools.py index 79ec85a9..b7804a62 100644 --- a/kombu/pools.py +++ b/kombu/pools.py @@ -16,7 +16,7 @@ from .connection import Resource from .five import range, values from .messaging import Producer from .utils import EqualityDict -from .utils.functional import promise +from .utils.functional import lazy __all__ = ['ProducerPool', 'PoolGroup', 'register_group', 'connections', 'producers', 'get_limit', 'set_limit', 'reset'] @@ -47,7 +47,7 @@ class ProducerPool(Resource): raise def new(self): - return promise(self.create_producer) + return lazy(self.create_producer) def setup(self): if self.limit: diff --git a/kombu/tests/utilities/test_functional.py b/kombu/tests/utilities/test_functional.py index b20ad5b7..f463ab87 100644 --- a/kombu/tests/utilities/test_functional.py +++ b/kombu/tests/utilities/test_functional.py @@ -2,7 +2,7 @@ from __future__ import absolute_import import pickle -from kombu.utils.functional import promise, maybe_promise +from kombu.utils.functional import lazy, maybe_evaluate from kombu.tests.utils import TestCase @@ -11,45 +11,45 @@ def double(x): return x * 2 -class test_promise(TestCase): +class test_lazy(TestCase): def test__str__(self): self.assertEqual( - str(promise(lambda: 'the quick brown fox')), + str(lazy(lambda: 'the quick brown fox')), 'the quick brown fox', ) def test__repr__(self): self.assertEqual( - repr(promise(lambda: 'fi fa fo')), + repr(lazy(lambda: 'fi fa fo')), "'fi fa fo'", ) def test_evaluate(self): - self.assertEqual(promise(lambda: 2 + 2)(), 4) - self.assertEqual(promise(lambda x: x * 4, 2), 8) - self.assertEqual(promise(lambda x: x * 8, 2)(), 16) + self.assertEqual(lazy(lambda: 2 + 2)(), 4) + self.assertEqual(lazy(lambda x: x * 4, 2), 8) + self.assertEqual(lazy(lambda x: x * 8, 2)(), 16) def test_cmp(self): - self.assertEqual(promise(lambda: 10), promise(lambda: 10)) - self.assertNotEqual(promise(lambda: 10), promise(lambda: 20)) + self.assertEqual(lazy(lambda: 10), lazy(lambda: 10)) + self.assertNotEqual(lazy(lambda: 10), lazy(lambda: 20)) def test__reduce__(self): - x = promise(double, 4) + x = lazy(double, 4) y = pickle.loads(pickle.dumps(x)) self.assertEqual(x(), y()) def test__deepcopy__(self): from copy import deepcopy - x = promise(double, 4) + x = lazy(double, 4) y = deepcopy(x) self.assertEqual(x._fun, y._fun) self.assertEqual(x._args, y._args) self.assertEqual(x(), y()) -class test_maybe_promise(TestCase): +class test_maybe_evaluate(TestCase): def test_evaluates(self): - self.assertEqual(maybe_promise(promise(lambda: 10)), 10) - self.assertEqual(maybe_promise(20), 20) + self.assertEqual(maybe_evaluate(lazy(lambda: 10)), 10) + self.assertEqual(maybe_evaluate(20), 20) diff --git a/kombu/utils/functional.py b/kombu/utils/functional.py index 374bf356..fca9bd25 100644 --- a/kombu/utils/functional.py +++ b/kombu/utils/functional.py @@ -1,12 +1,13 @@ from __future__ import absolute_import +__all__ = ['lazy', 'maybe_evaluate'] -class promise(object): - """A promise. + +class lazy(object): + """Holds lazy evaluation. Evaluated when called or if the :meth:`evaluate` method is called. - The function is evaluated on every access, so the value is not - memoized (see :class:`mpromise`). + The function is re-evaluated on every call. Overloaded operations that will evaluate the promise: :meth:`__str__`, :meth:`__repr__`, :meth:`__cmp__`. @@ -50,8 +51,13 @@ class promise(object): '_kwargs': self._kwargs}) -def maybe_promise(value): - """Evaluates if the value is a promise.""" - if isinstance(value, promise): +def maybe_evaluate(value): + """Evaluates if the value is a :class:`lazy` instance.""" + if isinstance(value, lazy): return value.evaluate() return value + + +# Compat names (before kombu 3.0) +promise = lazy +maybe_promise = maybe_evaluate