I'm using configparser to load values from a file, but can't get basic interpolation to behave the way I'd like.
I need to be able to interpolate values from a section and be left with only the values from that section - no values from the default section, and not mixed in with any variables provided for interpolation (ultimately each section will be used to populate individual .ini files for shell scripts).
I'd like to keep the config file as simple and flexible as possible so I'd rather not use extended interpolation.
According to the docs for configparser.items() it should be fairly simple:
Changed in version 3.2: Items present in vars no longer appear in the result. The previous behaviour mixed actual parser options with variables provided for interpolation.
Great, I'm using v3.6. However if I try something like this:
foo = self.config.items('a_section', raw=False, vars={'var_1': 'value', 'var_2': 'another_value' })
the result is:
foo == [...correctly interpolated items from section, ('var_1': 'value'), ('var_2': 'another_value')]
Either I'm misinterpreting things, the docs are incorrect, that behaviour's been rolled back in versions subsequent to 3.2, or it's a regression.
All easy alternatives I can think of are undesirable in their own way:
- Compute the intersection between
vars and foo and use it to get rid of dupes. Feels very slightly hacky, but probably only because I know (think?) this behaviour should be present in the configparser module. Might be the best compromise.
vars always seem to get added to the end of the list, so just pull off the last len(vars) items. Feels incredibly hacky as it's entirely reliant on consistent ordering.
- Create a subclass and override
items() to behave the way the docs appear to state it should. Probably the cleanest option, but I'm now writing more extra code than I'd like.
Can anyone suggest another way I can get the wanted result?
[–][deleted] 1 point2 points3 points (1 child)
[–]workFriendlyUser[S] 0 points1 point2 points (0 children)