Created
February 13, 2009 18:02
-
-
Save tdavis/64029 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
""" | |
This script assumes the file config.py and optional file config_local.py are in | |
the same directory as this file. Of course, you wouldn't need this if you just | |
had config_local.py exist by default and have it `import * from config` ;) | |
A few tweaks could be made to remove the need to have the `import *` part in | |
config_local.py, basically: | |
def __getattr__(self, name): | |
return getattr(self._mod_local, name) or getattr(self._mod, name) | |
With necessary additions/changes made elsewhere. | |
Hope this helps! | |
""" | |
from os import path | |
class Config(object): | |
def __init__(self): | |
this_dir = path.abspath(path.split(__file__)[0]) | |
p = path.join(this_dir,'config_local.py') | |
if path.isfile(p): | |
mod_name = 'config_local' | |
else: | |
mod_name = 'config' | |
self._mod = __import__(mod_name, {}, {}, ['']) | |
def __getattr__(self, name): | |
return getattr(self._mod, name) | |
def __setattr__(self, name, value): | |
# Prevent infinite loop | |
if name == '_mod': | |
self.__dict__[name] = value | |
else: | |
return setattr(self._mod, name, value) | |
conf = Config() | |
# In another file: | |
# from <this_module_name> import conf |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment