Skip to content

Instantly share code, notes, and snippets.

@codysoyland
Created June 21, 2011 21:47
Show Gist options
  • Select an option

  • Save codysoyland/1038987 to your computer and use it in GitHub Desktop.

Select an option

Save codysoyland/1038987 to your computer and use it in GitHub Desktop.

Revisions

  1. codysoyland revised this gist Jun 21, 2011. 1 changed file with 4 additions and 0 deletions.
    4 changes: 4 additions & 0 deletions temporary_settings.py
    Original file line number Diff line number Diff line change
    @@ -1,3 +1,7 @@
    # Example usage:
    # with temporary_settings(CELERY_ALWAYS_EAGER=True):
    # run_task.delay() # runs task with eager setting enabled.

    from contextlib import contextmanager
    from django.conf import settings

  2. codysoyland created this gist Jun 21, 2011.
    16 changes: 16 additions & 0 deletions temporary_settings.py
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,16 @@
    from contextlib import contextmanager
    from django.conf import settings

    @contextmanager
    def temporary_settings(**temp_settings):
    orig_settings = {}
    for key, value in temp_settings.iteritems():
    if hasattr(settings, key):
    orig_settings[key] = getattr(settings, key)
    setattr(settings, key, value)
    yield
    for key, value in temp_settings.iteritems():
    if orig_settings.has_key(key):
    setattr(settings, key, orig_settings[key])
    else:
    delattr(settings, key)