However writing a parser for configuration files takes time, and the standard ones (.ini and .xml) have their limitations. Lucky for us each Python program comes with the full interpreter bundled. We can do stuff like:
# settings.py
from sys import platform
if platform == "win32":
APP_HOME = "c:\\my_cool_app"
else:
APP_HOME = "/opt/my_cool_app"
Then using
__import__
in our application we can load the configuration file and parse it without a sweat:
# my_cool_app.py
settings = __import__("settings.py")
print settings.APP_HOME
To view this approach taken to extreme, have a look at http://www.unixreview.com/documents/s=9133/ur0404e/
No comments:
Post a Comment