Differences between revisions 16 and 21 (spanning 5 versions)
Revision 16 as of 2005-10-18 10:44:33
Size: 7317
Editor: cmkl
Comment:
Revision 21 as of 2006-01-04 03:11:46
Size: 8669
Editor: limodou
Comment:
Deletions are marked like this. Additions are marked like this.
Line 29: Line 29:
 * can convert to dict '''new'''
Line 35: Line 36:
 * Dict4Ini also ships with NewEdit, you can also download NewEdit, and get it from modules/dict4ini.py, that's the same.
Line 37: Line 39:
 * 2005/01/04 Version 0.5
  * 2006/01/04
   * Add ordereditems() method, so you can get the items according the ini file definition
  * 2005/12/30
   * Support onelevel parameter, you can set it True, than only one level can be used, so that the key can include section delimeter char in it.
   * Support sectiondelimeter parmeter, you can set the section delimeter to which you want
 * 2005/12/12 Version 0.4
  * Fixed a bug about "\" in option's value, thanks to Andreas Kaiser
 * 2005/12/09 Version 0.3
  * Adding dict() method, then you can change the DictIni object to dict type, so you can really use it as a dict alternative
Line 198: Line 209:
=== Examples 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?

{{{#!python
ini = dict4ini.DictIni('x.ini')
for key, value in ini.ordereditems(ini):
   print key, value
}}}
This example is dealing with the first level section.

{{{#!python
ini = dict4ini.DictIni('x.ini')
for key, value in ini.ordereditems(ini.autostring):
   print key, value
}}}
This example is dealing with certain section.
Line 226: Line 254:
This is a neat piece of software, but why is it GPL licenced? cmkl.  * This is a neat piece of software, but why is it GPL licenced?     -- cmkl.
 * Because I like GPL :) -- limodou

::-- 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]
  • can save config items order
  • support multi data types: string, unicode, int, float, list/tuple, dict, etc, you can save them to or regain them from config file
  • can convert to dict new

  • 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. What's new?

  • 2005/01/04 Version 0.5
    • 2006/01/04
      • Add ordereditems() method, so you can get the items according the ini file definition
    • 2005/12/30
      • Support onelevel parameter, you can set it True, than only one level can be used, so that the key can include section delimeter char in it.
      • Support sectiondelimeter parmeter, you can set the section delimeter to which you want
  • 2005/12/12 Version 0.4
    • Fixed a bug about "\" in option's value, thanks to Andreas Kaiser
  • 2005/12/09 Version 0.3
    • Adding dict() method, then you can change the DictIni object to dict type, so you can really use it as a dict alternative

  • 2005/10/16 Version 0.2
    • Saving config items order
    • Support float format

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:

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