1
0
mirror of https://github.com/django/django.git synced 2025-07-04 09:49:12 +00:00

i18n: fixed handling of gettext_lazy - now it doesn't memoize function values any more

git-svn-id: http://code.djangoproject.com/svn/django/branches/i18n@904 bcc190cf-cafb-0310-a4f2-bffc1f526a37
This commit is contained in:
Georg Bauer 2005-10-17 13:54:52 +00:00
parent 49739a8f84
commit 3c68633d57
2 changed files with 24 additions and 70 deletions

View File

@ -3,37 +3,14 @@ def curry(*args, **kwargs):
return args[0](*(args[1:]+moreargs), **dict(kwargs.items() + morekwargs.items())) return args[0](*(args[1:]+moreargs), **dict(kwargs.items() + morekwargs.items()))
return _curried return _curried
class NoneSoFar:
"""
NoneSoFar is a singleton that denotes a missing value. This can be
used instead of None - because None might be a valid return value.
"""
pass
NoneSoFar = NoneSoFar()
def force(value):
"""
This function forces evaluation of a promise. It recognizes a promise
by it's magic __force__ function and just applies it and returns
the result. If the value isn't a promise, it just returns the value.
"""
return getattr(value, '__force__', lambda : value)()
def forced(value):
"""
This function returns true if a value is either not a promise or
is already forced. This uses the __forced__ magic method.
"""
return getattr(value, '__forced__', lambda : True)()
def lazy(func, *resultclasses): def lazy(func, *resultclasses):
""" """
lazy turns any callable passed in into a lazy evaluated callable. lazy turns any callable passed in into a lazy evaluated callable.
you need to give result classes or types - at least one is needed you need to give result classes or types - at least one is needed
so that the automatic forcing of the lazy evaluation code is so that the automatic forcing of the lazy evaluation code is
triggered. triggered. Results are not memoized - the function is evaluated
on every access.
""" """
class __proxy__: class __proxy__:
@ -49,48 +26,32 @@ def lazy(func, *resultclasses):
self.__func = func self.__func = func
self.__args = args self.__args = args
self.__kw = kw self.__kw = kw
self.__result = NoneSoFar
self.__dispatch = {} self.__dispatch = {}
for resultclass in resultclasses: for resultclass in resultclasses:
self.__dispatch[resultclass] = {} self.__dispatch[resultclass] = {}
for (k, v) in resultclass.__dict__.items(): for (k, v) in resultclass.__dict__.items():
setattr(self, k, self.__promise__(resultclass, k, v)) setattr(self, k, self.__promise__(resultclass, k, v))
def __force__(self):
"""
This function forces the evaluation of a promise and
returns the value itself.
"""
if self.__result is NoneSoFar:
self.__result = self.__func(*self.__args, **self.__kw)
return self.__result
def __forced__(self):
"""
This returns true if the promise is forced and false if not.
"""
if self.__result is NoneSoFar: return False
else: return True
def __promise__(self, klass, funcname, func): def __promise__(self, klass, funcname, func):
""" """
This function builds a wrapper around some magic method and This function builds a wrapper around some magic method and
registers that magic method for the given type and methodname. registers that magic method for the given type and methodname.
""" """
def __wrapper__(*args, **kw): def __wrapper__(*args, **kw):
""" """
This wrapper function automatically forces the evaluation of This wrapper function automatically triggers the evaluation of
a lazy value if the value isn't already forced. It then applies a lazy value. It then applies the given magic method of the
the given magic method of the result type. result type.
""" """
res = self.__force__() res = self.__func(*self.__args, **self.__kw)
return self.__dispatch[type(res)][funcname](res, *args, **kw) return self.__dispatch[type(res)][funcname](res, *args, **kw)
if not self.__dispatch.has_key(klass): self.__dispatch[klass] = {} if not self.__dispatch.has_key(klass):
self.__dispatch[klass][funcname] = func self.__dispatch[klass] = {}
return __wrapper__ self.__dispatch[klass][funcname] = func
return __wrapper__
def __wrapper__(*args, **kw): def __wrapper__(*args, **kw):
""" """
@ -101,17 +62,4 @@ def lazy(func, *resultclasses):
return __wrapper__ return __wrapper__
if __name__ == '__main__':
def anton(a,b):
return a+b
anton = lazy(anton, int, str)
print type(anton(5,6))
print anton(5,6)
print anton('anton','berta')
print type(force(anton(5,6)))
print forced(1)
print forced(anton(5,6))
print forced(force(anton(5,6)))

View File

@ -247,6 +247,8 @@ def get_language_from_request(request):
if request.GET or request.POST: if request.GET or request.POST:
lang_code = request.GET.get('django_language', None) or request.POST.get('django_language', None) lang_code = request.GET.get('django_language', None) or request.POST.get('django_language', None)
if lang_code is not None: if lang_code is not None:
if lang_code == 'en' or lang_code.startswith('en_'):
return lang_code
lang = gettext_module.find('django', globalpath, [lang_code]) lang = gettext_module.find('django', globalpath, [lang_code])
if lang is not None: if lang is not None:
if hasattr(request, 'session'): if hasattr(request, 'session'):
@ -258,12 +260,16 @@ def get_language_from_request(request):
if hasattr(request, 'session'): if hasattr(request, 'session'):
lang_code = request.session.get('django_language', None) lang_code = request.session.get('django_language', None)
if lang_code is not None: if lang_code is not None:
if lang_code == 'en' or lang_code.startswith('en_'):
return lang_code
lang = gettext_module.find('django', globalpath, [lang_code]) lang = gettext_module.find('django', globalpath, [lang_code])
if lang is not None: if lang is not None:
return lang_code return lang_code
lang_code = request.COOKIES.get('django_language', None) lang_code = request.COOKIES.get('django_language', None)
if lang_code is not None: if lang_code is not None:
if lang_code == 'en' or lang_code.startswith('en_'):
return lang_code
lang = gettext_module.find('django', globalpath, [lang_code]) lang = gettext_module.find('django', globalpath, [lang_code])
if lang is not None: if lang is not None:
return lang_code return lang_code