Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Consider using plain Python files as configuration files.</p> <p>An example (<code>example.conf</code>):</p> <pre><code># use normal python comments value1 = 32 value2 = u"A unicode value" value3 = "A plain string value" value4 = ["lists", "are", "handy"] value5 = {"and": "so", "are": "dictionaries"} </code></pre> <p>In your program, load the config file using <code>execfile</code> (<a href="http://docs.python.org/library/functions.html#execfile">2.7 docs</a>):</p> <pre><code>if __name__ == "__main__": config = {} execfile("example.conf", config) # python 3: exec(open("example.conf").read(), config) print config["value1"] print config["value5"] </code></pre> <p>I like this approach, for the following reasons:</p> <ul> <li>In the simple case, the format is as easy to author as an INI-style config file. It also shares an important characteristic with INI files: it is very suitable for version control (this is less true for XML and maybe also for JSON)</li> <li>I like the flexibility that comes with having the config file in an actual programming language.</li> </ul> <p>The approach is widely used, a few examples:</p> <ul> <li>A Django site's settings lives inside <a href="https://docs.djangoproject.com/en/1.3/topics/settings/">settings.py</a>. Django does not use <code>execfile</code>, it uses <code>import</code> to read/execute <code>settings.py</code> AFAIK, but the end result is the same: the code inside the settings file is executed.</li> <li>The bash shell reads and executes <code>~/.bashrc</code> on startup.</li> <li>The Python interpreter imports (and executes) <code>site.py</code> on startup.</li> </ul>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload