views:

37

answers:

1

If I have a seperate class for my db calls, and I create another implementation of the db layer but say with a different data store.

Is there a way for me to completly swap out the implementation without having to change allot of code?

i.e. I am starting a project, so I can design things properly to achieve this from the get-go.

Note: I will use this pattern for other parts of the site also, not just the db layer so its not really specific to db layer only.

+2  A: 

As long as two modules implement exactly the same interface (classes with the same names, methods, and other attributes, functions with the same names and signatures, ...) you can pick one or the other at the time your application is starting up, for example on the basis of some configuration file, and import the chosen one under a fixed name. All the rest of your application can then use that fixed name and, net of the startup code, be blissfully unaware of any shenanigans that may have been done at the start.

For example, consider a simplified case:

# english.py
def greet(): return 'Hello!'

# italian.py
def greet(): return 'Ciao!'

# french.py
def greet(): return 'Salut!'

# config.py
langname = 'italian'

# startit.py
import config
import sys
lang = __import__(config.langname)
sys.modules['lang'] = lang

Now, all the rest of the application can just import lang, and it will be getting under that name the italian module, so, when calling lang.greet(), it will get the string 'Ciao!'.

Of course, in real life you'll have multiple modules, each with multiple functions, classes, and whatnot, but the general principles stay very similar. Just take special care about modules with qualified names (such as foo.bar), i.e., modules which must reside in a package (in this case, foo). For those, you can't just use __import__'s return value, but must use a slightly more roundabout approach, such as:

import sys
def importanyasname(actualname, fakename):
    __import__(actualname)
    sys.modules[fakename] = sys.modules[actualname]

that is, ignore __import__'s return value, and reach right for the value that's left (with the actual name as the key) in the sys.modules dictionary -- that is the module object you seek, and that you can set back into sys.modules with the "fake name" by which all the rest of the application will be able to blissfully import it any time.

Alex Martelli
thanks alex, slightly off topic, do you prefer to run python on a web server like apache or do you just run 'paster' on different ports, and let haproxy distribute amongst them? it seems high traffic shops are using the later.
Blankman
how would you use the 'importanyasname'? little confused as to what is going on there.
Blankman
@Blankman, I'm usually in situations different from what you envision (coding to run on Google production systems, either on App Engine or otherwise), so my experience is not directly applicable (for personal/open-source uses I use things like ligthttpd, tornado, twisted's own servers, and the like -- apache if I have to, of course;-). `importasanyname` is meant to be called, instead of placing the two statements at the end of `startit.py` in the previous snippet, if you need to support modules whose real names are qualified-names.
Alex Martelli