::-- limodou [2005-09-14 06:31:19]

1. Dict4Ini

This module is used to process ini format configuration file. It acts just like a dict, but you can also access it's sections and options with attribute syntax, just like x.test.

1.1. Why reinvent this module?

I used Config4Obj module for GoogleTalkBot software (confbot) to deal with configuration file. But I found its lacks on:

Above is only my opinions, so they may be not right.

So I decide to reinvent a new module to solve these lacks, I named it as Dict4Ini, it means you can access the config object just like a dict.

1.2. What's it features

1.3. Where can I download it?

1.4. What's new?

1.5. Examples

1.5.1. Example 1 Create a ini file

   1     import dict4ini
   2 
   3     x = dict4ini.DictIni('test.ini')
   4     x.common.name = 'limodou'
   5     x.common.bool = 1
   6     x.common.list = [3, 1.2, 'Hello', 'have spaces']
   7     x.save()

This example will save option to test.ini. As you can see, you needn't create section "common" at first, just think it's there, it's ok. The result of test.ini is:

[common] 
list = 3,1.2,Hello,"have spaces",
bool = 1
name = limodou

And you can see, once the value has special chars, just like ' ', ',', '\"', etc, the string will be quoted by double quoter. But "Hello" is a single word, and it has not the special chars, so it won't be quoted. If the value is number, it'll be just like number literal, but if the value is number string, it'll be quoted by double quoter.

In this time, the Dict4Ini support int, float, list/tuple, string, unicode data type, for others you should convert yourself.

1.5.2. Example 2 Open an existed ini file

   1     import dict4ini
   2 
   3     x = dict4ini.DictIni('test.ini')
   4     print x.common.bool
   5     print x.common.list
   6     print x.common.name

So it's easy. The result will be:

1 
[3, 1.2, 'Hello', 'have spaces']
limodou

The data is auto converted to its original type.

1.5.3. Example 3 Dealing default values

Many times, you may want to set default values of options, once it is not set in configuration file. Using Dict4Ini, you have many ways to do that:

1.5.3.1. Using dict's setdefault() method
   1 x = dict4ini.DictIni()
   2 x.test.setdefault('a', 'b')
   3 print x.test.a
1.5.3.2. Passing a dict to DictIni init method
   1 d = {'test':{'a':'b'}}
   2 x = dict4ini.DictIni(values=d)
   3 print x.test.a
1.5.3.3. Creating config object first, then assign a dict value
   1 x = dict4ini.DictIni()
   2 d = {'a':'b'}
   3 x.test = d
   4 print x.test.a

1.5.4. Example 4 Saving comments in ini file

   1     import dict4ini
   2     x = dict4ini.DictIni('test.ini')
   3 
   4     x.common._comment = 'This is a comment test.\nThis is the second line.'
   5     x.common.name = 'limodou'
   6     x.common.comment('name', 'Input your name')
   7     x.common.bool = 1
   8     x.common.comment('bool', 'Boot type')
   9     x.common.list = ['3', 'Hello', 'have spaces']
  10     x.common.comment('list', 'list testing')
  11     x.save()

You can save comments in configuration also. Adding comments to section, you should using x.section._comment = 'comments'. Comments could be multi lines. Or you could use more commonly method, x.comment(). Just like x.comment('common', 'comments'). Add comments to options, you can only using comment() method, just like x.common.comment('list', 'comments').

The result of the ini file will be:

# This is a comment test. 
# This is the second line.
[common]
# Boot type
bool = 1
# list testing
list = "3",Hello,"have spaces",
# Input your name
name = limodou

1.5.5. Example 5 Using unicode in ini file

1.5.5.1. Using default encoding
   1 #coding=utf-8
   2 import dict4ini
   3 x = dict4ini.DictIni('test.ini')
   4 x.common.name = u'中文名'
   5 x.save()

Note: You should specify the coding used in the .py file. In this case is utf-8. Then I assign x.common.name with a unicode string. If you don't specify the encoding in create instance of the DictIni, the Dict4Ini will auto find the default encoding in the system in turns of:

The result of the ini file will be:

[common] 
name = u"中文名"

You should notice the file encoding will be utf-8, and the name's value is like python unicode syntax. For easiness, it doesn't support using unicode in comments.

1.5.5.2. Specifying encoding name

You can also specify the encoding of ini file, just like:

   1 #coding=utf-8
   2 import dict4ini
   3 x = dict4ini.DictIni('test.ini', encoding='gbk')
   4 x.common.name = u'中文名'
   5 x.save()

It's easy to set an encoding of ini file.

1.5.6. Example 6 Using multi section

   1 import dict4ini
   2 x = dict4ini.DictIni('test.ini')
   3 x.common.settings.a = 1
   4 x.common.settings.b = ["3", "hello"]
   5 x.special.name = 'limodou'
   6 x.special.homepage = 'http://www.donews.net/limodou'
   7 x.save()

You don't need to care if subsection is created, you need to just use it.

The result of the ini file will be:

   [common/settings] 
    a = 1
    b = "3",hello,


    [special]
    homepage = http://www.donews.net/limodou
    name = limodou

1.5.7. Example 7 Getting ordered options

Sometimes we need to keep the order or the options according to the ini file, so how to get the ordered items?

   1 ini = dict4ini.DictIni('x.ini')
   2 for key, value in ini.ordereditems(ini):
   3    print key, value

This example is dealing with the first level section.

   1 ini = dict4ini.DictIni('x.ini')
   2 for key, value in ini.ordereditems(ini.autostring):
   3    print key, value

This example is dealing with certain section.

1.5.8. Examples waiting

1.6. FAQ

1.6.1. 1. Can I delete an option?

A: Yes. For example:

   1 import dict4ini
   2 x = dict4ini.DictIni('test.ini')
   3 del x.a
   4 x.save()

1.6.2. 2. How to use 'xxx.xxx' style option key?

A: Easy. Just using dict syntax, for example:

   1 x['common']['xxx.xxx'] = 'a'

or

   1 x.common['xxx.xxx'] = 'a'

1.6.3. 3. How to deal the key including section delimeter char, just like '/'

A: As you creating the DictIni instance, you can specify a "onelevel=True" parameter:

   1 x = dict4ini.DictIni('inifile', onelevel=True)

But it'll not support multi section again. So you can also defined another sectiondelimeter char different from '/', just like:

   1 x = dict4ini.DictIni('inifile', sectiondelimeter='@')

But every time you called dict4ini.DictIni() you may need including sectiondelimeter parameter.

1.7. Feedback

Write down your opinions, thanks

last edited 2006-01-04 03:19:03 by limodou