Ticket #14290: ticket14290_agressive.diff

File ticket14290_agressive.diff, 5.1 KB (added by Anssi Kääriäinen, 14 years ago)

More aggressive optimizations

  • django/utils/formats.py

    diff --git a/django/utils/formats.py b/django/utils/formats.py
    index 372998f..1c5f381 100644
    a b from django.utils.importlib import import_module  
    77from django.utils.encoding import smart_str
    88from django.utils import dateformat, numberformat, datetime_safe
    99
    10 def get_format_modules(reverse=False):
     10# format_cache is a mapping from format_type to a 1-tuple. This allows
     11# us to separate 3 distinct cases:
     12#      (None,)            the format_type was checked previously, but
     13#                         no value was found.
     14#      (<format value>, ) the previously fetched value for format_type
     15#                         is format_value
     16#      if a format_type key is not in _format_cache, then format_type
     17#      hasn't yet been fetched.
     18_format_cache = {}
     19
     20def get_format_modules(reverse=False, lang=None):
    1121    """
    1222    Returns an iterator over the format modules found in the project and Django
    1323    """
     24    if lang is None:
     25        lang = get_language()
    1426    modules = []
    15     if not check_for_language(get_language()) or not settings.USE_L10N:
     27    if not check_for_language(lang) or not settings.USE_L10N:
    1628        return modules
    17     locale = to_locale(get_language())
     29    locale = to_locale(lang)
    1830    if settings.FORMAT_MODULE_PATH:
    1931        format_locations = [settings.FORMAT_MODULE_PATH + '.%s']
    2032    else:
    def get_format_modules(reverse=False):  
    3446        modules.reverse()
    3547    return modules
    3648
    37 def get_format(format_type):
     49_use_l10n = None
     50def get_format(format_type, lang=None):
    3851    """
    3952    For a specific format type, returns the format for the current
    4053    language (locale), defaults to the format in the settings.
    4154    format_type is the name of the format, e.g. 'DATE_FORMAT'
    4255    """
    43     format_type = smart_str(format_type)
    44     if settings.USE_L10N:
    45         for module in get_format_modules():
    46             try:
    47                 return getattr(module, format_type)
    48             except AttributeError:
     56    # format_type = smart_str(format_type)
     57    global _format_cache
     58    global _use_l10n
     59    if _use_l10n is None:
     60        _use_l10n = settings.USE_L10N
     61       
     62    if _use_l10n:
     63        if lang is None:
     64            lang = get_language()
     65        cached_value = _format_cache.get((format_type, lang), None)
     66        if cached_value:
     67            if cached_value[0]:
     68                return cached_value[0]
     69            else:
     70                # format_type was previously checked, but didn't have a value
    4971                pass
    50     return getattr(settings, format_type)
     72        else:
     73            for module in get_format_modules(lang=lang):
     74                try:
     75                    val = getattr(module, format_type)
     76                    _format_cache[(format_type, lang)] = (val,)
     77                    return val
     78                except AttributeError:
     79                    pass
     80            _format_cache[(format_type, lang)] = (None,)
     81    cached_value = _format_cache.get(format_type)
     82    if not cached_value:
     83        cached_value = (getattr(settings, format_type),)
     84        _format_cache[format_type] = cached_value
     85    return cached_value[0]
    5186
    5287def date_format(value, format=None):
    5388    """
    def number_format(value, decimal_pos=None):  
    66101    """
    67102    Formats a numeric value using localization settings
    68103    """
     104    lang = get_language()
    69105    return numberformat.format(
    70106        value,
    71         get_format('DECIMAL_SEPARATOR'),
     107        get_format('DECIMAL_SEPARATOR', lang=lang),
    72108        decimal_pos,
    73         get_format('NUMBER_GROUPING'),
    74         get_format('THOUSAND_SEPARATOR'),
     109        get_format('NUMBER_GROUPING', lang=lang),
     110        get_format('THOUSAND_SEPARATOR', lang=lang),
    75111    )
    76112
    77113def localize(value):
  • django/utils/translation/__init__.py

    diff --git a/django/utils/translation/__init__.py b/django/utils/translation/__init__.py
    index 6d81726..7d8eec3 100644
    a b __all__ = ['gettext', 'gettext_noop', 'gettext_lazy', 'ngettext',  
    2020# replace the functions with their real counterparts (once we do access the
    2121# settings).
    2222
     23_trans = None
     24
    2325def delayed_loader(real_name, *args, **kwargs):
    2426    """
    2527    Call the real, underlying function.  We have a level of indirection here so
    2628    that modules can use the translation bits without actually requiring
    2729    Django's settings bits to be configured before import.
    2830    """
    29     from django.conf import settings
    30     if settings.USE_I18N:
    31         from django.utils.translation import trans_real as trans
    32     else:
    33         from django.utils.translation import trans_null as trans
    34 
     31    global _trans
     32    if not _trans:
     33        from django.conf import settings
     34        if settings.USE_I18N:
     35            from django.utils.translation import trans_real as trans
     36        else:
     37            from django.utils.translation import trans_null as trans
     38        _trans = trans
    3539    # Make the originally requested function call on the way out the door.
    36     return getattr(trans, real_name)(*args, **kwargs)
     40    return getattr(_trans, real_name)(*args, **kwargs)
    3741
    3842g = globals()
    3943for name in __all__:
Back to Top