Python Forum
opinion: configparser should have been better
Thread Rating:
  • 1 Vote(s) - 1 Average
  • 1
  • 2
  • 3
  • 4
  • 5
opinion: configparser should have been better
#1
configparser would have been easier to work with if it had used the dictionary API. i'm not saying not to do what it does, now, but to also do the dictionary API. that is, you would be able to get an option by doing conf[secname][optname] instead of conf.get(secname,optname). that might not seem like much, but you could do if optname in conf[secname]: instead of if conf.has_option(secname,optname):. would that have been so hard to do?

why not abstract that way?
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply
#2
It does
import configparser

parser = configparser.ConfigParser()
parser.read_dict({'section1': {'key1': 'value1',
                               'key2': 'value2',
                                'key3': 'value3'},
                  'section2': {'keyA': 'valueA',
                                'keyB': 'valueB',
                                'keyC': 'valueC'},
                  'section3': {'foo': 'x',
                                'bar': 'y',
                                'baz': 'z'}})
print(parser.sections())
print([option for option in parser['section3']])
print(parser['section3']['foo'])
Output:
['section1', 'section2', 'section3'] ['foo', 'bar', 'baz'] x
but like with dict it's better to use .get() to avoid KeyError and providing default value
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#3
i don't need .get or what it provides. modeling this a a dict of some name dicts would have worked better. then i could work with a section by itself as a dict reference. configparser has no means to just reference a section as an object.
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply
#4
you wanted to do conf[secname][optname] and I showed you can.
Also, you can assign section to a name and work with that object.

import configparser
 
parser = configparser.ConfigParser()
parser.read_dict({'section1': {'key1': 'value1',
                               'key2': 'value2',
                                'key3': 'value3'},
                  'section2': {'keyA': 'valueA',
                                'keyB': 'valueB',
                                'keyC': 'valueC'},
                  'section3': {'foo': 'x',
                                'bar': 'y',
                                'baz': 'z'}})

spam = parser['section3']
print(type(spam))
print(spam['bar'])
print(parser['section3']['foo'])
output
Output:
<class 'configparser.SectionProxy'> y x
really don't understand what your problem in this case is
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#5
maybe your code is cheating by using .read_dict. show me how by reading from a file, updating an option like and writing it back.
parser['thissection']['thisoption'] = 'thisvalue'
print('thissection now has:',repr(parser['thissection']))
print('these sections exist:',' '.join(k for k in parser['thissection'].keys()))
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply
#6
(Oct-01-2020, 10:58 PM)Skaperen Wrote: maybe your code is cheating by using .read_dict.
It doesn't matter how you populate the configparser.ConfigParser object

create example.ini
Output:
[section1] key1 = value1 key2 = value2 key3 = value3 [section2] keya = valueA keyb = valueB keyc = valueC [section3] foo = x bar = y baz = z
then

import configparser
parser = configparser.ConfigParser()
parser.read('example.ini')
spam = parser['section3']
print(type(spam))
print(spam['bar'])
print(parser['section3']['foo'])
parser['section3']['eggs'] = 'thisvalue'
print('these options exist:',' '.join(k for k in parser['section3'].keys()))
print(parser['section3']['eggs'])
Output:
<class 'configparser.SectionProxy'> y x these options exist: foo bar baz eggs thisvalue
I don't know why you didn't try that to test for yourself before posting
And please, consult the documentation if you are not sure about something - all this is in the examples in the docs at the top under Quick start.
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#7
that's still not behaving like 2 layers of dict. all it gives you is access to option values that way. the 2 layers should be splitable so one can do:
favsect = parser[favsectname]
favopts = favsect.keys()
print(f'favorite has options: {" ".join(favopts)}')
features for INI files, such as applying parser.optionxform() to the names, can still be done while implementing as a 2 layers of dict. my library design style is to make library usage simpler even if it means adding more to that library implementation. in this case it would need a class for a section (the inner dictionary) and a class for the INI object (the outer dictionary). that way you can get a reference to just one section at a time and it operates like a dictionary.
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply
#8
Sorry, but I will not continue to participate in this absurd discussion where you just rant, without even trying to try for yourself before posting. And you don't even read what I post. This is my last post in this thread.


I already show you you that you get separate class for section (inner dictionary) - configparser.SectionProxy. The object for the outer dict is the ConfigParser object itself. Below is a code that show it again as well as the functionality you claim (in your last post) that does not exists - configparser.SectionProxy has keys() method and it will return KeysView object


import configparser
parser = configparser.ConfigParser()
parser.read('example.ini')
spam = parser['section3'] # this is section3 object
print(type(spam))
print(spam['bar'])
print(parser['section3']['foo'])
parser['section3']['eggs'] = 'thisvalue'
print('these options exist:',' '.join(k for k in parser['section3'].keys()))
print(parser['section3']['eggs'])
print(spam.keys()) 
print(list(spam.keys()))
Output:
<class 'configparser.SectionProxy'> y x these options exist: foo bar baz eggs thisvalue KeysView(<Section: section3>) ['foo', 'bar', 'baz', 'eggs']
Now, feel free to develop whatever library/API you like and be happy.
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#9
when posts show code that is more complicated i don't make an effort to understand it because i don't want to go more complicated. and this thread was not intended for code help as i already did my little project (a command to add or delete profiles in Firefox on Linux/Posix). i'm just saying, in a way, that "Mapping Protocol Access" should not have been an add-on; it should have been the way it was done from the beginning, along with customizing like the .optionxform attribute. a dict already has ways to do defaults like .get(). i'm sure that if configparser had not existed and i implemented it some other way, i would have been accused of pythonic heresy.
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Upgrade to python 3 opinion hokie1999 5 2,762 May-11-2021, 06:42 PM
Last Post: hokie1999
  what is your opinion of this syntactic sugar idea? Skaperen 8 4,352 Nov-28-2018, 03:15 AM
Last Post: Skaperen
  Guido van Rossum does not care about the opinion of the community. Kirill_Dubovitskiy 12 7,963 Sep-14-2018, 06:33 PM
Last Post: micseydel
  [split] My opinion about OS choice for programming wavic 12 11,854 Oct-07-2016, 10:08 PM
Last Post: wavic

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020