Differences between revisions 1 and 9 (spanning 8 versions)
Revision 1 as of 2005-09-14 06:31:19
Size: 3739
Editor: limodou
Comment:
Revision 9 as of 2005-09-14 08:44:45
Size: 5938
Editor: limodou
Comment:
Deletions are marked like this. Additions are marked like this.
Line 12: Line 12:
 * You must create section first, so you can access its options. So if you didn't create x['name']={} section, so you cann't do x['name']['o'] = 3
 * The option's data can be saved as string format, but as read out again, Config4Obj cann't convert it to their original value type, so you must conver it yourself
 * You must create section first, then you can access its options. So if you didn't create x['name']={} section, so you cann't do x['name']['o'] = 3
 * The option's data can be saved as string format, but as read out again, Config4Obj cann't convert it to their original value type, so you must conver it yourself. I didn't tried validate module ships with Config4Obj.
Line 16: Line 16:
*Above is my opinion, so may be not right. * '''Above is only my opinions, so they may be not right. '''
Line 18: Line 18:
So I decide to _reinvent_ a new module to solve there lacks, I named it as Dict4Ini, it means you can see the object is just like a dict. 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.
Line 24: Line 24:
 * you also can access options according to attr syntax, just like x.name.o = 1, x.name.keys(), x.name.values, etc. So the name must be Identifier or single word.  * you also can access options according to attr syntax, just like x.name.o = 1, x.name.keys(), x.name.values(), etc. So the name must be Identifier or single word.
Line 27: Line 27:
 * It's a litter module, just for my mind, so if you like, you should try it, but if you don't like, just skip it, that's ok  * It's a little module, just for my mind, so if you like, you could try it, but if you don't like, just skip it, that's ok

== Where can I download it? ==

 * Visit the http://wiki.woodpecker.org.cn/moin/Dict4Ini site
 * Download it from here attachment:dict4ini.py
Line 31: Line 36:
=== Examples 1 Create a ini file === === Example 1 Create a ini file ===
Line 68: Line 73:
So you also can see, the order of options don't be holding. In many cases, it's not very seriously. The data is auto converted to its original type. Then you also can see, the order of options don't be holding. In many cases, it's not very seriously.
Line 70: Line 75:
=== Example 3 Default value === === Example 3 Dealing default values ===
Line 72: Line 77:
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: 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:
Line 97: Line 102:
=== Example 4 Saving comments in ini file ===

{{{#!python
    import dict4ini
    x = dict4ini.DictIni('test.ini')

    x.common._comment = 'This is a comment test.\nThis is the second line.'
    x.common.name = 'limodou'
    x.common.comment('name', 'Input your name')
    x.common.bool = 1
    x.common.comment('bool', 'Boot type')
    x.common.list = ['3', 'Hello', 'have spaces']
    x.common.comment('list', 'list testing')
    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}}}

=== Example 5 Using unicode in ini file ===
==== Using default encoding ====
{{{#!python
#coding=utf-8
import dict4ini
x = dict4ini.DictIni('test.ini')
x.common.name = u'中文名'
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.

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.

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

{{{#!python
#coding=utf-8
import dict4ini
x = dict4ini.DictIni('test.ini', encoding='gbk')
x.common.name = u'中文名'
x.save()}}}

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

::-- limodou [DateTime(2005-09-14T06:31:19Z)] TableOfContents

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:

  • Only can access options as x['name']['o'], but not as x.name.o
  • You must create section first, then you can access its options. So if you didn't create x['name']={} section, so you cann't do x['name']['o'] = 3
  • The option's data can be saved as string format, but as read out again, Config4Obj cann't convert it to their original value type, so you must conver it yourself. I didn't tried validate module ships with Config4Obj.

  • Didn't support unicode

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

  • as simple as others
  • you can access options according to dict syntax, just like x['name']['o'] = 1, x['name'].keys(), x['name'].values(), etc.
  • you also can access options according to attr syntax, just like x.name.o = 1, x.name.keys(), x.name.values(), etc. So the name must be Identifier or single word.
  • you can save comments in it(but this feature is not tested so much)
  • support multi level section, subsection name will just like: [firsub/secsub]
  • It's a little module, just for my mind, so if you like, you could try it, but if you don't like, just skip it, that's ok

1.3. Where can I download it?

1.4. Examples

1.4.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, '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,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 number, list, string, unicode data type, for others you should convert yourself.

1.4.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:

Dict4Ini (last edited 2009-12-25 07:13:47 by localhost)