Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to escape a % sign in a configobj value?

I'd like to use both parse time and runtime interpolation for values of a configobj configuration file. The easiest way to do simple string interpolation in Python is "%(foo)s" % somedict. Unfortunately configobj uses the same interpolation mechanism and I have not yet found a way to escape it. Ideally my value would look like:

othervar = foo
someconfigobjkey = %(othervar)s %%(runtimevar)s

However configobj tries (and fails) to replace the second variable. Another way to answer this question is to provide a different (configobj) way to do both parse time and runtime interpolation.

like image 494
Helmut Grohne Avatar asked Oct 28 '10 14:10

Helmut Grohne


1 Answers

Digging deeper in the documentation I found a solution that fits better than the one proposed by bobince: Use a different interpolation engine, such as interpolation="template". It has two advantages.

  1. Does not interfere with % signs.
  2. Supports escaping. ($$ gets interpolated to $.)

The example would look like:

othervar = foo
someconfigobjkey = $othervar %(runtimevar)s
like image 68
Helmut Grohne Avatar answered Sep 29 '22 13:09

Helmut Grohne