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 following code, I bind __read_ini to __read and override __read so it
can choose between __read_ini and __read_custom. But it seems that
__read_custom never gets called when I expect it to aways be called. I have
a feeling this has to do with me not entirely understanding magic atributes
and name mangling. Anyone have ideas?
greg
from ConfigParser import ConfigParser
class CustomConfigParser(ConfigParser):
def __init__(self, defaults=None):
ConfigParser.__init__(self, defaults)
#Replace the normal __read with my custom __read
#while keeping the normal one around if I need it
self.__read_ini = ConfigParser._ConfigParser__read
ConfigParser._ConfigParser__read = self.__read
def __read(self, fp, fpname):
#Eventually this will decide between __read_custom
#and __read_ini, etc.
self.__read_custom(fp, fpname)
def __read_custom(self, fp, fpname):
print "__read_custom" #This never gets printed.
cp = CustomConfigParser()
cp.read('config.ini')
print cp.sections() 3 2600
Greg Krohn schrieb: 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 following code, I bind __read_ini to __read and override __read so it can choose between __read_ini and __read_custom. But it seems that __read_custom never gets called when I expect it to aways be called. I have a feeling this has to do with me not entirely understanding magic atributes and name mangling. Anyone have ideas?
greg from ConfigParser import ConfigParser
class CustomConfigParser(ConfigParser): def __init__(self, defaults=None): ConfigParser.__init__(self, defaults)
#Replace the normal __read with my custom __read #while keeping the normal one around if I need it self.__read_ini = ConfigParser._ConfigParser__read ConfigParser._ConfigParser__read = self.__read
def __read(self, fp, fpname): #Eventually this will decide between __read_custom #and __read_ini, etc. self.__read_custom(fp, fpname)
def __read_custom(self, fp, fpname): print "__read_custom" #This never gets printed.
cp = CustomConfigParser() cp.read('config.ini') print cp.sections()
Hi Greg,
are you sure you have a file 'config.ini' in your current working
directory? ConfigParser is silently ignoring files which cannot be
opened, so that _read is not called!
BTW, you did not mention the Python version you are using. In Python 2.3
the method you overwrite is named '_read', not '__read' (so there is no
name mangling). This is one drawback of overwriting "private" methods,
when the author renames or removes them, your code is broken.
Anyway, your method of overwriting the method is a bit complicated . You
can do it much easier: just overwrite _read and call the original method
when appropriate:
class CustomConfigParser(ConfigParser):
def __init__(self, defaults=None):
ConfigParser.__init__(self, defaults)
def _read(self, fp, fpname):
if ... # your criteria for custom ini
then return self._read_custom(fp, fpname)
else return ConfigParser._read(self, fp, fpname)
def _read_custom(self, fp, fpname):
...
Michael
Greg Krohn schrieb: 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 following code, I bind __read_ini to __read and override __read so it can choose between __read_ini and __read_custom. But it seems that __read_custom never gets called when I expect it to aways be called. I have a feeling this has to do with me not entirely understanding magic atributes and name mangling. Anyone have ideas?
greg from ConfigParser import ConfigParser
class CustomConfigParser(ConfigParser): def __init__(self, defaults=None): ConfigParser.__init__(self, defaults)
#Replace the normal __read with my custom __read #while keeping the normal one around if I need it self.__read_ini = ConfigParser._ConfigParser__read ConfigParser._ConfigParser__read = self.__read
def __read(self, fp, fpname): #Eventually this will decide between __read_custom #and __read_ini, etc. self.__read_custom(fp, fpname)
def __read_custom(self, fp, fpname): print "__read_custom" #This never gets printed.
cp = CustomConfigParser() cp.read('config.ini') print cp.sections()
Hi Greg,
are you sure you have a file 'config.ini' in your current working
directory? ConfigParser is silently ignoring files which cannot be
opened, so that _read is not called!
BTW, you did not mention the Python version you are using. In Python 2.3
the method you overwrite is named '_read', not '__read' (so there is no
name mangling). This is one drawback of overwriting "private" methods,
when the author renames or removes them, your code is broken.
Anyway, your method of overwriting the method is a bit complicated . You
can do it much easier: just overwrite _read and call the original method
when appropriate:
class CustomConfigParser(ConfigParser):
def __init__(self, defaults=None):
ConfigParser.__init__(self, defaults)
def _read(self, fp, fpname):
if ... # your criteria for custom ini
then return self._read_custom(fp, fpname)
else return ConfigParser._read(self, fp, fpname)
def _read_custom(self, fp, fpname):
...
Michael
"Michael Amrhein" <mi*************@t-online.de> wrote in message
news:3F**************@t-online.de... Hi Greg, are you sure you have a file 'config.ini' in your current working directory? ConfigParser is silently ignoring files which cannot be opened, so that _read is not called!
I was getting a ParsingError that showed the line it was choking on, so I
know it was finding the file. BTW, you did not mention the Python version you are using. In Python 2.3 the method you overwrite is named '_read', not '__read' (so there is no name mangling). This is one drawback of overwriting "private" methods, when the author renames or removes them, your code is broken.
I _was_ using ActivePython 2.2 (2.3 isn't out yet). I downloaded the 2.3
regular distro last night because I read ConfigParser was cleaned up, but I
haven't had time to mess around with it much.
Anyway, your method of overwriting the method is a bit complicated . You can do it much easier: just overwrite _read and call the original method when appropriate:
class CustomConfigParser(ConfigParser): def __init__(self, defaults=None): ConfigParser.__init__(self, defaults) def _read(self, fp, fpname): if ... # your criteria for custom ini then return self._read_custom(fp, fpname) else return ConfigParser._read(self, fp, fpname)
def _read_custom(self, fp, fpname): ...
Michael
Yes, this is great! Everything is working as expected now. Originally I was
expecting code like yours to work in 2.2 and when it didn't I tried all
sorts of elaborate hacks. What a mess. Thank you very much for your reply.
greg This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
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",...
|
by: Roy H. Berger |
last post by:
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...
|
by: Robin Munn |
last post by:
I've been loving SQLObject. The ability to set up a database
connection and then completely *forget* about it and just manipulate
Python objects has been great. But I'm running into a problem, and...
|
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 &, [ and ] ?
Thanks ...
|
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,...
|
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...
|
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)...
|
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...
|
by: Phoe6 |
last post by:
Hi,
I have a configfile, in fact, I am providing a configfile in the
format:
Name: Foo
Author: Bar
Testcases:
tct123
|
by: Faith0G |
last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
|
by: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM).
In this session, we are pleased to welcome former...
|
by: ryjfgjl |
last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
by: aa123db |
last post by:
Variable and constants
Use var or let for variables and const fror constants.
Var foo ='bar';
Let foo ='bar';const baz ='bar';
Functions
function $name$ ($parameters$) {
}
...
|
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
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
by: Sonnysonu |
last post by:
This is the data of csv file
1 2 3
1 2 3
1 2 3
1 2 3
2 3
2 3
3
the lengths should be different i have to store the data by column-wise with in the specific length.
suppose the i have to...
|
by: Hystou |
last post by:
There are some requirements for setting up RAID:
1. The motherboard and BIOS support RAID configuration.
2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
| |