title: Storing configuration defaults in a Python docstring?
body: <p>I have a Python app with many modules. There's a YAML config file which contains configuration settings for each module. In each module's <strong>init</strong>(), I load the config and then process it into a config dictionary for that module.</p>
  
  <p>The processing is ugly right now. Dozens and dozens of lines at the beginning of each module with a lot of stuff like:</p>
  
  <pre><code>if 'foo' not in config:
      config['foo'] = bar
  </code></pre>
  
  <p>or</p>
  
  <pre><code>if 'foo' in config:
      config['foo'] = string_to_list(config['foo'])
  else:
      config['foo'] = list()
  </code></pre>
  
  <p>etc.</p>
  
  <p>So now I want to write a centralized config processing method that each module can use. I'm thinking that I want to use a YAML-formatted string to specify what the final config should look like. (Which settings are mandatory, default values, object types, etc.)</p>
  
  <p>I'm thinking the config specification in each module could be something like this: (with the type|default|required values)</p>
  
  <pre><code>config_template = """MySection:
                         setting1: int|0
                         setting2: string|None|required
                         setting3: int|10"""
  </code></pre>
  
  <p>So far, so good. My real question is whether there's any way for me to save this config specification thing in each module in a docstring for <strong>init</strong>() or something. I'm thinking that since I'm essentially defining exactly what the module expects for its config, along with the defaults and the types, if I could store this config thing in the docstring then I ought to be able to write something and/or configure sphinx to pretty it up for the documentation?</p>
  
  <p>So I wonder if this approach sounds sane in general, and, if so, is there a way I can store this config info in the docstring to get it to work as double duty?</p>
  
  <p>EDIT: I considered setting up a dictionary with default values first, and certainly that will take care of the defaults. But there are a lot of cases where I need the values to be of a certain type. For example, maybe some have to be a list, but if a single item is entered into the config file it will be read as a string, so I need the config processor to convert the string to a list with that string as its only item.</p>
  
  <p>EDIT 2: The reason I was asking about the docstring is because I was thinking that my config specification would essentially tell a programmer what this modules was expecting in terms of a config dictionary, so if I can do it in the docstring then I can just specify it once and have the module use it for its config as well as have it appear in the sphinx documentation. But if that's not possible, then so be it. I can use it in a variable (like in my example above) and then write the docstring manually.</p>
  
  <p>Thanks!
  Brian</p>