Python Forum
ConfigParser(dict_type=) not behaving as expected
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
ConfigParser(dict_type=) not behaving as expected
#1
I am using the configparser module. I am having trouble understanding the
ConfigParser(dict_type=)
method. My code:
class MultiDict(OrderedDict): def __init__(self): self.x = 0 def __setitem__(self, key, value): if key == 'textsize': key += str(self.x) OrderedDict.__setitem__(self, key, value) print(self) self.x += 1 ... config1 = cp.ConfigParser(dict_type=MultiDict) data = config1.read('ini_file.ini')
INI file:
[Menu] MainMenuRandomizer=0 OptionsInfo=1 RandomizerQty=3 TextFont = Calibri TextSize = 22 TextFontStatus = Courier New TextSize = 11 TextColorNormal = 250,240,180 TextShadowColorNormal = 50,50,50 TextColorSelected = 255,255,255 TextShadowColorSelected = 50,50,50 TextColorEditing = 255,100,50 TextShadowColorEditing = 50,50,50 PositionX = 800 PositionY = 120 InfoPositionX = 480 InfoPositionY = 800 TitleColumnSize = 1000 MainItemColumnSize = 100 ItemColumnSize = 300 InfoColumnSize = 960 RowSpace = 0 RowsPerPage = 30 KeyEnable = 24 KeyUp = 200 KeyDown = 208 KeyLeft = 203 KeyRight = 205 KeyPageUp = 201 KeyPageDown = 209 KeyAdd = 78 KeySubtract = 74 KeySave = 28 KeyEditing = 156 StepValue =0.1 Decimals = 4
As you can see from the config file, there are two options named "TextSize". When I run the above code, I get the following error:
configparser.DuplicateOptionError: While reading from 'ini_file.ini' [line 182]: option 'textsize' in section 'Menu' already exists
The print statement in my class shows that the ordered dictionary is getting updated, like so:
MultiDict([('mainmenurandomizer', ['0']), ('optionsinfo', ['1']), ('randomizerqty', ['3']), ('textfont', ['Calibri']), ('textsize4', ['22'])]
Why is the read operation still throwing an exception? I don't understand.
Reply
#2
Use strict=False in the ConfigParser constructor as per the documentation.
Reply
#3
I know I can do it that way, thanks. I've got code that does what I want using strict=False. However, I'd like to figure out why I can't modify the dictionary passed to read(). Ideally, I'd like to keep the configuration file complete, just renaming options that are identical in the config file.
Reply
#4
This code apparently does what you want
from configparser import ConfigParser import itertools as itt confstr="""\ [Menu] MainMenuRandomizer=0 OptionsInfo=1 RandomizerQty=3 TextFont = Calibri TextSize = 22 TextFontStatus = Courier New TextSize = 11 TextColorNormal = 250,240,180 TextShadowColorNormal = 50,50,50 TextColorSelected = 255,255,255 TextShadowColorSelected = 50,50,50 TextColorEditing = 255,100,50 TextShadowColorEditing = 50,50,50 PositionX = 800 PositionY = 120 InfoPositionX = 480 InfoPositionY = 800 TitleColumnSize = 1000 MainItemColumnSize = 100 ItemColumnSize = 300 InfoColumnSize = 960 RowSpace = 0 RowsPerPage = 30 KeyEnable = 24 KeyUp = 200 KeyDown = 208 KeyLeft = 203 KeyRight = 205 KeyPageUp = 201 KeyPageDown = 209 KeyAdd = 78 KeySubtract = 74 KeySave = 28 KeyEditing = 156 StepValue =0.1 Decimals = 4 """ class MyDict(dict): def __init__(self, *args, **kwargs): self.c = itt.count() super().__init__(*args, **kwargs) def __setitem__(self, k, v): if k == 'textsize': n = next(self.c) k = k + str(n) print('setitem:', id(self), k, v) super().__setitem__(k, v) conf = ConfigParser(dict_type=MyDict, strict=False) conf.read_string(confstr) print(dict(conf['Menu']))
Reply
#5
You didn't call __init__() for OrderedDict. Not your problem here, but always a good idea to call super()__init__().

Notice that the error occurs in "configparser.py", not your program file, not in your special dictionary. Allowing or disallowing duplicate keys is an attribute of the configparser. The test for duplicates is performed using a set (elements_added) that is a local variable in the read_dict() method. Other than subclassing configparser and modifying that method the only way to allow duplicate keys in a section is to set "strict=False".
Reply
#6
Necro time. Been away from these forums (using two others for help, but this place is incredibly helpful as well. So, here I am back for help), but want to keep it in rotation.

This script was an aside of mine, and I haven't picked it back up since back in February. Having read the replies, it looks like I can't do what I was trying to do? I don't know, I'd have to dig up the old script somewhere on my backup drive.

But, I necro'd just to say thanks you two (sorry if it's frowned upon). If I pick the script up again, I'll refer back here.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Two loops behaving differently DavidTheGrockle 5 4,259 Dec-27-2020, 03:56 AM
Last Post: deanhystad
  using alternate names in configparser Skaperen 6 5,053 Oct-01-2020, 08:27 PM
Last Post: Skaperen
  if condition not behaving as expected EricBHK 10 6,523 May-16-2020, 03:41 PM
Last Post: EricBHK
Question configparser.NoSectionError (Python 3.7) alpho 2 8,658 Apr-08-2020, 10:26 PM
Last Post: micseydel
  Error in configparser fullstop 12 15,444 Dec-22-2019, 07:36 PM
Last Post: snippsat
  Help with ConfigParser EricALionsFan 3 4,511 Jul-27-2018, 08:46 PM
Last Post: buran
  ConfigParser.NoSectionError: No section: 'Default' degenaro 1 17,482 Jun-08-2018, 08:33 PM
Last Post: ljmetzger
  Requests not behaving... PythonLamer 1 6,032 Dec-01-2017, 06:45 PM
Last Post: nilamo
  No module named 'ConfigParser' Bani 13 69,568 May-22-2017, 08:02 PM
Last Post: Ofnuts

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020
This forum uses Lukasz Tkacz MyBB addons.
Forum use Krzysztof "Supryk" Supryczynski addons.