I'm not sure if this is a real problem or if I have been staring at code
too long. given this code
#!/usr/bin/python
from ConfigParser import *
configuration_file = "test.conf"
substitution = {"xyzzy":"maze"}
configuration = SafeConfigParser()
configuration.readfp(file(configuration_file))
list = configuration.items("core")
print list
list = configuration.items("core",0, substitution)
print list
---------------
and this configuration file
-----------
[core]
xyzzy=little bird
dwarf = greasy smoke %(xyzzy)s plugh
------------
why do I get the following results?
-------------
trial backup # python test.py
[('dwarf', 'greasy smoke little bird plugh'), ('xyzzy', 'little bird')]
[('dwarf', 'greasy smoke maze plugh'), ('xyzzy', 'maze')]
trial backup #
-------------
if you're having a hard time seeing it, before the substitution, xyzzy
is set to the value in the configuration file, afterwards, it is set to
the value of the substitution in the code. It seems to me that
substitutions should not affect any configuration file symbols of the
same name.
anyway to fix this problem without python diving?
---eric 3 1809
Sort of hard to explain, but if you put another:
list = configuration.items("core")
print list
at the end of the script, you will find that the original config hasn't
been changed.
It is a quirk of how the items() method is implemented using 'yield'
that means that
you see what you do.
In particular to use 'yield' it it necessary to create a temporary
dictionary which
contains the key/value pairs from that section of the config and then
overlay it
with the user supplied vars. Ie., the items() code has:
.. d = self._defaults.copy()
.. try:
.. d.update(self._sections[section])
.. except KeyError:
.. if section != DEFAULTSECT:
.. raise NoSectionError(section)
.. # Update with the entry specific variables
.. if vars:
.. d.update(vars)
See the last line, that will replace the value of 'xyzzy' with that
passed in
as argument to items().
To avoid this, you need to write something like:
.. list = []
.. for key in configuration.options("core"):
.. list.append((key,configuration.get("core",substitu tion))
.. print list
This cause me problems for a different reason, ie., that user vars keys
appear in what items() returns. I avoid using items() for this reason. gr*****@dscpl.com.au wrote: To avoid this, you need to write something like:
. list = [] . for key in configuration.options("core"): . list.append((key,configuration.get("core",substitu tion)) . print list
This cause me problems for a different reason, ie., that user vars keys appear in what items() returns. I avoid using items() for this reason.
it turns out, I originally discovered this problem through the get with
substitutions. It's too late to muck with it now but if you are really
interested I will generate a test case showing the failure or else prove
I was hallucinating.
My current workaround is to look for a %( in every value returned and if
so do a string substitution.
value = base.get(section,item, 1)
if value.find("%(") != -1:
value = value% self.interpolation_symbols
yes, it is as ugly as a roadkill toad but it gets the job done. I've
spent away too much time on this problem for this particular project. I
think we all know the feeling.
---eric
True, wasn't thinking. This will affect get() as well. My problem was a
slightly different problem.
In your case you would have got what you wanted if get()/items()
instead of being implemented as:
.. try:
.. value = d[option]
.. except KeyError:
.. raise NoOptionError(option, section)
Was implemented as:
.. try:
.. value = self._sections[section][option]
.. except KeyError:
.. raise NoOptionError(option, section)
That is, get the raw value from the original dictionary for the
section. That way you avoid picking up a value from the user
supplied vars. It does also avoid picking a key which has come
through from the default section, but that could easily be
accomodated if that was perceived to be expected behaviour
by having the except clause fall through to looking in the
default section. Similarly if seen that getting it from vars is
okay as well, fall back onto that as file step. All depends on
what the perceived overridding priority is.
At the moment vars overrides section which overrides default
and the document sort of does say that that is what one should
expect for vars at least:
.. Additional substitutions may be provided using the
.. `vars' argument, which must be a dictionary whose contents overrides
.. any pre-existing defaults.
Probably not what I would have expected either. I would have expected
vars to be available for substitution but not for option selection in
the
first place.
It is too late on a Friday, so I may be hallucinating now. :-) This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
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...
|
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: Matthew Barnes |
last post by:
I'm considering submitting a patch for Python 2.4 to allow environment
variable expansion in ConfigParser files. The use cases for this
should be obvious. I'd like to be able to specify something...
|
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: ryjfgjl |
last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
|
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...
|
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...
| |