I want to set default values for a ConfigParser. So far, its job is
very small, so there is only one section heading, ['Main']. Reading the
docs, I see that in order to set default values in a ConfigParser, you
initialize it with a dictionary or defaults. However, I'm not quite
sure of the syntax to add the section headings in to the dictionary of
defaults. For now, I'm doing it like this:
default_values = {'username' : 'put username here',
'teamnumber': 'team number here',
'update_interva l' : 'update interval'}
self.INI = ConfigParser.Co nfigParser(defa ult_values)
self.INI.add_se ction('Main')
This works, but clearly won't last beyond the adding of a second
section. What is the correct way to initialize it with a full
dictionary of defaults, including section names?
Thanks. 3 11702
On 28 Feb 2006 17:05:32 -0800
"mwt" <mi*********@gm ail.com> wrote: I want to set default values for a ConfigParser. So far, its job is very small, so there is only one section heading, ['Main']. Reading the docs, I see that in order to set default values in a ConfigParser, you initialize it with a dictionary or defaults.
You know, I never noticed that, but there is another way.
I used a multi-line string constant with the same syntax
as the original file, and just fed that in as the first
source (this is particularly easy, so I think it must have
been intended). Slightly snipped version of my code for
this:
default_cfg = StringIO("""\
[VARIMAGE]
V1_COMPATIBILIT Y: ON
[SECURITY]
MAX_OPERATORS: 0
""")
# Look for varimage.cfg in 3 possible locations:
config = ConfigParser.Co nfigParser()
config.readfp(d efault_cfg)
config.read(['/etc/varimage.cfg',
os.path.expandv ars('${INSTANCE _HOME}/varimage.cfg'),
os.path.join(pk ghome, 'varimage.cfg') ])
--
Terry Hancock (ha*****@Anansi Spaceworks.com)
Anansi Spaceworks http://www.AnansiSpaceworks.com
Thanks, Terry. That's an interesting way to go about it.
mwt wrote: I want to set default values for a ConfigParser. So far, its job is very small, so there is only one section heading, ['Main']. Reading the docs, I see that in order to set default values in a ConfigParser, you initialize it with a dictionary or defaults. However, I'm not quite sure of the syntax to add the section headings in to the dictionary of defaults. For now, I'm doing it like this:
default_values = {'username' : 'put username here', 'teamnumber': 'team number here', 'update_interva l' : 'update interval'} self.INI = ConfigParser.Co nfigParser(defa ult_values) self.INI.add_se ction('Main')
This works, but clearly won't last beyond the adding of a second section. What is the correct way to initialize it with a full dictionary of defaults, including section names?
An alternative approach is to use `ConfigObj
<http://www.voidspace.o rg.uk/python/configobj.html> `_. It has two ways
of specifying default values.
The first way is to provide a configspec. This is a schema (that looks
very like a config file itself) that specifies the type (and paramater
bounds if you want) for each member. It can also include a default
value.
Simpler (although without the benefit of validation and type
conversion) is to use the ``merge`` method which is a recursive update.
(Although for config files that are a maximum of one section deep, the
dictionary method ``update`` will do the same job).
default_values = {'username' : 'put username here',
'teamnumber': 'team number here',
'update_interva l' : 'update interval'}
user_values = ConfigObj(filen ame)
cfg = ConfigObj(defau lt_values)
cfg.merge(user_ values)
Note that for a config file with only a few values in it, ConfigObj
doesn't force you to use a section if it's not needed. To put your
default values into a 'Main' section you would actually do :
default_values = { 'Main': {'username' : 'put username here',
'teamnumber': 'team number here',
'update_interva l' : 'update interval'}
}
#
user_values = ConfigObj(filen ame)
cfg = ConfigObj(defau lt_values)
cfg.merge(user_ values)
All the best,
Fuzzyman http://www.voidspace.org.uk/python/index.shtml
Thanks. 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 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...
|
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: S.Ramaswamy |
last post by:
I am trying unsuccessfully to set the order of options using the
set(section,option,value) method ( Python 2.2.2) and writing to a
file. But the options always appear in a random order. Before each
option I am writing a comment line using set(section,"#",value) - a
one line explanation for the option that follows - but the comments
get re-ordered randomly.
...
|
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: 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 All,
I am able to use urlib2 through proxy. I give proxy credentials and use
# Set the Proxy Address
proxy_ip = "10.0.1.1:80"
proxy_user = 'senthil_or'
proxy_password_orig='password'
|
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: Hamish Moffatt |
last post by:
SafeConfigParser is supposed to be safer than ConfigParser, but calling
set with a string value containing '%' generates exceptions when you
get() it back.
Python 2.5.1 (r251:54863, Apr 25 2007, 21:31:46)
on linux2
Type "help", "copyright", "credits" or "license" for more information.
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ImportError: No module named configparser
|
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: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
|
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: isladogs |
last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules.
He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms.
Adolph will...
|
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
| |