2

I have Python script written in 3.x version which I need to transform in 2.7 due to environment limitations.

I managed to refactor most of the syntax difs and module imports but I came across one line which I do not know how to handle:

def readConfigFile(self, file):
        config = ConfigParser.ConfigParser(interpolation = ConfigParser.ExtendedInterpolation())
        config.optionxform = str
        config.read(file)

**config = ConfigParser.ConfigParser(interpolation = ConfigParser.ExtendedInterpolation())**

I found some reference here ( Python ConfigParser interpolation from foreign section) , but I do not know exactly how this can be replaced(refactored).

The ConfigParser :

So my question , is there any way to refactor the code above to work in python 2.7 and retain functionality ?

Community
  • 1
  • 1
gbarzu
  • 45
  • 1
  • 6

2 Answers2

0

As stated in another question (Python ConfigParser interpolation from foreign section) no, unfortunately there is not an easy way to do this. You would need to create your own parser for the returned values and iterate over them. This seems a bit kludgy though.

Community
  • 1
  • 1
The NetYeti
  • 353
  • 3
  • 12
0

I got this to work in ArcPy 2.7 by using

from backports import configparser

config = configparser.ConfigParser(interpolation=configparser.ExtendedInterpolation())
config.read('path/to/the/config_file.cfg')

I then used the following code in my config file:

[Section 1]
foo: bar

...

[Section 5]
foo:  ${Section1:foo}

Trying the call in the interpreter produces the predicted results:

config['Section 5']['foo']
>>> bar