Removed a few gratuitous lambdas.

This commit is contained in:
Aymeric Augustin 2013-12-26 13:46:15 +01:00
parent 4e7aa573ec
commit 8f04f53dd8
7 changed files with 37 additions and 20 deletions

View File

@ -46,9 +46,12 @@ from django.views.decorators.csrf import csrf_protect
IS_POPUP_VAR = '_popup' IS_POPUP_VAR = '_popup'
TO_FIELD_VAR = '_to_field' TO_FIELD_VAR = '_to_field'
HORIZONTAL, VERTICAL = 1, 2 HORIZONTAL, VERTICAL = 1, 2
# returns the <ul> class for a given radio_admin field
get_ul_class = lambda x: 'radiolist%s' % (' inline' if x == HORIZONTAL else '')
def get_ul_class(radio_style):
return 'radiolist' if radio_style == VERTICAL else 'radiolist inline'
class IncorrectLookupParameters(Exception): class IncorrectLookupParameters(Exception):

View File

@ -333,7 +333,7 @@ def date_hierarchy(cl):
month_lookup = cl.params.get(month_field) month_lookup = cl.params.get(month_field)
day_lookup = cl.params.get(day_field) day_lookup = cl.params.get(day_field)
link = lambda d: cl.get_query_string(d, [field_generic]) link = lambda filters: cl.get_query_string(filters, [field_generic])
if not (year_lookup or month_lookup or day_lookup): if not (year_lookup or month_lookup or day_lookup):
# select appropriate start level # select appropriate start level

View File

@ -21,7 +21,11 @@ from django.contrib.sites.models import get_current_site
UNMASKED_DIGITS_TO_SHOW = 6 UNMASKED_DIGITS_TO_SHOW = 6
mask_password = lambda p: "%s%s" % (p[:UNMASKED_DIGITS_TO_SHOW], "*" * max(len(p) - UNMASKED_DIGITS_TO_SHOW, 0))
def mask_password(password):
shown = password[:UNMASKED_DIGITS_TO_SHOW]
masked = "*" * max(len(password) - UNMASKED_DIGITS_TO_SHOW, 0)
return shown + masked
class ReadOnlyPasswordHashWidget(forms.Widget): class ReadOnlyPasswordHashWidget(forms.Widget):

View File

@ -1,8 +1,12 @@
from django.conf import settings from django.conf import settings
from django.utils.module_loading import import_by_path as get_storage from django.utils.module_loading import import_by_path
# Callable with the same interface as the storage classes i.e. accepts a def default_storage(request):
# 'request' object. It is wrapped in a lambda to stop 'settings' being used at """
# the module level Callable with the same interface as the storage classes.
default_storage = lambda request: get_storage(settings.MESSAGE_STORAGE)(request)
This isn't just default_storage = import_by_path(settings.MESSAGE_STORAGE)
to avoid accessing the settings at the module level.
"""
return import_by_path(settings.MESSAGE_STORAGE)(request)

View File

@ -1,8 +1,7 @@
from __future__ import unicode_literals from __future__ import unicode_literals
from collections import OrderedDict
import re
from bisect import bisect from bisect import bisect
from collections import OrderedDict
import warnings import warnings
from django.apps import apps from django.apps import apps
@ -13,10 +12,9 @@ from django.db.models.fields.proxy import OrderWrt
from django.utils import six from django.utils import six
from django.utils.functional import cached_property from django.utils.functional import cached_property
from django.utils.encoding import force_text, smart_text, python_2_unicode_compatible from django.utils.encoding import force_text, smart_text, python_2_unicode_compatible
from django.utils.text import camel_case_to_spaces
from django.utils.translation import activate, deactivate_all, get_language, string_concat from django.utils.translation import activate, deactivate_all, get_language, string_concat
# Calculate the verbose_name by converting from InitialCaps to "lowercase with spaces".
get_verbose_name = lambda class_name: re.sub('(((?<=[a-z])[A-Z])|([A-Z](?![A-Z]|$)))', ' \\1', class_name).lower().strip()
DEFAULT_NAMES = ('verbose_name', 'verbose_name_plural', 'db_table', 'ordering', DEFAULT_NAMES = ('verbose_name', 'verbose_name_plural', 'db_table', 'ordering',
'unique_together', 'permissions', 'get_latest_by', 'unique_together', 'permissions', 'get_latest_by',
@ -109,7 +107,7 @@ class Options(object):
# First, construct the default values for these options. # First, construct the default values for these options.
self.object_name = cls.__name__ self.object_name = cls.__name__
self.model_name = self.object_name.lower() self.model_name = self.object_name.lower()
self.verbose_name = get_verbose_name(self.object_name) self.verbose_name = camel_case_to_spaces(self.object_name)
# Store the original user-defined values for each option, # Store the original user-defined values for each option,
# for use when serializing the model definition # for use when serializing the model definition

View File

@ -17,6 +17,7 @@ if six.PY2:
# people rely on it being here. # people rely on it being here.
from django.utils.encoding import force_unicode # NOQA from django.utils.encoding import force_unicode # NOQA
# Capitalizes the first letter of a string. # Capitalizes the first letter of a string.
capfirst = lambda x: x and force_text(x)[0].upper() + force_text(x)[1:] capfirst = lambda x: x and force_text(x)[0].upper() + force_text(x)[1:]
capfirst = allow_lazy(capfirst, six.text_type) capfirst = allow_lazy(capfirst, six.text_type)
@ -25,6 +26,7 @@ capfirst = allow_lazy(capfirst, six.text_type)
re_words = re.compile(r'<.*?>|((?:\w[-\w]*|&.*?;)+)', re.U | re.S) re_words = re.compile(r'<.*?>|((?:\w[-\w]*|&.*?;)+)', re.U | re.S)
re_tag = re.compile(r'<(/)?([^ ]+?)(?:(\s*/)| .*?)?>', re.S) re_tag = re.compile(r'<(/)?([^ ]+?)(?:(\s*/)| .*?)?>', re.S)
re_newlines = re.compile(r'\r\n|\r') # Used in normalize_newlines re_newlines = re.compile(r'\r\n|\r') # Used in normalize_newlines
re_camel_case = re.compile(r'(((?<=[a-z])[A-Z])|([A-Z](?![A-Z]|$)))')
def wrap(text, width): def wrap(text, width):
@ -420,3 +422,11 @@ def slugify(value):
value = re.sub('[^\w\s-]', '', value).strip().lower() value = re.sub('[^\w\s-]', '', value).strip().lower()
return mark_safe(re.sub('[-\s]+', '-', value)) return mark_safe(re.sub('[-\s]+', '-', value))
slugify = allow_lazy(slugify, six.text_type) slugify = allow_lazy(slugify, six.text_type)
def camel_case_to_spaces(value):
"""
Splits CamelCase and converts to lower case. Also strips leading and
trailing whitespace.
"""
return re_camel_case.sub(r' \1', value).strip().lower()

View File

@ -154,12 +154,10 @@ def setup(verbosity, test_labels):
if not test_labels: if not test_labels:
module_found_in_labels = True module_found_in_labels = True
else: else:
match = lambda label: ( module_found_in_labels = any(
module_label == label or # exact match # exact match or ancestor match
module_label.startswith(label + '.') # ancestor match module_label == label or module_label.startswith(label + '.')
) for label in test_labels_set)
module_found_in_labels = any(match(l) for l in test_labels_set)
if module_found_in_labels: if module_found_in_labels:
if verbosity >= 2: if verbosity >= 2: