def test_can_disable_proxy_cache(self): self.counter = 0 def add_one(): self.counter += 1 return self.counter proxy = support.LazyProxy(add_one, enable_cache=False) self.assertEqual(1, proxy.value) self.assertEqual(2, proxy.value)
def test_proxy_caches_result_of_function_call(self): self.counter = 0 def add_one(): self.counter += 1 return self.counter proxy = support.LazyProxy(add_one) self.assertEqual(1, proxy.value) self.assertEqual(1, proxy.value)
def test_handle_attribute_error(self): def raise_attribute_error(): raise AttributeError('message') proxy = support.LazyProxy(raise_attribute_error) with pytest.raises(AttributeError) as exception: proxy.value self.assertEqual('message', str(exception.value))
def test_lazy_proxy(): def greeting(name='world'): return u'Hello, %s!' % name lazy_greeting = support.LazyProxy(greeting, name='Joe') assert str(lazy_greeting) == u"Hello, Joe!" assert u' ' + lazy_greeting == u' Hello, Joe!' assert u'(%s)' % lazy_greeting == u'(Hello, Joe!)' greetings = [ support.LazyProxy(greeting, 'world'), support.LazyProxy(greeting, 'Joe'), support.LazyProxy(greeting, 'universe'), ] greetings.sort() assert [str(g) for g in greetings] == [ u"Hello, Joe!", u"Hello, universe!", u"Hello, world!", ]
def lazy_gettext(string): """ Args: string: Returns: """ return support.LazyProxy(lambda: active_translation.ugettext(string))
def lazy_gettext(string, **variables): """A lazy version of :func:`gettext`. :param string: The string to be translated. :param variables: Variables to format the returned string. :returns: A ``babel.support.LazyProxy`` object that when accessed translates the string. """ return support.LazyProxy(gettext, string, **variables)
def test_can_deepcopy_proxy(self): from copy import deepcopy numbers = [1, 2] def first(xs): return xs[0] proxy = support.LazyProxy(first, numbers) proxy_deepcopy = deepcopy(proxy) numbers.pop(0) self.assertEqual(2, proxy.value) self.assertEqual(1, proxy_deepcopy.value)
def lazy_ngettext(singular, plural, n, **variables): """A lazy version of :func:`ngettext`. :param singular: The singular for of the string to be translated. :param plural: The plural for of the string to be translated. :param n: An integer indicating if this is a singular or plural. If greater than 1, it is a plural. :param variables: Variables to format the returned string. :returns: A ``babel.support.LazyProxy`` object that when accessed translates the string. """ return support.LazyProxy(ngettext, singular, plural, n, **variables)