diff options
Diffstat (limited to 'django/utils')
-rw-r--r-- | django/utils/datastructures.py | 25 | ||||
-rw-r--r-- | django/utils/formats.py | 20 | ||||
-rw-r--r-- | django/utils/hashcompat.py | 8 | ||||
-rw-r--r-- | django/utils/text.py | 12 | ||||
-rw-r--r-- | django/utils/translation/trans_real.py | 2 |
5 files changed, 48 insertions, 19 deletions
diff --git a/django/utils/datastructures.py b/django/utils/datastructures.py index 3cbbe27b91..30ce28d38d 100644 --- a/django/utils/datastructures.py +++ b/django/utils/datastructures.py @@ -77,6 +77,27 @@ class MergeDict(object): """Returns a copy of this object.""" return self.__copy__() + def __str__(self): + ''' + Returns something like + + "{'key1': 'val1', 'key2': 'val2', 'key3': 'val3'}" + + instead of the generic "<object meta-data>" inherited from object. + ''' + return str(dict(self.items())) + + def __repr__(self): + ''' + Returns something like + + MergeDict({'key1': 'val1', 'key2': 'val2'}, {'key3': 'val3'}) + + instead of generic "<object meta-data>" inherited from object. + ''' + dictreprs = ', '.join(repr(d) for d in self.dicts) + return '%s(%s)' % (self.__class__.__name__, dictreprs) + class SortedDict(dict): """ A dictionary that keeps its keys in the order in which they're inserted. @@ -99,9 +120,11 @@ class SortedDict(dict): self.keyOrder = data.keys() else: self.keyOrder = [] + seen = set() for key, value in data: - if key not in self.keyOrder: + if key not in seen: self.keyOrder.append(key) + seen.add(key) def __deepcopy__(self, memo): return self.__class__([(key, deepcopy(value, memo)) diff --git a/django/utils/formats.py b/django/utils/formats.py index 31027abd23..372998f221 100644 --- a/django/utils/formats.py +++ b/django/utils/formats.py @@ -79,16 +79,16 @@ def localize(value): Checks if value is a localizable type (date, number...) and returns it formatted as a string using current locale format """ - if settings.USE_L10N: - if isinstance(value, (decimal.Decimal, float, int)): - return number_format(value) - elif isinstance(value, datetime.datetime): - return date_format(value, 'DATETIME_FORMAT') - elif isinstance(value, datetime.date): - return date_format(value) - elif isinstance(value, datetime.time): - return time_format(value, 'TIME_FORMAT') - return value + if isinstance(value, (decimal.Decimal, float, int)): + return number_format(value) + elif isinstance(value, datetime.datetime): + return date_format(value, 'DATETIME_FORMAT') + elif isinstance(value, datetime.date): + return date_format(value) + elif isinstance(value, datetime.time): + return time_format(value, 'TIME_FORMAT') + else: + return value def localize_input(value, default=None): """ diff --git a/django/utils/hashcompat.py b/django/utils/hashcompat.py index b1e6021890..4d9b76f3a6 100644 --- a/django/utils/hashcompat.py +++ b/django/utils/hashcompat.py @@ -1,17 +1,17 @@ """ The md5 and sha modules are deprecated since Python 2.5, replaced by the hashlib module containing both hash algorithms. Here, we provide a common -interface to the md5 and sha constructors, preferring the hashlib module when -available. +interface to the md5 and sha constructors, depending on system version. """ -try: +import sys +if sys.version_info >= (2, 5): import hashlib md5_constructor = hashlib.md5 md5_hmac = md5_constructor sha_constructor = hashlib.sha1 sha_hmac = sha_constructor -except ImportError: +else: import md5 md5_constructor = md5.new md5_hmac = md5 diff --git a/django/utils/text.py b/django/utils/text.py index 5d633b7afe..b05460486d 100644 --- a/django/utils/text.py +++ b/django/utils/text.py @@ -39,7 +39,10 @@ wrap = allow_lazy(wrap, unicode) def truncate_words(s, num, end_text='...'): """Truncates a string after a certain number of words. Takes an optional argument of what should be used to notify that the string has been - truncated, defaults to ellipsis (...)""" + truncated, defaulting to ellipsis (...) + + Newlines in the string will be stripped. + """ s = force_unicode(s) length = int(num) words = s.split() @@ -51,10 +54,13 @@ def truncate_words(s, num, end_text='...'): truncate_words = allow_lazy(truncate_words, unicode) def truncate_html_words(s, num, end_text='...'): - """Truncates html to a certain number of words (not counting tags and + """Truncates HTML to a certain number of words (not counting tags and comments). Closes opened tags if they were correctly closed in the given html. Takes an optional argument of what should be used to notify that the - string has been truncated, defaults to ellipsis (...).""" + string has been truncated, defaulting to ellipsis (...). + + Newlines in the HTML are preserved. + """ s = force_unicode(s) length = int(num) if length <= 0: diff --git a/django/utils/translation/trans_real.py b/django/utils/translation/trans_real.py index 017b99a8ca..f732cd931e 100644 --- a/django/utils/translation/trans_real.py +++ b/django/utils/translation/trans_real.py @@ -502,7 +502,7 @@ def parse_accept_lang_header(lang_string): return [] priority = priority and float(priority) or 1.0 result.append((lang, priority)) - result.sort(lambda x, y: -cmp(x[1], y[1])) + result.sort(key=lambda k: k[1], reverse=True) return result # get_date_formats and get_partial_date_formats aren't used anymore by Django |