Renames promise -> lazy, maybe_promise -> maybe_evaluate so that it does not crash with the new async promise

This commit is contained in:
Ask Solem 2013-09-11 15:36:24 +01:00
parent 84d0c1a503
commit 8058fe6dce
5 changed files with 36 additions and 30 deletions

View File

@ -28,7 +28,7 @@ from .log import get_logger
from .transport import get_transport_cls, supports_librabbitmq from .transport import get_transport_cls, supports_librabbitmq
from .utils import cached_property, retry_over_time, shufflecycle from .utils import cached_property, retry_over_time, shufflecycle
from .utils.compat import OrderedDict, get_errno from .utils.compat import OrderedDict, get_errno
from .utils.functional import promise from .utils.functional import lazy
from .utils.url import parse_url from .utils.url import parse_url
__all__ = ['Connection', 'ConnectionPool', 'ChannelPool'] __all__ = ['Connection', 'ConnectionPool', 'ChannelPool']
@ -898,7 +898,7 @@ class Resource(object):
try: try:
R = self.prepare(R) R = self.prepare(R)
except BaseException: except BaseException:
if isinstance(R, promise): if isinstance(R, lazy):
# no evaluated yet, just put it back # no evaluated yet, just put it back
self._resource.put_nowait(R) self._resource.put_nowait(R)
else: else:
@ -1043,7 +1043,7 @@ class ConnectionPool(Resource):
conn = self.new() conn = self.new()
conn.connect() conn.connect()
else: else:
conn = promise(self.new) conn = lazy(self.new)
self._resource.put_nowait(conn) self._resource.put_nowait(conn)
def prepare(self, resource): def prepare(self, resource):
@ -1062,14 +1062,14 @@ class ChannelPool(Resource):
preload=preload) preload=preload)
def new(self): def new(self):
return promise(self.connection.channel) return lazy(self.connection.channel)
def setup(self): def setup(self):
channel = self.new() channel = self.new()
if self.limit: if self.limit:
for i in range(self.limit): for i in range(self.limit):
self._resource.put_nowait( self._resource.put_nowait(
i < self.preload and channel() or promise(channel)) i < self.preload and channel() or lazy(channel))
def prepare(self, channel): def prepare(self, channel):
if isinstance(channel, Callable): if isinstance(channel, Callable):

View File

@ -9,7 +9,7 @@ from logging.handlers import WatchedFileHandler
from .five import string_t from .five import string_t
from .utils import cached_property from .utils import cached_property
from .utils.encoding import safe_repr, safe_str 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'] __all__ = ['LogMixin', 'LOG_LEVELS', 'get_loglevel', 'setup_logging']
@ -89,7 +89,7 @@ class LogMixin(object):
if self.logger.isEnabledFor(severity): if self.logger.isEnabledFor(severity):
log = self.logger.log log = self.logger.log
if len(args) > 1 and isinstance(args[0], string_t): 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, return log(severity,
self.annotate(args[0].replace('%r', '%s')), self.annotate(args[0].replace('%r', '%s')),
*list(safeify_format(args[0], expand)), **kwargs) *list(safeify_format(args[0], expand)), **kwargs)

View File

@ -16,7 +16,7 @@ from .connection import Resource
from .five import range, values from .five import range, values
from .messaging import Producer from .messaging import Producer
from .utils import EqualityDict from .utils import EqualityDict
from .utils.functional import promise from .utils.functional import lazy
__all__ = ['ProducerPool', 'PoolGroup', 'register_group', __all__ = ['ProducerPool', 'PoolGroup', 'register_group',
'connections', 'producers', 'get_limit', 'set_limit', 'reset'] 'connections', 'producers', 'get_limit', 'set_limit', 'reset']
@ -47,7 +47,7 @@ class ProducerPool(Resource):
raise raise
def new(self): def new(self):
return promise(self.create_producer) return lazy(self.create_producer)
def setup(self): def setup(self):
if self.limit: if self.limit:

View File

@ -2,7 +2,7 @@ from __future__ import absolute_import
import pickle import pickle
from kombu.utils.functional import promise, maybe_promise from kombu.utils.functional import lazy, maybe_evaluate
from kombu.tests.utils import TestCase from kombu.tests.utils import TestCase
@ -11,45 +11,45 @@ def double(x):
return x * 2 return x * 2
class test_promise(TestCase): class test_lazy(TestCase):
def test__str__(self): def test__str__(self):
self.assertEqual( self.assertEqual(
str(promise(lambda: 'the quick brown fox')), str(lazy(lambda: 'the quick brown fox')),
'the quick brown fox', 'the quick brown fox',
) )
def test__repr__(self): def test__repr__(self):
self.assertEqual( self.assertEqual(
repr(promise(lambda: 'fi fa fo')), repr(lazy(lambda: 'fi fa fo')),
"'fi fa fo'", "'fi fa fo'",
) )
def test_evaluate(self): def test_evaluate(self):
self.assertEqual(promise(lambda: 2 + 2)(), 4) self.assertEqual(lazy(lambda: 2 + 2)(), 4)
self.assertEqual(promise(lambda x: x * 4, 2), 8) self.assertEqual(lazy(lambda x: x * 4, 2), 8)
self.assertEqual(promise(lambda x: x * 8, 2)(), 16) self.assertEqual(lazy(lambda x: x * 8, 2)(), 16)
def test_cmp(self): def test_cmp(self):
self.assertEqual(promise(lambda: 10), promise(lambda: 10)) self.assertEqual(lazy(lambda: 10), lazy(lambda: 10))
self.assertNotEqual(promise(lambda: 10), promise(lambda: 20)) self.assertNotEqual(lazy(lambda: 10), lazy(lambda: 20))
def test__reduce__(self): def test__reduce__(self):
x = promise(double, 4) x = lazy(double, 4)
y = pickle.loads(pickle.dumps(x)) y = pickle.loads(pickle.dumps(x))
self.assertEqual(x(), y()) self.assertEqual(x(), y())
def test__deepcopy__(self): def test__deepcopy__(self):
from copy import deepcopy from copy import deepcopy
x = promise(double, 4) x = lazy(double, 4)
y = deepcopy(x) y = deepcopy(x)
self.assertEqual(x._fun, y._fun) self.assertEqual(x._fun, y._fun)
self.assertEqual(x._args, y._args) self.assertEqual(x._args, y._args)
self.assertEqual(x(), y()) self.assertEqual(x(), y())
class test_maybe_promise(TestCase): class test_maybe_evaluate(TestCase):
def test_evaluates(self): def test_evaluates(self):
self.assertEqual(maybe_promise(promise(lambda: 10)), 10) self.assertEqual(maybe_evaluate(lazy(lambda: 10)), 10)
self.assertEqual(maybe_promise(20), 20) self.assertEqual(maybe_evaluate(20), 20)

View File

@ -1,12 +1,13 @@
from __future__ import absolute_import 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. Evaluated when called or if the :meth:`evaluate` method is called.
The function is evaluated on every access, so the value is not The function is re-evaluated on every call.
memoized (see :class:`mpromise`).
Overloaded operations that will evaluate the promise: Overloaded operations that will evaluate the promise:
:meth:`__str__`, :meth:`__repr__`, :meth:`__cmp__`. :meth:`__str__`, :meth:`__repr__`, :meth:`__cmp__`.
@ -50,8 +51,13 @@ class promise(object):
'_kwargs': self._kwargs}) '_kwargs': self._kwargs})
def maybe_promise(value): def maybe_evaluate(value):
"""Evaluates if the value is a promise.""" """Evaluates if the value is a :class:`lazy` instance."""
if isinstance(value, promise): if isinstance(value, lazy):
return value.evaluate() return value.evaluate()
return value return value
# Compat names (before kombu 3.0)
promise = lazy
maybe_promise = maybe_evaluate