Login

Dynamic Backends

Author:
LorenDavie
Posted:
September 28, 2008
Language:
Python
Version:
1.0
Tags:
backend dynamic-factory
Score:
0 (after 0 ratings)

This allows various implementations of a common interface to be loaded. Back end modules can be specified in settings.py, and from there be loaded and treated polymorphically by an application.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
# ================================
# = Dynamic Backends for Modules =
# ================================
def load_backend(setting_name,default_name=None):
    """
    Dynamically loads a backend module from the specified django settings
    name, or falling back to the default value if the settings is empty
    or unspecified in the settings.
    """
    backend_name = None
    from django.conf import settings as django_settings
    if hasattr(django_settings,setting_name):
        backend_name = getattr(django_settings,setting_name) or default_name
    else:
        backend_name = default_name
    
    if backend_name:
        return __import__(backend_name,'','',[''])
    else:
        raise ImportError, 'No backend module specified for %s in Django settings, and no default available.' % setting_name

More like this

  1. Load dynamically loaded form javascript assets using dajax by PhilipGarnero 2 months, 4 weeks ago
  2. Load customized SQL by roppert 6 years, 2 months ago
  3. email backend which use sendmail subprocess by pawnhearts 5 years, 4 months ago
  4. Dynamic Test Loading by cronosa 5 years, 4 months ago
  5. clean validation for dynamic form fields by leveillej 5 years ago

Comments

Please login first before commenting.