def test_deprecated_memoize(self): """ Ensure the correct warning is raised when memoize is used. """ with warnings.catch_warnings(record=True) as recorded: warnings.simplefilter('always') memoize(lambda x: x, {}, 1) msg = str(recorded.pop().message) self.assertEqual(msg, 'memoize wrapper is deprecated and will be removed in Django ' '1.9. Use django.utils.lru_cache instead.')
def test_deprecated_memoize(self): """ Ensure the correct warning is raised when memoize is used. """ with warnings.catch_warnings(record=True) as recorded: warnings.simplefilter('always') memoize(lambda x: x, {}, 1) msg = str(recorded.pop().message) self.assertEqual( msg, 'memoize wrapper is deprecated and will be removed in Django ' '1.9. Use django.utils.lru_cache instead.')
def __init__(self, context, render_full_page): self._prepare_assets_cache = {} setattr(self, '_prepare_assets', memoize( self._prepare_assets, self._prepare_assets_cache, 2)) self.cache_root = os.path.join( settings.SKYLARK_CACHE_ROOT, self.cache_prefix) self.cache_url = urljoin(settings.SKYLARK_CACHE_URL, '%s/' % self.cache_prefix) self.context = context self.render_full_page = render_full_page """ As we process the page instructions, we gather the output we need to convert this into an html page inside this dictionary """ self.prepared_instructions = { 'meta': [], 'js': [], 'css': [], 'chirp': [], } self.prepared_instructions['render_full_page'] = self.render_full_page self.prepared_instructions['cache_prefix'] = '%s/' % self.cache_prefix if not os.path.exists(self.cache_root): os.makedirs(self.cache_root)
def auth(request): """ Returns context variables required by apps that use Django's authentication system. If there is no 'user' attribute in the request, uses AnonymousUser (from django.contrib.auth). """ # If we access request.user, request.session is accessed, which results in # 'Vary: Cookie' being sent in every request that uses this context # processor, which can easily be every request on a site if # TEMPLATE_CONTEXT_PROCESSORS has this context processor added. This kills # the ability to cache. So, we carefully ensure these attributes are lazy. # We don't use django.utils.functional.lazy() for User, because that # requires knowing the class of the object we want to proxy, which could # break with custom auth backends. LazyObject is a less complete but more # flexible solution that is a good enough wrapper for 'User'. def get_user(): if hasattr(request, 'user'): return request.user else: from django.contrib.auth.models import AnonymousUser return AnonymousUser() return { 'user': SimpleLazyObject(get_user), 'messages': lazy(memoize(lambda: get_user().get_and_delete_messages(), {}, 0), list)(), 'perms': lazy(lambda: PermWrapper(get_user()), PermWrapper)(), }
def get_resolver(self, name): def _wrapped(name): urls = self.get_urls(name) urlconf_module, app_name, namespace = include(urls) resolver = RegexURLResolver('', urlconf_module, {}, app_name=app_name, namespace=namespace) return resolver _wrapped = memoize(_wrapped, self._cache, 1) return _wrapped(name)
def crops_in_circle(request): lat = float(request.GET.get("lat")) lon = float(request.GET.get("lon")) radius = float(request.GET.get("radius")) location = request.GET.get("location", "US TOTAL") response_data = memoize(crops_in_circle_helper, {}, 4)(lat, lon, radius, location) response_json = json.dumps(response_data) return HttpResponse(response_json, content_type="application/json")
def get_metadata(self): def do_get_metadata(): book = self.open_xsl() sheet = book.sheet_by_index(0) attr_map = [ "element_name", "indicator_group", "display_name", "short_definition", "long_definition", "hub_programming", "query_level", "", "file_name", "key_type", "min_threshold", "min", "max", "", "", "", "universe", "limitations", "", "range_params", "routine_use", "datasources", "subagency", "", "", "", "time_key", "time_parameters_available", "time_type", "update_frequency", "purpose", "", "", "", "", "raw_tags", "", "data_type", "unit", "", "", "", ] metadata = [] for row_num in range(1, sheet.nrows): row = map(safe_strip, sheet.row_values(row_num)) metadata_dict = dict(zip(attr_map, row)) metadata.append(metadata_dict) return metadata do_get_metadata = memoize(do_get_metadata, get_metadata_cache, 0) return do_get_metadata()
def get_files(self): def do_get_files(): files = {} for path, subpaths, subfiles in os.walk(self.directory): for filename in subfiles: files[filename] = os.path.join(path, filename) return files do_get_files = memoize(do_get_files, get_files_cache, 0) return do_get_files()
def get_metadata(self): def do_get_metadata(): book = self.open_xsl() sheet = book.sheet_by_index(0) attr_map = [ 'element_name', 'indicator_group', 'display_name', 'short_definition', 'long_definition', 'hub_programming', 'query_level', '', 'file_name', 'key_type', 'min_threshold', 'min', 'max', '', '', '', 'universe', 'limitations', '', 'range_params', 'routine_use', 'datasources', 'subagency', '', '', '', 'time_key', 'time_parameters_available', 'time_type', 'update_frequency', 'purpose', '', '', '', '', 'raw_tags', '', 'data_type', 'unit', '', '', '' ] metadata = [] for row_num in range(1, sheet.nrows): row = map(safe_strip, sheet.row_values(row_num)) metadata_dict = dict(zip(attr_map, row)) metadata.append(metadata_dict) return metadata do_get_metadata = memoize(do_get_metadata, get_metadata_cache, 0) return do_get_metadata()
def get_key_field(self, pregen_part): def do_get_key_field(pregen_part): print 'in do_get_key_field for %s' % pregen_part if pregen_part.key_column: return pregen_part.key_column file_path = self.find_file(pregen_part) reader = csv.reader(open(file_path, 'rU')) header = reader.next() return header[0] do_get_key_field = memoize(do_get_key_field, key_field_cache, 1) return do_get_key_field(pregen_part)
def render_markdown_path(markdown_file_path): # type: (str) -> str """ Given a path to a markdown file, return the rendered html """ import markdown def path_to_html(path): markdown_string = open(path).read() return markdown.markdown(markdown_string, safe_mode="escape") html = memoize(path_to_html, memoize_cache, 1)(markdown_file_path) return mark_safe(html)
def render_markdown_path(markdown_file_path): # type: (str) -> str """ Given a path to a markdown file, return the rendered html """ import markdown def path_to_html(path): markdown_string = open(path).read() return markdown.markdown(markdown_string, safe_mode='escape') html = memoize(path_to_html, memoize_cache, 1)(markdown_file_path) return mark_safe(html)
def get_key_field(self, table_name): def do_get_key_field(table_name): print 'in do_get_key_field for %s' % table_name files = self.get_files() for filename, path in files.iteritems(): if filename.startswith(table_name) and filename.endswith('csv'): # assuming the first column is the key field reader = csv.reader(open(path, 'rU')) header = reader.next() return header[0] print "WARNING: Couldn't find a key field for %s" % table_name return '' do_get_key_field = memoize(do_get_key_field, key_field_cache, 1) return do_get_key_field(table_name)
def raw_html(path): def get_content(path): import os.path basedir = os.path.join(settings.PROJECT_PATH, 'templates') #TODO this sucks. f = file(os.path.join(basedir, path), 'r') try: content = f.read() finally: f.close() return mark_safe(content) if settings.MEMOIZE_RAW_HTML: return memoize(get_content, {}, 1)(path) return get_content(path)
def get_messages(request): """ Returns the message storage on the request if it exists, otherwise returns user.message_set.all() as the old auth context processor did. """ if hasattr(request, '_messages'): return request._messages user = get_user(request) if user: return lazy(memoize(user.get_and_delete_messages, {}, 0), list)() else: return None """
def get_messages(request): """ Returns the message storage on the request if it exists, otherwise returns user.message_set.all() as the old auth context processor did. """ if hasattr(request, '_messages'): return request._messages def get_user(): if hasattr(request, 'user'): return request.user else: from django.contrib.auth.models import AnonymousUser return AnonymousUser() return lazy(memoize(get_user().get_and_delete_messages, {}, 0), list)()
def get_key_field(self, table_name): def do_get_key_field(table_name): print 'in do_get_key_field for %s' % table_name files = self.get_files() for filename, path in files.iteritems(): if filename.startswith(table_name) and filename.endswith( 'csv'): # assuming the first column is the key field reader = csv.reader(open(path, 'rU')) header = reader.next() return header[0] print "WARNING: Couldn't find a key field for %s" % table_name return '' do_get_key_field = memoize(do_get_key_field, key_field_cache, 1) return do_get_key_field(table_name)
def patch_check_function(org): """ Patches the check_for_language function so that it always returns true for pseudo localizations. :param org: the original function. :return: the patched function. """ def check_for_language(lang): if lang.startswith("pse"): return True return org(lang) if django.VERSION < (1, 7): from django.utils.functional import memoize check_for_language = memoize(check_for_language, {}, 1) else: from django.utils import lru_cache check_for_language = lru_cache.lru_cache(1000)(check_for_language) return check_for_language
from django.template import Context from django.template.loader import get_template from django.utils.functional import memoize from django import template from crispy_forms.exceptions import CrispyError TEMPLATE_PACK = getattr(settings, 'CRISPY_TEMPLATE_PACK', 'bootstrap') DEBUG = getattr(settings, 'DEBUG', False) def uni_formset_template(template_pack=TEMPLATE_PACK): return get_template('%s/uni_formset.html' % template_pack) uni_formset_template = memoize(uni_formset_template, {}, 1) def uni_form_template(template_pack=TEMPLATE_PACK): return get_template('%s/uni_form.html' % template_pack) uni_form_template = memoize(uni_form_template, {}, 1) register = template.Library() @register.filter(name='crispy') def as_crispy_form(form, template_pack=TEMPLATE_PACK): """ The original and still very useful way to generate a div elegant form/formset::
def decorator(function, cache_dict=None): if cache_dict is None: cache_dict = {} return memoize(function, cache_dict, 1)
if not callable(lookup_view): try: # Bail early for non-ASCII strings (they can't be functions). lookup_view = lookup_view.encode('ascii') mod_name, func_name = get_mod_func(lookup_view) if func_name != '': lookup_view = getattr(__import__(mod_name, {}, {}, ['']), func_name) if not callable(lookup_view): raise AttributeError("'%s.%s' is not a callable." % (mod_name, func_name)) except (ImportError, AttributeError): if not can_fail: raise except UnicodeEncodeError: pass return lookup_view get_callable = memoize(get_callable, _callable_cache, 1) def get_resolver(urlconf): if urlconf is None: from django.conf import settings urlconf = settings.ROOT_URLCONF return RegexURLResolver(r'^/', urlconf) get_resolver = memoize(get_resolver, _resolver_cache, 1) def get_mod_func(callback): # Converts 'django.views.news.stories.story_detail' to # ['django.views.news.stories', 'story_detail'] try: dot = callback.rindex('.') except ValueError: return callback, ''
# No match. return all and [] or None def get_finders(): for finder_path in settings.STATICFILES_FINDERS: yield get_finder(finder_path) def _get_finder(import_path): """ Imports the staticfiles finder class described by import_path, where import_path is the full Python path to the class. """ module, attr = import_path.rsplit('.', 1) try: mod = import_module(module) except ImportError as e: raise ImproperlyConfigured('Error importing module %s: "%s"' % (module, e)) try: Finder = getattr(mod, attr) except AttributeError: raise ImproperlyConfigured('Module "%s" does not define a "%s" ' 'class.' % (module, attr)) if not issubclass(Finder, BaseFinder): raise ImproperlyConfigured('Finder "%s" is not a subclass of "%s"' % (Finder, BaseFinder)) return Finder() get_finder = memoize(_get_finder, _finders, 1)
matches = [] for finder in get_finders(): result = finder.find(path, all=all) if not all and result: return result if not isinstance(result, (list, tuple)): result = [result] matches.extend(result) if matches: return matches # No match. return [] if all else None def get_finders(): for finder_path in settings.STATICFILES_FINDERS: yield get_finder(finder_path) def _get_finder(import_path): """ Imports the staticfiles finder class described by import_path, where import_path is the full Python path to the class. """ Finder = import_by_path(import_path) if not issubclass(Finder, BaseFinder): raise ImproperlyConfigured('Finder "%s" is not a subclass of "%s"' % (Finder, BaseFinder)) return Finder() get_finder = memoize(_get_finder, _finders, 1)
"""Comment flags for Zinnia""" from django.contrib.auth import get_user_model from django.utils.functional import memoize from events.settings import COMMENT_FLAG_USER_ID PINGBACK = 'pingback' TRACKBACK = 'trackback' FLAGGER_USERNAME = '******' user_flagger_ = {} def _get_user_flagger(): """ Return an User instance used by the system when flagging a comment as trackback or pingback. """ User = get_user_model() try: user = User.objects.get(pk=COMMENT_FLAG_USER_ID) except User.DoesNotExist: try: user = User.objects.get(**{User.USERNAME_FIELD: FLAGGER_USERNAME}) except User.DoesNotExist: user = User.objects.create_user(FLAGGER_USERNAME) return user get_user_flagger = memoize(_get_user_flagger, user_flagger_, 0)
# load backends from defined modules BACKENDS, BACKENDS_BY_AUTH = get_backends() def get_backend_from_auth(auth_backend_name): """ Return a valid backend to use for a specified auth_backend, None in other case """ backend_class = BACKENDS_BY_AUTH.get(auth_backend_name, None) if not backend_class: return None return get_backend(backend_class.name) get_backend_from_auth.__cache = {} get_backend = memoize(get_backend_from_auth, get_backend_from_auth.__cache, 1) def get_backend(name): """ Return a valid backend based on its name, None in other case """ backend_class = BACKENDS_BY_AUTH.get(name, None) if not backend_class: return None return backend_class() get_backend.__cache = {} get_backend = memoize(get_backend, get_backend.__cache, 1)
_analyzers_cache = {} def clear_analyzers_cache(): global _analyzers_cache _analyzers_cache.clear() def load_analyzer(analyzer_name): module_name, attr = analyzer_name.rsplit(".", 1) try: module = import_module(module_name) except ImportError, e: raise ImproperlyConfigured('Error importing analyzer %s: "%s"' % (analyzer_name, e)) try: analyzer = getattr(module, attr) except AttributeError, e: raise ImproperlyConfigured('Error importing analyzer %s: "%s"' % (analyzer_name, e)) return analyzer def get_analyzers(): analyzers = [] for analyzer_name in getattr(settings, "LINT_ANALYZERS", ()): analyzers.append(load_analyzer(analyzer_name)) return analyzers get_analyzers = memoize(get_analyzers, _analyzers_cache, 0)
return "" else: raise except SyntaxError: return "" for t in xml.getiterator("tag"): count = utils.safeint(t.find("count").text) if count >= getattr(settings, 'LASTFM_TAG_USAGE_THRESHOLD', 15): tag = slugify(smart_unicode(t.find("name").text)) tags.add(tag[:50]) return tags # Memoize tags to avoid unnecessary API calls. _tag_cache = {} _tags_for_url = memoize(_tags_for_url, _tag_cache, 1) @transaction.commit_on_success def _handle_track(artist_name, artist_mbid, track_name, track_mbid, url, timestamp, tags): t = Track( artist_name = artist_name, track_name = track_name, url = url, track_mbid = track_mbid is not None and track_mbid or '', artist_mbid = artist_mbid is not None and artist_mbid or '', ) if not _track_exists(artist_name, track_name, timestamp): log.debug("Saving track: %r - %r", artist_name, track_name) return Item.objects.create_or_update( instance = t, timestamp = timestamp,
Returns the swappable Model that is active in this project. Swappable is in the form ``<APP_NAME>_<MODEL_NAME>_MODEL``. """ from django.db.models import get_model try: app_label, model_name = getattr(settings, swappable).split('.') except ValueError: raise ImproperlyConfigured("%s must be of the form 'app_label.model_name'") model = get_model(app_label, model_name) if model is None: raise ImproperlyConfigured("%s refers to model '%s' that has not been installed" % (swappable, getattr(settings, swappable))) return model get_swapped_model._cache = {} get_swapped_model = memoize(get_swapped_model, get_swapped_model._cache, 1) def on_task_prerun(sender, **kwargs): if not getattr(sender.run, '_django_task', False): return local_settings = kwargs['kwargs'].pop('local_settings', None) if not isinstance(local_settings, dict): warnings.warn("%s.%s task must be called with a valid local_settings named argument" % (sender.run.__module__, sender.run.__name__)) return if not getattr(sender.request, 'is_eager', False): # Setup settings: try: from raven.contrib.django.models import client except ImportError: pass
HttpResponseNotModified) from django.utils.http import http_date from django.utils.functional import memoize from django.views.static import directory_index, was_modified_since from merengue.pluggable.models import RegisteredPlugin _plugins_full_path_cache = {} def get_plugins_full_path(): all_plugins = RegisteredPlugin.objects.all().order_by('-active') paths = dict([(p.directory_name, p.get_path()) for p in all_plugins]) return paths get_plugins_full_path = memoize(get_plugins_full_path, _plugins_full_path_cache, 1) def serve(request, path, document_root=None, show_indexes=False): """ Serve static files combining media dirs for all plugins and apps. This is based on django.views.static.serve. To use, put a URL pattern such as:: (r'^(?P<path>.*)$', 'merengue.views.static.serve', {'document_root' : '/path/to/my/files/'}) in your URLconf. You must provide the ``document_root`` param. You may also set ``show_indexes`` to ``True`` if you'd like to serve a basic index of the directory. This index view will use the template hardcoded below,
from django.utils.functional import memoize from leetchi.api import LeetchiAPI from . import settings def _get_handler(): return LeetchiAPI(settings.API_PARTNER_ID, settings.API_PRIVATE_KEY, settings.API_PRIVATE_KEY_PASSWORD, sandbox=settings.API_USE_SANDBOX, host=settings.API_HOST) handler = memoize(_get_handler, {}, 0)()
def json(self): try: return json.loads(self.value) except (TypeError, ValueError): return {} def unmemoized_get_config(conf): try: c = Config.objects.get(key=conf) return c.value except Config.DoesNotExist: return get_config = memoize(unmemoized_get_config, _config_cache, 1) def set_config(conf, value): cf, created = Config.objects.get_or_create(key=conf) cf.value = value cf.save() _config_cache.clear() class ValidationJob(amo.models.ModelBase): application = models.PositiveIntegerField(choices=amo.APPS_CHOICES, db_column='application_id') curr_max_version = models.ForeignKey(AppVersion, related_name='validation_current_set') target_version = models.ForeignKey(AppVersion,
middleware = ResolverMatch(sub_match.func, sub_match.args, sub_match_dict, sub_match.url_name, self.app_name or sub_match.app_name, [self.namespace] + sub_match.namespaces) found.add(middleware.func) tried.append([pattern]) if len(found) == 0: raise MiddlewareResolver404({'tried': tried, 'path': new_path}) if len(found) == 0: raise MiddlewareResolver404({'path': path}) return list(found) def get_resolver(urlconf): if urlconf is None: from django.conf import settings urlconf = settings.ROOT_URLCONF return MiddlewareRegexURLResolver(r'^/', urlconf) get_resolver = memoize(get_resolver, _resolver_cache, 1) def resolve(path, urlconf=None): if urlconf is None: urlconf = get_urlconf() return get_resolver(urlconf).resolve(path) def get_urlconf(default=None): """ Returns the root URLconf to use for the current thread if it has been changed from the default one. """ return getattr(_urlconfs, "value", default)
except (ImportError, AttributeError): pass return backends, backends_by_auth # load backends from defined modules BACKENDS, BACKENDS_BY_AUTH = get_backends() def get_backend_from_auth(auth_backend_name): """ Return a valid backend to use for a specified auth_backend, None in other case """ backend_class = BACKENDS_BY_AUTH.get(auth_backend_name, None) if not backend_class: return None return get_backend(backend_class.name) get_backend_from_auth.__cache = {} get_backend = memoize(get_backend_from_auth, get_backend_from_auth.__cache, 1) def get_backend(name): """ Return a valid backend based on its name, None in other case """ backend_class = BACKENDS_BY_AUTH.get(name, None) if not backend_class: return None return backend_class() get_backend.__cache = {} get_backend = memoize(get_backend, get_backend.__cache, 1)
from .models import Translation fields = [f.name for f in Translation._meta.get_fields()] fields.remove("id") return fields try: from django.utils.lru_cache import lru_cache get_translation_field_names = lru_cache()(_get_translation_field_names) except ImportError: from django.utils.functional import memoize get_translation_field_names = memoize(_get_translation_field_names, {}, 0) @python_2_unicode_compatible class CachedTranslation(object): def __init__(self, **kwargs): self.fields = get_translation_field_names() attrs = self.fields + ["instance", "translation"] for attr in attrs: setattr(self, attr, None) self.__dict__.update(**kwargs) self.is_new = True
def check_for_language(lang_code): """ Checks whether there is a global language file for the given language code. This is used to decide whether a user-provided language is available. This is only used for language codes from either the cookies or session and during format localization. """ for path in all_locale_paths(): if gettext_module.find('django', path, [to_locale(lang_code)]) is not None: return True return False check_for_language = memoize(check_for_language, _checked_languages, 1) def get_supported_language_variant(lang_code, supported=None, strict=False): """ Returns the language-code that's listed in supported languages, possibly selecting a more generic variant. Raises LookupError if nothing found. If `strict` is False (the default), the function will look for an alternative country-specific variant when the currently checked is not found. """ if supported is None: from django.conf import settings supported = OrderedDict(settings.LANGUAGES) if lang_code: # if fr-CA is not supported, try fr-ca; if that fails, fallback to fr.
class Command(BaseCommand): help = 'Installs the named fixture(s) in the database.' args = "fixture [fixture ...]" option_list = BaseCommand.option_list + ( make_option('--database', action='store', dest='database', default=DEFAULT_DB_ALIAS, help='Nominates a specific database to load ' 'fixtures into. Defaults to the "default" database.'), make_option('--ignorenonexistent', '-i', action='store_true', dest='ignore', default=False, help='Ignores entries in the serialized data for fields' ' that do not currently exist on the model.'), ) def handle(self, *fixture_labels, **options): self.ignore = options.get('ignore') self.using = options.get('database') if not len(fixture_labels): raise CommandError( "No database fixture specified. Please provide the path " "of at least one fixture in the command line.") self.verbosity = int(options.get('verbosity')) with transaction.commit_on_success_unless_managed(using=self.using): self.loaddata(fixture_labels) # Close the DB connection -- unless we're still in a transaction. This # is required as a workaround for an edge case in MySQL: if the same # connection is used to create tables, load data, and query, the query # can return incorrect results. See Django #7572, MySQL #37735. if transaction.get_autocommit(self.using): connections[self.using].close() def loaddata(self, fixture_labels): connection = connections[self.using] # Keep a count of the installed objects and fixtures self.fixture_count = 0 self.loaded_object_count = 0 self.fixture_object_count = 0 self.models = set() self.serialization_formats = serializers.get_public_serializer_formats( ) self.compression_formats = { None: open, 'gz': gzip.GzipFile, 'zip': SingleZipReader } if has_bz2: self.compression_formats['bz2'] = bz2.BZ2File with connection.constraint_checks_disabled(): for fixture_label in fixture_labels: self.load_label(fixture_label) # Since we disabled constraint checks, we must manually check for # any invalid keys that might have been added table_names = [model._meta.db_table for model in self.models] try: connection.check_constraints(table_names=table_names) except Exception as e: e.args = ("Problem installing fixtures: %s" % e, ) raise # If we found even one object in a fixture, we need to reset the # database sequences. if self.loaded_object_count > 0: sequence_sql = connection.ops.sequence_reset_sql( no_style(), self.models) if sequence_sql: if self.verbosity >= 2: self.stdout.write("Resetting sequences\n") cursor = connection.cursor() for line in sequence_sql: cursor.execute(line) cursor.close() if self.verbosity >= 1: if self.fixture_object_count == self.loaded_object_count: self.stdout.write( "Installed %d object(s) from %d fixture(s)" % (self.loaded_object_count, self.fixture_count)) else: self.stdout.write( "Installed %d object(s) (of %d) from %d fixture(s)" % (self.loaded_object_count, self.fixture_object_count, self.fixture_count)) def load_label(self, fixture_label): """ Loads fixtures files for a given label. """ for fixture_file, fixture_dir, fixture_name in self.find_fixtures( fixture_label): _, ser_fmt, cmp_fmt = self.parse_name( os.path.basename(fixture_file)) open_method = self.compression_formats[cmp_fmt] fixture = open_method(fixture_file, 'r') try: self.fixture_count += 1 objects_in_fixture = 0 loaded_objects_in_fixture = 0 if self.verbosity >= 2: self.stdout.write( "Installing %s fixture '%s' from %s." % (ser_fmt, fixture_name, humanize(fixture_dir))) objects = serializers.deserialize( ser_fmt, fixture, using=self.using, ignorenonexistent=self.ignore) for obj in objects: objects_in_fixture += 1 if router.allow_syncdb(self.using, obj.object.__class__): loaded_objects_in_fixture += 1 self.models.add(obj.object.__class__) try: obj.save(using=self.using) except (DatabaseError, IntegrityError) as e: e.args = ( "Could not load %(app_label)s.%(object_name)s(pk=%(pk)s): %(error_msg)s" % { 'app_label': obj.object._meta.app_label, 'object_name': obj.object._meta.object_name, 'pk': obj.object.pk, 'error_msg': force_text(e) }, ) raise self.loaded_object_count += loaded_objects_in_fixture self.fixture_object_count += objects_in_fixture except Exception as e: if not isinstance(e, CommandError): e.args = ("Problem installing fixture '%s': %s" % (fixture_file, e), ) raise finally: fixture.close() # Warn if the fixture we loaded contains 0 objects. if objects_in_fixture == 0: warnings.warn( "No fixture data found for '%s'. (File format may be " "invalid.)" % fixture_name, RuntimeWarning) def _find_fixtures(self, fixture_label): """ Finds fixture files for a given label. """ fixture_name, ser_fmt, cmp_fmt = self.parse_name(fixture_label) databases = [self.using, None] cmp_fmts = list( self.compression_formats.keys()) if cmp_fmt is None else [cmp_fmt] ser_fmts = serializers.get_public_serializer_formats( ) if ser_fmt is None else [ser_fmt] # Check kept for backwards-compatibility; it doesn't look very useful. if '.' in os.path.basename(fixture_name): raise CommandError( "Problem installing fixture '%s': %s is not a known " "serialization format." % tuple(fixture_name.rsplit('.'))) if self.verbosity >= 2: self.stdout.write("Loading '%s' fixtures..." % fixture_name) if os.path.sep in fixture_name: fixture_dirs = [os.path.dirname(fixture_name)] fixture_name = os.path.basename(fixture_name) else: fixture_dirs = self.fixture_dirs suffixes = ('.'.join(ext for ext in combo if ext) for combo in product(databases, ser_fmts, cmp_fmts)) targets = set('.'.join((fixture_name, suffix)) for suffix in suffixes) fixture_files = [] for fixture_dir in fixture_dirs: if self.verbosity >= 2: self.stdout.write("Checking %s for fixtures..." % humanize(fixture_dir)) fixture_files_in_dir = [] for candidate in glob.iglob( os.path.join(fixture_dir, fixture_name + '*')): if os.path.basename(candidate) in targets: # Save the fixture_dir and fixture_name for future error messages. fixture_files_in_dir.append( (candidate, fixture_dir, fixture_name)) if self.verbosity >= 2 and not fixture_files_in_dir: self.stdout.write("No fixture '%s' in %s." % (fixture_name, humanize(fixture_dir))) # Check kept for backwards-compatibility; it isn't clear why # duplicates are only allowed in different directories. if len(fixture_files_in_dir) > 1: raise CommandError( "Multiple fixtures named '%s' in %s. Aborting." % (fixture_name, humanize(fixture_dir))) fixture_files.extend(fixture_files_in_dir) if fixture_name != 'initial_data' and not fixture_files: # Warning kept for backwards-compatibility; why not an exception? warnings.warn("No fixture named '%s' found." % fixture_name) return fixture_files _label_to_fixtures_cache = {} find_fixtures = memoize(_find_fixtures, _label_to_fixtures_cache, 2) @cached_property def fixture_dirs(self): """ Return a list of fixture directories. The list contains the 'fixtures' subdirectory of each installed application, if it exists, the directories in FIXTURE_DIRS, and the current directory. """ dirs = [] for path in get_app_paths(): d = os.path.join(path, 'fixtures') if os.path.isdir(d): dirs.append(d) dirs.extend(list(settings.FIXTURE_DIRS)) dirs.append('') dirs = [upath(os.path.abspath(os.path.realpath(d))) for d in dirs] return dirs def parse_name(self, fixture_name): """ Splits fixture name in name, serialization format, compression format. """ parts = fixture_name.rsplit('.', 2) if len(parts) > 1 and parts[-1] in self.compression_formats: cmp_fmt = parts[-1] parts = parts[:-1] else: cmp_fmt = None if len(parts) > 1 and parts[-1] in self.serialization_formats: ser_fmt = parts[-1] parts = parts[:-1] else: ser_fmt = None name = '.'.join(parts) return name, ser_fmt, cmp_fmt
_migrations = SortedDict() def _get_migration(import_path): """ Imports the staticfiles migration class described by import_path, where import_path is the full Python path to the class. This code has been borrowed from Django's staticfiles contrib. """ from .base import MigrateModel module, attr = import_path.rsplit('.', 1) try: mod = import_module(module) except ImportError, e: raise ImproperlyConfigured('Error importing module %s: "%s"' % (module, e)) try: Migration = getattr(mod, attr) except AttributeError: raise ImproperlyConfigured('Module "%s" does not define a "%s" ' 'class.' % (module, attr)) if not issubclass(Migration, MigrateModel): raise ImproperlyConfigured('Migration "%s" is not a subclass of "%s"' % (Migration, MigrateModel)) return Migration() get_migration = memoize(_get_migration, _migrations, 1)
models.signals.post_save.connect(denormalize_votes, sender=Vote) models.signals.post_delete.connect(denormalize_votes, sender=Vote) cached_langs = {} def get_lang(lang_code): """ Get a Language instance for a particular language, and remember it so that it doesn't have to be looked up again. """ return Language.objects.get(id=lang_code) get_lang = memoize(get_lang, cached_langs, 1) class Language(models.Model): """ A database object representing a language. Instances of Language can be used in filter expressions to select only objects that apply to a particular language. For example: >>> en = Language.get('en') >>> english_sentences = Sentence.objects.filter(language=en) """ id = models.CharField(max_length=16, primary_key=True) name = models.TextField(blank=True)
# These measurements have to be exactly the same as the ones used in # price_tags.css. If they are not the image might be distorted enough # to not register on the scanner. if settings.DEBUG: assert (height == 1) assert (expect_width is None or width == expect_width) pil_image.save(memory_file, format=ext) data = memory_file.getvalue() dataurl_format = 'data:{mime_type};base64,{base64_data}' return dataurl_format.format(mime_type=mime_type, base64_data=base64.encodestring(data)) get_dataurl = memoize(generate_dataurl, barcode_dataurl_cache, 2) @register.simple_tag def barcode_dataurl(code, ext, expect_width=143): return get_dataurl(code, ext, expect_width) @register.simple_tag def barcode_css(low=4, high=6, target=None, container=None, compress=False): target = target or ".barcode_img.barcode_img{0}" container = container or ".barcode_container.barcode_container{0}" css = """ {target}, {container} {{ width: {px}px;
from django.utils.functional import memoize from base import BasePlan from fewest import FewestFiles from reusable import ReusableFiles from separate import SeparateEverything __all__ = ['MissingMediaPlan', 'get_plan', 'plan_options', 'get_for_context'] __plan_cache = {} def get_plan(module, attr): plans = __import__(module, globals(), locals(), attr) return getattr(plans, attr) get_plan = memoize(get_plan, __plan_cache, 2) def plan_options(**kwargs): BasePlan.set_options(**kwargs) class MissingMediaPlan(Exception): pass def get_for_context(context, render_full_page): try: from skylark.conf import settings plan = get_plan( settings.SKYLARK_PLANS,
if not detected: return None # Detect Platform platform = PLATFORM_OTHER.short for pattern in PLATFORM_PATTERNS: if ua.find(pattern[0]) >= 0: platform = pattern[1] break detected['platform'] = platform return detected _ua_parse_cache = {} ua_parse = memoize(ua_parse, _ua_parse_cache, 1) def detect_language(request): """ Pick a user's preferred language from their Accept-Language headers. """ accept = request.META.get('HTTP_ACCEPT_LANGUAGE') if not accept: return '' ranked_languages = parse_accept_lang_header(accept) for lang, q in ranked_languages: locale = to_locale(lang).replace('_', '-') if locale in product_details.languages: return locale
""" res = {} get_all_subclasses(subtype, res) return res def get_all_documents_with_level(only_accept_files=False): lst = [] level="=>" get_all_subclasses_with_level(Document, lst, level) classes = get_all_documents() if only_accept_files: return [(n, l) for n, l in lst if classes[n].ACCEPT_FILES] return lst get_all_documents_with_level = memoize(get_all_documents_with_level, {}, 1) def get_best_document_type(files): """ .. versionadded:: 2.0 Returns the document type (str) which returns the highest creation score for the uploaded files (list of :class:`PrivateFile`). .. seealso:: :meth:`Document.get_creation_score` """ dtype = "Document" max_score = 0 for name, cls in get_all_documents().iteritems():
# django.views.decorators.cache fully_decorated = cache_page(60*15)(fully_decorated) fully_decorated = cache_control(private=True)(fully_decorated) fully_decorated = never_cache(fully_decorated) # django.contrib.auth.decorators # Apply user_passes_test twice to check #9474 fully_decorated = user_passes_test(lambda u:True)(fully_decorated) fully_decorated = login_required(fully_decorated) fully_decorated = permission_required('change_world')(fully_decorated) # django.contrib.admin.views.decorators fully_decorated = staff_member_required(fully_decorated) # django.utils.functional fully_decorated = memoize(fully_decorated, {}, 1) fully_decorated = allow_lazy(fully_decorated) fully_decorated = lazy(fully_decorated) class DecoratorsTest(TestCase): def test_attributes(self): """ Tests that django decorators set certain attributes of the wrapped function. """ # Only check __name__ on Python 2.4 or later since __name__ can't be # assigned to in earlier Python versions. if version_info[0] >= 2 and version_info[1] >= 4: self.assertEquals(fully_decorated.__name__, 'fully_decorated')
def json(self): try: return json.loads(self.value) except (TypeError, ValueError): return {} def unmemoized_get_config(conf): try: c = Config.objects.get(key=conf) return c.value except Config.DoesNotExist: return get_config = memoize(unmemoized_get_config, _config_cache, 1) def set_config(conf, value): cf, created = Config.objects.get_or_create(key=conf) cf.value = value cf.save() _config_cache.clear() class EmailPreviewTopic(object): """Store emails in a given topic so an admin can preview before re-sending. A topic is a unique string identifier that groups together preview emails. If you pass in an object (a Model instance) you will get a poor man's
if not can_fail: raise else: try: lookup_view = getattr(mod, func_name) if not callable(lookup_view): raise ViewDoesNotExist( "Could not import %s.%s. View is not callable." % (mod_name, func_name)) except AttributeError: if not can_fail: raise ViewDoesNotExist( "Could not import %s. View does not exist in module %s." % (lookup_view, mod_name)) return lookup_view get_callable = memoize(get_callable, _callable_cache, 1) def get_resolver(urlconf): if urlconf is None: from django.conf import settings urlconf = settings.ROOT_URLCONF return RegexURLResolver(r'^/', urlconf) get_resolver = memoize(get_resolver, _resolver_cache, 1) def get_ns_resolver(ns_pattern, resolver): # Build a namespaced resolver for the given parent urlconf pattern. # This makes it possible to have captured parameters in the parent # urlconf pattern. ns_resolver = RegexURLResolver(ns_pattern, resolver.url_patterns) return RegexURLResolver(r'^/', [ns_resolver])
""" Returns filter instances configured in ASSETFILES_FILTERS. """ for filter_path in settings.FILTERS: yield get_filter(filter_path) def _get_filter(import_path): """ Imports the assetfiles filter class described by import_path, where import_path is the full Python path to the class. """ module, attr = import_path.rsplit('.', 1) try: mod = import_module(module) except ImportError as e: raise ImproperlyConfigured('Error importing module %s: "%s"' % (module, e)) try: Filter = getattr(mod, attr) except AttributeError: raise ImproperlyConfigured('Module "%s" does not define a "%s" ' 'class.' % (module, attr)) if not issubclass(Filter, BaseFilter): raise ImproperlyConfigured('Filter "%s" is not a subclass of "%s"' % (Filter, BaseFilter)) return Filter() _filters = SortedDict() get_filter = memoize(_get_filter, _filters, 1)
} # Handles custom attributes added to helpers for attribute_name, value in attrs.items(): if attribute_name not in response_dict: response_dict[attribute_name] = value if 'csrf_token' in context: response_dict['csrf_token'] = context['csrf_token'] return response_dict def whole_uni_formset_template(template_pack=TEMPLATE_PACK): return get_template('%s/whole_uni_formset.html' % template_pack) whole_uni_formset_template = memoize(whole_uni_formset_template, {}, 1) def whole_uni_form_template(template_pack=TEMPLATE_PACK): return get_template('%s/whole_uni_form.html' % template_pack) whole_uni_form_template = memoize(whole_uni_form_template, {}, 1) class CrispyFormNode(BasicNode): def render(self, context): c = self.get_render(context) if c['is_formset']: template = whole_uni_formset_template(self.template_pack) else:
path = '/' + post return force_ascii(path.lower()) def get_view(lookup_view): """ Uses similar logic to django.urlresolvers.get_callable, but always raises on failures and supports class based views. """ lookup_view = lookup_view.encode('ascii') mod_name, func_or_class_name = get_mod_func(lookup_view) assert func_or_class_name != '' view = getattr(import_module(mod_name), func_or_class_name) assert callable(view) or hasattr(view, 'as_view') return view get_view = memoize(get_view, _view_cache, 1) def get_redirect_url_with_query_string(request, url): query_string = request.META.get('QUERY_STRING', '') if query_string: return '{}?{}'.format(url, query_string) return url def force_cache_invalidation(request): ''' Returns true if a request from contains the Cache-Control: no-cache header ''' return 'no-cache' in request.META.get('HTTP_CACHE_CONTROL', '')
from django_hosts.defaults import host as host_cls _hostconf_cache = {} _hostconf_module_cache = {} _host_patterns_cache = {} _host_cache = {} def get_hostconf(): try: return settings.ROOT_HOSTCONF except AttributeError: raise ImproperlyConfigured("Missing ROOT_HOSTCONF setting") get_hostconf = memoize(get_hostconf, _hostconf_cache, 0) def get_hostconf_module(hostconf=None): if hostconf is None: hostconf = get_hostconf() return import_module(hostconf) get_hostconf_module = memoize(get_hostconf_module, _hostconf_module_cache, 1) def get_host(name): for host in get_host_patterns(): if host.name == name: return host
@property def json(self): try: return json.loads(self.value) except TypeError, ValueError: return {} def get_config(conf): try: c = Config.objects.get(key=conf) return c.value except Config.DoesNotExist: return get_config = memoize(get_config, _config_cache, 1) def set_config(conf, value): cf, created = Config.objects.get_or_create(key=conf) cf.value = value cf.save() _config_cache.clear() class ValidationJob(amo.models.ModelBase): application = models.ForeignKey(Application) curr_max_version = models.ForeignKey(AppVersion, related_name='validation_current_set') target_version = models.ForeignKey(AppVersion, related_name='validation_target_set')
# Handles custom attributes added to helpers for attribute_name, value in attrs.items(): if attribute_name not in response_dict: response_dict[attribute_name] = value if 'csrf_token' in context: response_dict['csrf_token'] = context['csrf_token'] return response_dict def whole_uni_formset_template(template_pack=TEMPLATE_PACK): return get_template('%s/whole_uni_formset.html' % template_pack) whole_uni_formset_template = memoize(whole_uni_formset_template, {}, 1) def whole_uni_form_template(template_pack=TEMPLATE_PACK): return get_template('%s/whole_uni_form.html' % template_pack) whole_uni_form_template = memoize(whole_uni_form_template, {}, 1) class CrispyFormNode(BasicNode): def render(self, context): c = self.get_render(context) if c['is_formset']: template = whole_uni_formset_template(self.template_pack)