diff options
Diffstat (limited to 'django/utils')
| -rw-r--r-- | django/utils/functional.py | 15 | ||||
| -rw-r--r-- | django/utils/html.py | 16 | ||||
| -rw-r--r-- | django/utils/itercompat.py | 8 |
3 files changed, 31 insertions, 8 deletions
diff --git a/django/utils/functional.py b/django/utils/functional.py index 0c31c1f375..a57546ad2d 100644 --- a/django/utils/functional.py +++ b/django/utils/functional.py @@ -3,6 +3,21 @@ def curry(_curried_func, *args, **kwargs): return _curried_func(*(args+moreargs), **dict(kwargs, **morekwargs)) return _curried +def memoize(func, cache): + """ + Wrap a function so that results for any argument tuple are stored in + 'cache'. Note that the args to the function must be usable as dictionary + keys. + """ + def wrapper(*args): + if args in cache: + return cache[args] + + result = func(*args) + cache[args] = result + return result + return wrapper + class Promise: """ This is just a base class for the proxy class created in diff --git a/django/utils/html.py b/django/utils/html.py index 607362817b..e1860627ce 100644 --- a/django/utils/html.py +++ b/django/utils/html.py @@ -53,16 +53,18 @@ def fix_ampersands(value): def urlize(text, trim_url_limit=None, nofollow=False): """ - Converts any URLs in text into clickable links. Works on http://, https:// and - www. links. Links can have trailing punctuation (periods, commas, close-parens) - and leading punctuation (opening parens) and it'll still do the right thing. + Converts any URLs in text into clickable links. Works on http://, https:// + and www. links. Links can have trailing punctuation (periods, commas, + close-parens) and leading punctuation (opening parens) and it'll still do + the right thing. - If trim_url_limit is not None, the URLs in link text will be limited to - trim_url_limit characters. + If trim_url_limit is not None, the URLs in link text longer than this limit + will truncated to trim_url_limit-3 characters and appended with an elipsis. - If nofollow is True, the URLs in link text will get a rel="nofollow" attribute. + If nofollow is True, the URLs in link text will get a rel="nofollow" + attribute. """ - trim_url = lambda x, limit=trim_url_limit: limit is not None and (x[:limit] + (len(x) >=limit and '...' or '')) or x + trim_url = lambda x, limit=trim_url_limit: limit is not None and (len(x) > limit and ('%s...' % x[:max(0, limit - 3)])) or x words = word_split_re.split(text) nofollow_attr = nofollow and ' rel="nofollow"' or '' for i, word in enumerate(words): diff --git a/django/utils/itercompat.py b/django/utils/itercompat.py index 48a2d99d3a..0de1b6cbe2 100644 --- a/django/utils/itercompat.py +++ b/django/utils/itercompat.py @@ -34,7 +34,7 @@ def groupby(iterable, keyfunc=None): keyfunc = lambda x:x iterable = iter(iterable) l = [iterable.next()] - lastkey = keyfunc(l) + lastkey = keyfunc(l[0]) for item in iterable: key = keyfunc(item) if key != lastkey: @@ -45,6 +45,12 @@ def groupby(iterable, keyfunc=None): l.append(item) yield lastkey, l +# Not really in itertools, since it's a builtin in Python 2.4 and later, but it +# does operate as an iterator. +def reversed(data): + for index in xrange(len(data)-1, -1, -1): + yield data[index] + if hasattr(itertools, 'tee'): tee = itertools.tee else: |
