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 CustomConfigPar ser(ConfigParse r):
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._C onfigParser__re ad
ConfigParser._C onfigParser__re ad = self.__read
def __read(self, fp, fpname):
#Eventually this will decide between __read_custom
#and __read_ini, etc.
self.__read_cus tom(fp, fpname)
def __read_custom(s elf, fp, fpname):
print "__read_cus tom" #This never gets printed.
cp = CustomConfigPar ser()
cp.read('config .ini')
print cp.sections() 3 2624
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 CustomConfigPar ser(ConfigParse r): 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._C onfigParser__re ad ConfigParser._C onfigParser__re ad = self.__read
def __read(self, fp, fpname): #Eventually this will decide between __read_custom #and __read_ini, etc. self.__read_cus tom(fp, fpname)
def __read_custom(s elf, fp, fpname): print "__read_cus tom" #This never gets printed.
cp = CustomConfigPar ser() 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 CustomConfigPar ser(ConfigParse r):
def __init__(self, defaults=None):
ConfigParser.__ init__(self, defaults)
def _read(self, fp, fpname):
if ... # your criteria for custom ini
then return self._read_cust om(fp, fpname)
else return ConfigParser._r ead(self, fp, fpname)
def _read_custom(se lf, 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 CustomConfigPar ser(ConfigParse r): 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._C onfigParser__re ad ConfigParser._C onfigParser__re ad = self.__read
def __read(self, fp, fpname): #Eventually this will decide between __read_custom #and __read_ini, etc. self.__read_cus tom(fp, fpname)
def __read_custom(s elf, fp, fpname): print "__read_cus tom" #This never gets printed.
cp = CustomConfigPar ser() 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 CustomConfigPar ser(ConfigParse r):
def __init__(self, defaults=None):
ConfigParser.__ init__(self, defaults)
def _read(self, fp, fpname):
if ... # your criteria for custom ini
then return self._read_cust om(fp, fpname)
else return ConfigParser._r ead(self, fp, fpname)
def _read_custom(se lf, 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 CustomConfigPar ser(ConfigParse r): def __init__(self, defaults=None): ConfigParser.__ init__(self, defaults) def _read(self, fp, fpname): if ... # your criteria for custom ini then return self._read_cust om(fp, fpname) else return ConfigParser._r ead(self, fp, fpname)
def _read_custom(se lf, 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", "credits" or "license" for more information.
>>> from ConfigParser import ConfigParser
>>> cp = ConfigParser()
|
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 *
class MyConfigParser(ConfigParser):
def __init__(self, defaults=None):
ConfigParser.__init__(self, defaults)
|
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 I'd
like some advice.
I'm writing an app using SQLite as the database backend, and
interfacing to it via SQLObject. I have all my SQL table definitions
(classes inheriting from SQLObject) in one module, and for
simplicity's sake I'm using a...
|
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 Manlio Perillo
|
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, I'd rather
use a syntax that ConfigParser can't handle.
I know I can maintain two separate configuration files, and if I have
to I will, but I'd rather avoid that, if possible, and a solution
that suits my purposes is quite straightforward. I...
| |
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 ConfigParser
def whatzit(thingname, thing):
print thingname, "value:", thing
print thingname, "length:", len(thing)
|
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)
config.add_section('test')
config.set('test', 'haha', 'hehe')
|
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
ConfigParser.read('non-existent-filename') returns in 2.4.x
One user with 2.3.x reported an error stemming from my use of
len(cfgObject.read('potentially-non-existent-filename'))
|
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: 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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look !
Part I. Meaning of...
|
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it.
First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
| |
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it.
Here is my compilation command:
g++-12 -std=c++20 -Wnarrowing bit_field.cpp
Here is the code in...
|
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own....
Now, this would greatly impact the work of software developers. The idea...
|
by: TSSRALBI |
last post by:
Hello
I'm a network technician in training and I need your help.
I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs.
The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols.
I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
| |
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...
| |