473,396 Members | 1,722 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Subclassing ConfigParser question

If I want to subclass ConfigParser and changed the optionxform method
to not return things in lower case wouldn't I just need the following
code in my subclasss module?

from ConfigParser import *

class MyConfigParser(ConfigParser):
def __init__(self, defaults=None):
ConfigParser.__init__(self, defaults)

def optionxform (self, optionstr):
return optionstr

Or do I need to re-implement all of the methods of ConfigParser?
Sorry -- haven't done much subclassing with python.

Thanks in advance.

Roy
ro*****@att.net
Jul 18 '05 #1
2 4278
Roy H. Berger wrote:
If I want to subclass ConfigParser and changed the optionxform method
to not return things in lower case wouldn't I just need the following
code in my subclasss module?

from ConfigParser import *

class MyConfigParser(ConfigParser):
def __init__(self, defaults=None):
ConfigParser.__init__(self, defaults)

def optionxform (self, optionstr):
return optionstr

Or do I need to re-implement all of the methods of ConfigParser?
Sorry -- haven't done much subclassing with python.


You need to reimplement only methods with changed behaviour - that's the
whole idea of inheritance. In the above example even the constructor is
superfluous:

import ConfigParser # import * is *bad*

class MyConfigParser(ConfigParser.ConfigParser):
def optionxform(self, optionstr):
return optionstr

would suffice. There is of course no guarantee that other methods of
ConfigParser make assumptions about optionxform() that your modified method
does not hold. These you will have to detect with a properly designed test
suite.

Peter
Jul 18 '05 #2

"Roy H. Berger" <ro*****@att.net> wrote in message
news:b6***********************@posting.google.com. ..
If I want to subclass ConfigParser and changed the optionxform method
to not return things in lower case wouldn't I just need the following
code in my subclasss module?

from ConfigParser import *

class MyConfigParser(ConfigParser):
def __init__(self, defaults=None):
ConfigParser.__init__(self, defaults)

def optionxform (self, optionstr):
return optionstr

Or do I need to re-implement all of the methods of ConfigParser?
Sorry -- haven't done much subclassing with python.

Thanks in advance.

Roy
ro*****@att.net


Can't you just write:

cp = ConfigParser.ConfigParser()
cp.optionxform = str

to accomplish the same thing? That seems to be what the documentation
implies.
Jul 18 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: Greg Krohn | last post by:
I'm trying to subclass ConfigParser so I can use a custom __read method (the custom format doesn't conform to RFC 822) when needed. Needless to say, it's not working as expected. In the...
1
by: Martin Maney | last post by:
Summary: with batteries like these, get a longer extension cord? <transcript from interactive session> Python 2.2.1 (#1, Sep 7 2002, 14:34:30) on linux2 Type "help", "copyright",...
11
by: Manlio Perillo | last post by:
Regards. Since sections in CongiParser files are delimited by , why there is not an escape (and unescape) function for escaping &, characters to &amp;, &lbrack; and &rbrack; ? Thanks ...
2
by: rzed | last post by:
I am working with PythonCard in one of my apps. For its purposes, it uses an .ini file that is passed to ConfigParser. For my app, I also need configuration information, but for various reasons,...
10
by: Terry Carroll | last post by:
It looks like ConfigParser will accept a list to be writing to the *.ini file; but when reading it back in, it treats it as a string. Example: ############################### import...
1
by: pipehappy | last post by:
Hello everyone: I came across the module ConfigParser and can use it correctly. import ConfigParser fp = open('test.cfg','w+') config = ConfigParser.ConfigParser() config.readfp(fp)...
4
by: Danil Dotsenko | last post by:
Wrote a little "user-friedly" wrapper for ConfigParser for a KDE's SuperKaramba widget. (http://www.kde-look.org/content/show.php?content=32185) I was using 2.4.x python docs as reference and...
3
by: tony.ha | last post by:
Hello I use ConfigParser as show below to read a config.txt file; from ConfigParser import ConfigParser config = ConfigParser() config.read('config.txt') items = config.items('FV') for item...
3
by: jamitwidme | last post by:
Hello. I am a novice programmer and have a question I have a configuration file(configuration.cfg) I read this from reading.py using ConfigParser When I use ConfigParser.get() function, it...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.