Python Forum
case transparent names in configpaser
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
case transparent names in configpaser
#1
is there a way to customize configparser to disable case insensitive aspect of INI files. i only need case transparency where sections and options i add retain the same case i added them with so that the re-created file has them. it turns out that writing the INI back out to a file changes all sections and all options to lower case even if i change nothing.
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
You can override optionxform. There's a section in the docs about doing this.

Quote:ConfigParser.optionxform(option)

This method transforms option names on every read, get, or set operation. The default converts the name to lowercase. This also means that when a configuration file gets written, all keys will be lowercase. Override this method if that’s unsuitable. For example:

>>> config = """
... [Section1]
... Key = Value
...
... [Section2]
... AnotherKey = Value
... """
>>> typical = configparser.ConfigParser()
>>> typical.read_string(config)
>>> list(typical['Section1'].keys())
['key']
>>> list(typical['Section2'].keys())
['anotherkey']
>>> custom = configparser.RawConfigParser()
>>> custom.optionxform = lambda option: option
>>> custom.read_string(config)
>>> list(custom['Section1'].keys())
['Key']
>>> list(custom['Section2'].keys())
['AnotherKey']
Reply
#3
can you be more explicit in pointing to the docs? i don't see it. or maybe the PDF doc page number.
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
There doesn't seem to be an id tag for the section I quoted in the configparser docs. Search for "ConfigParser.optionxform(option)" on that page. (or any of the other places that "optionx" is mentioned).
Reply
#5
i think this was over explained. it simply is:

the attribute of the configparser object named "optionxform" is where the configparser code gets a reference to the function it calls to change option names and section names to lower case. by replacing this reference, you can substitute your own function to make whatever change you want to have made (or none at all). your function should be idempotent as names could be passed through many times.

so, for my case i could just do myparser.optionxform = str.
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
  Display transparent image as overlay jds8086 0 1,239 Apr-17-2022, 11:43 AM
Last Post: jds8086
  Switch case or match case? Frankduc 9 4,498 Jan-20-2022, 01:56 PM
Last Post: Frankduc

Forum Jump:

User Panel Messages

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