Changeset 904
- Timestamp:
- 10/17/05 08:54:52 (3 years ago)
- Files:
-
- django/branches/i18n/django/utils/functional.py (modified) (5 diffs)
- django/branches/i18n/django/utils/translation.py (modified) (3 diffs)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
django/branches/i18n/django/utils/functional.py
r884 r904 3 3 return args[0](*(args[1:]+moreargs), **dict(kwargs.items() + morekwargs.items())) 4 4 return _curried 5 6 class NoneSoFar:7 """8 NoneSoFar is a singleton that denotes a missing value. This can be9 used instead of None - because None might be a valid return value.10 """11 pass12 NoneSoFar = NoneSoFar()13 14 def force(value):15 """16 This function forces evaluation of a promise. It recognizes a promise17 by it's magic __force__ function and just applies it and returns18 the result. If the value isn't a promise, it just returns the value.19 """20 21 return getattr(value, '__force__', lambda : value)()22 23 def forced(value):24 """25 This function returns true if a value is either not a promise or26 is already forced. This uses the __forced__ magic method.27 """28 return getattr(value, '__forced__', lambda : True)()29 5 30 6 def lazy(func, *resultclasses): … … 34 10 you need to give result classes or types - at least one is needed 35 11 so that the automatic forcing of the lazy evaluation code is 36 triggered. 12 triggered. Results are not memoized - the function is evaluated 13 on every access. 37 14 """ 38 15 … … 50 27 self.__args = args 51 28 self.__kw = kw 52 self.__result = NoneSoFar53 29 self.__dispatch = {} 54 30 for resultclass in resultclasses: … … 57 33 setattr(self, k, self.__promise__(resultclass, k, v)) 58 34 59 def __force__(self):60 """61 This function forces the evaluation of a promise and62 returns the value itself.63 """64 if self.__result is NoneSoFar:65 self.__result = self.__func(*self.__args, **self.__kw)66 return self.__result67 68 def __forced__(self):69 """70 This returns true if the promise is forced and false if not.71 """72 if self.__result is NoneSoFar: return False73 else: return True74 75 35 def __promise__(self, klass, funcname, func): 76 36 77 """78 This function builds a wrapper around some magic method and79 registers that magic method for the given type and methodname.80 """37 """ 38 This function builds a wrapper around some magic method and 39 registers that magic method for the given type and methodname. 40 """ 81 41 82 def __wrapper__(*args, **kw):83 """84 This wrapper function automatically forces the evaluation of85 a lazy value if the value isn't already forced. It then applies86 the given magic method of theresult type.87 """88 res = self.__force__()89 return self.__dispatch[type(res)][funcname](res, *args, **kw)42 def __wrapper__(*args, **kw): 43 """ 44 This wrapper function automatically triggers the evaluation of 45 a lazy value. It then applies the given magic method of the 46 result type. 47 """ 48 res = self.__func(*self.__args, **self.__kw) 49 return self.__dispatch[type(res)][funcname](res, *args, **kw) 90 50 91 if not self.__dispatch.has_key(klass): self.__dispatch[klass] = {} 92 self.__dispatch[klass][funcname] = func 93 return __wrapper__ 51 if not self.__dispatch.has_key(klass): 52 self.__dispatch[klass] = {} 53 self.__dispatch[klass][funcname] = func 54 return __wrapper__ 94 55 95 56 def __wrapper__(*args, **kw): … … 102 63 return __wrapper__ 103 64 104 if __name__ == '__main__':105 def anton(a,b):106 return a+b107 65 108 anton = lazy(anton, int, str)109 110 print type(anton(5,6))111 print anton(5,6)112 print anton('anton','berta')113 print type(force(anton(5,6)))114 print forced(1)115 print forced(anton(5,6))116 print forced(force(anton(5,6)))117 django/branches/i18n/django/utils/translation.py
r884 r904 248 248 lang_code = request.GET.get('django_language', None) or request.POST.get('django_language', None) 249 249 if lang_code is not None: 250 if lang_code == 'en' or lang_code.startswith('en_'): 251 return lang_code 250 252 lang = gettext_module.find('django', globalpath, [lang_code]) 251 253 if lang is not None: … … 259 261 lang_code = request.session.get('django_language', None) 260 262 if lang_code is not None: 263 if lang_code == 'en' or lang_code.startswith('en_'): 264 return lang_code 261 265 lang = gettext_module.find('django', globalpath, [lang_code]) 262 266 if lang is not None: … … 265 269 lang_code = request.COOKIES.get('django_language', None) 266 270 if lang_code is not None: 271 if lang_code == 'en' or lang_code.startswith('en_'): 272 return lang_code 267 273 lang = gettext_module.find('django', globalpath, [lang_code]) 268 274 if lang is not None:
