How To Use Variables Already Defined In Configparser
Solution 1:
You can use ConfigParser interpolation
On top of the core functionality, SafeConfigParser supports interpolation. This means values can contain format strings which refer to other values in the same section, or values in a special DEFAULT section. Additional defaults can be provided on initialization.
For example:
[My Section] foodir: %(dir)s/whatever dir=frob long: this value continues in the next line
would resolve the %(dir)s to the value of dir (frob in this case). All reference expansions are done on demand.
Your example becomes:
[general]
name: my_namebase_dir: /home/myhome/expexe_dir: %(base_dir)s/bin
Solution 2:
Instead of "${foo}", write "%(foo)s". (See http://docs.python.org/library/configparser.html and search for "interpolation". This works for either an ordinary ConfigParser or a SafeConfigParser.)
Solution 3:
In Python 3, you can use ${base_dir}/bin
, and the extended interpolation allows you to use variables from other sections. Example:
[Common]
home_dir: /Userslibrary_dir: /Librarysystem_dir: /Systemmacports_dir: /opt/local
[Frameworks]
Python: 3.2path: ${Common:system_dir}/Library/Frameworks/
[Arthur]
nickname: Two Shedslast_name: Jacksonmy_dir: ${Common:home_dir}/twoshedsmy_pictures: ${my_dir}/Picturespython_dir: ${Frameworks:path}/Python/Versions/${Frameworks:Python}
Post a Comment for "How To Use Variables Already Defined In Configparser"