Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Reference variable from another ini section

Tags:

python

ini

Is it possible to reference a variable within an ini from another section?

I know you can do the following

[env]
dir  = /home/a
dir2 = %(dir)s/b

But what happens if I have two sections and want to reference the variable from that section?

[env]
name =  DEV

[dir]
home = /home/<name from env here>/scripts

Thanks

like image 754
mlevit Avatar asked Jul 31 '14 00:07

mlevit


1 Answers

See the documentation on configparser. Create a parser with extended interpolation. Use ${section:option} syntax to reference options from other sections.

from configparser import ConfigParser, ExtendedInterpolation

parser = ConfigParser(interpolation=ExtendedInterpolation())
parser.read_string('''[env]
name = DEV

[dir]
home = /home/${env:name}/scripts
''')

print(parser['dir']['home'])
like image 163
davidism Avatar answered Nov 15 '22 17:11

davidism