Hello.
I've been using the ConfigParser module, which is a great module, gets the job done, but when it creates INI files its hard to read them. Heres what I mean, currently, when written to, the INI files look something like this: - [a]
-
something = somethingelse
-
[c]
-
5 = True
-
1 = false
-
3 = False
-
2 = True
-
4 = True
-
[b]
-
d = 1
-
a = 3
-
c = 6
-
b = 2
I'd like to modify the module to make them look like this: - [a]
-
something = somethingelse
-
[b]
-
a = 3
-
b = 2
-
c = 6
-
d = 1
-
[c]
-
1 = false
-
2 = True
-
3 = False
-
4 = True
-
5 = True
-
See what I mean? Anyone have any ideas on how I can accomplish this? thanks.
9 2182 bartonc 6,596
Recognized Expert Expert
Hello.
I've been using the ConfigParser module, which is a great module, gets the job done, but when it creates INI files its hard to read them. Heres what I mean, currently, when written to, the INI files look something like this: - [a]
-
something = somethingelse
-
[c]
-
5 = True
-
1 = false
-
3 = False
-
2 = True
-
4 = True
-
[b]
-
d = 1
-
a = 3
-
c = 6
-
b = 2
I'd like to modify the module to make them look like this: - [a]
-
something = somethingelse
-
[b]
-
a = 3
-
b = 2
-
c = 6
-
d = 1
-
[c]
-
1 = false
-
2 = True
-
3 = False
-
4 = True
-
5 = True
-
See what I mean? Anyone have any ideas on how I can accomplish this? thanks.
Cool... Makes me wish I was using CP. Here's the idea:
Derive from CP: - class MyConfigParser(ConfigParser):
-
# and override the write method
-
def write(self, fp):
-
# and sort the dictionary keys before calling fp.write()
.
Sounds like fun. If you need help, we're here.
okay, sorting the sections is easy, thats just a list. but i've never sorted a dictionary before. how would i sort the keys? - for (key, value) in self._sections[section].items():
bartonc 6,596
Recognized Expert Expert
okay, sorting the sections is easy, thats just a list. but i've never sorted a dictionary before. how would i sort the keys? - for (key, value) in self._sections[section].items():
I know that there is a better way, but at the moment this works:
Make a list from the dict.keys(), sort it, iterate over it:
>>> d = dict(a=1,b=2,c= 3)
>>> d
{'a': 1, 'c': 3, 'b': 2}
>>> l = d.keys()
>>> l
['a', 'c', 'b']
>>> l.sort()
>>> l
['a', 'b', 'c']
>>> for key in l:
... print d[key]
...
1
2
3
>>>
bartonc 6,596
Recognized Expert Expert
I know that there is a better way, but at the moment this works:
Make a list from the dict.keys(), sort it, iterate over it:
>>> d = dict(a=1,b=2,c= 3)
>>> d
{'a': 1, 'c': 3, 'b': 2}
>>> l = d.keys()
>>> l
['a', 'c', 'b']
>>> l.sort()
>>> l
['a', 'b', 'c']
>>> for key in l:
... print d[key]
...
1
2
3
>>>
-
for key in l:
-
value = self._sections[section][key]
-
# ...
Okay, this is what I came up with. - def write(self, fp):
-
"""Write an .ini-format representation of the configuration state."""
-
if self._defaults:
-
fp.write("[%s]\n" % DEFAULTSECT)
-
for (key, value) in self._defaults.items():
-
fp.write("%s = %s\n" % (key, str(value).replace('\n', '\n\t')))
-
fp.write("\n")
-
sections = sorted(self._sections)
-
for section in sections:
-
fp.write("[%s]\n" % section)
-
d = self._sections[section].items()
-
l = sorted(d.keys())
-
for key in l:
-
value = self._sections[section][key]
-
if key != "__name__":
-
fp.write("%s = %s\n" %
-
(key, str(value).replace('\n', '\n\t')))
-
fp.write("\n")
But it generates this error:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "C:\Python25\li b\ConfigParser2 .py", line 372, in write
l = sorted(d.keys() )
AttributeError: 'list' object has no attribute 'keys'
bartonc 6,596
Recognized Expert Expert
Okay, this is what I came up with. - def write(self, fp):
-
"""Write an .ini-format representation of the configuration state."""
-
if self._defaults:
-
fp.write("[%s]\n" % DEFAULTSECT)
-
for (key, value) in self._defaults.items():
-
fp.write("%s = %s\n" % (key, str(value).replace('\n', '\n\t')))
-
fp.write("\n")
-
sections = sorted(self._sections)
-
for section in sections:
-
fp.write("[%s]\n" % section)
-
d = self._sections[section].items()
-
l = sorted(d.keys())
-
for key in l:
-
value = self._sections[section][key]
-
if key != "__name__":
-
fp.write("%s = %s\n" %
-
(key, str(value).replace('\n', '\n\t')))
-
fp.write("\n")
But it generates this error:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "C:\Python25\li b\ConfigParser2 .py", line 372, in write
l = sorted(d.keys() )
AttributeError: 'list' object has no attribute 'keys'
I'll just do a little mark up, so you'll understand what you've written: - def write(self, fp):
-
"""Write an .ini-format representation of the configuration state."""
-
if self._defaults:
-
fp.write("[%s]\n" % DEFAULTSECT)
-
for (key, value) in self._defaults.items():
-
fp.write("%s = %s\n" % (key, str(value).replace('\n', '\n\t')))
-
fp.write("\n")
-
sections = sorted(self._sections)
-
for section in sections:
-
fp.write("[%s]\n" % section)
-
# dict.items returns a list of (key, value) tuples
-
### d = self._sections[section].items()
-
# if you want you can copy the section
-
d = self._sections[section]
-
# instead of items(), we're using keys() and d[key] for the pair
-
l = sorted(d.keys())
-
for key in l:
-
# then use the copy here too
-
value = d[key]
-
#### value = self._sections[section][key]
-
if key != "__name__":
-
fp.write("%s = %s\n" %
-
(key, str(value).replace('\n', '\n\t')))
-
fp.write("\n")
Looks great otherwise.
bartonc 6,596
Recognized Expert Expert
I'll just do a little mark up, so you'll understand what you've written: - def write(self, fp):
-
"""Write an .ini-format representation of the configuration state."""
-
if self._defaults:
-
fp.write("[%s]\n" % DEFAULTSECT)
-
for (key, value) in self._defaults.items():
-
fp.write("%s = %s\n" % (key, str(value).replace('\n', '\n\t')))
-
fp.write("\n")
-
sections = sorted(self._sections)
-
for section in sections:
-
fp.write("[%s]\n" % section)
-
# dict.items returns a list of (key, value) tuples
-
### d = self._sections[section].items()
-
# if you want you can copy the section
-
d = self._sections[section]
-
# instead of items(), we're using keys() and d[key] for the pair
-
l = sorted(d.keys())
-
for key in l:
-
# then use the copy here too
-
value = d[key]
-
#### value = self._sections[section][key]
-
if key != "__name__":
-
fp.write("%s = %s\n" %
-
(key, str(value).replace('\n', '\n\t')))
-
fp.write("\n")
Looks great otherwise.
Looking at it, the list returned by items() may actually be sortable: you could try - def write(self, fp):
-
"""Write an .ini-format representation of the configuration state."""
-
if self._defaults:
-
fp.write("[%s]\n" % DEFAULTSECT)
-
for (key, value) in self._defaults.items():
-
fp.write("%s = %s\n" % (key, str(value).replace('\n', '\n\t')))
-
fp.write("\n")
-
sections = sorted(self._sections)
-
for section in sections:
-
fp.write("[%s]\n" % section)
-
# dict.items returns a list of (key, value) tuples
-
sectList = self._sections[section].items()
-
sectList.sort()
-
# or your sorted() trick (which is what I was trying to think of earlier)
-
for key, value in sectList:
-
if key != "__name__":
-
fp.write("%s = %s\n" %
-
(key, str(value).replace('\n', '\n\t')))
-
fp.write("\n")
Thanks for that, Works great.
bartonc 6,596
Recognized Expert Expert
Thanks for that, Works great.
You are quite welcome. Any time, really.
Sign in to post your reply or Sign up for a free account.
Similar topics |
by: Creative Acceleration |
last post by:
Hi all,
I need to convert the Query Strings into Clean URLs, Found some articles on
PHP and Apache server.. How do we it them with ASP ??
Thanks
Kart
|
by: dmiller23462 |
last post by:
Somebody take a look and give me any suggestions? My brain is nuked...
Here's my deal....I have online submission forms on my intranet at
work here....I am appending to an Access DB with the input from all
HTML fields and then querying aforementioned DB with different
variables (search by name, wave, reason, etc). The output that I'm
getting (SELECT * 'cause I need all of the data included in the
search) I would like to display in a nice...
|
by: Nick Gilbert |
last post by:
Hi,
In my VS.NET 2005, if I choose Build Clean Solution, the BIN folder is
not touched. Shouldn't it delete all the dll and pdb files in that
folder first? Instead, I'm finding I have to do it manually.
Also, when I change from Debug to Release, all the pdb files remain in
the bin folder. Shouldn't these be deleted automatically?
Please let me know what the "Clean Solution" is supposed to do (if it's
|
by: Ulysse |
last post by:
Hello,
I need to clean the string like this :
string =
"""
bonne mentalité mec!:) \n <br>bon pour
info moi je suis un serial posteur arceleur dictateur ^^*
\n <br>mais pour avoir des resultats probant il
faut pas faire les mariolles, comme le "fondateur" de bvs
|
by: Koliber (js) |
last post by:
sorry for my not perfect english
i am really f&*ckin angry
in this common pattern about dispose:
//////////////////////////////////////////////////////////
Public class MyClass:IDisposable
| |
by: Bob Altman |
last post by:
Hi,
My VB project (VS 2005) has a post-build event that runs a program that
creates an executable file in the target directory (that is, the same folder
that contains the normal build results). However, since VS (or, more
specifically, MSBuild) doesn't know about this new executable, it isn't
deleted when I select Build->Clean Solution. How do I tell my VB project
about this file so that it gets deleted when I "clean" the build...
|
by: jcor |
last post by:
Hi, I'm trying the "make distclean" comand. but I allways get this error:
if I use the "make clean" I get the same error.
Can someone teach me to use this commands, please?
Thanks a lot,
João
|
by: plenty900 |
last post by:
Hello,
I'm trying to build a large project, and I've found that
when something doesn't compile properly, and I attempt
to re-build it, Visual C++ Express doesn't make a new
attempt. So then I try to clean the solution, but
this has literally no effect because when I try to
build after that, again no attempt to made to build.
Can someone tell me to really to clean the solution,
|
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: 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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth.
The Art of Business Website Design
Your website is...
| |
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: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert into image.
Globals.ThisAddIn.Application.ActiveDocument.Select();...
|
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: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |