~pythonregexp2.7/python/issue2636-01

« back to all changes in this revision

Viewing changes to Doc/library/configparser.rst

  • Committer: Jeffrey C. "The TimeHorse" Jacobs
  • Date: 2008-06-09 14:37:21 UTC
  • mfrom: (39022.1.14 Regexp-2.6)
  • Revision ID: darklord@timehorse.com-20080609143721-bj0g1mwta28038da
Merged in changes from the core Regexp branch.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
:mod:`configparser` --- Configuration file parser
 
1
:mod:`ConfigParser` --- Configuration file parser
2
2
=================================================
3
3
 
4
4
.. module:: ConfigParser
5
 
   :synopsis: Old name for the configparser module.
6
 
 
7
 
.. module:: configparser
8
5
   :synopsis: Configuration file parser.
9
6
 
10
7
.. moduleauthor:: Ken Manheimer <klm@zope.com>
13
10
.. sectionauthor:: Christopher G. Petrilli <petrilli@amber.org>
14
11
 
15
12
.. note::
16
 
   The :mod:`ConfigParser` module has been renamed to :mod:`configparser` in
17
 
   Python 3.0.  It is importable under both names in Python 2.6 and the rest of
18
 
   the 2.x series.
 
13
 
 
14
   The :mod:`ConfigParser` module has been renamed to `configparser` in Python
 
15
   3.0.  The :term:`2to3` tool will automatically adapt imports when converting
 
16
   your sources to 3.0.
19
17
 
20
18
.. index::
21
19
   pair: .ini; file
233
231
   load the required file or files using :meth:`readfp` before calling :meth:`read`
234
232
   for any optional files::
235
233
 
236
 
      import configparser, os
 
234
      import ConfigParser, os
237
235
 
238
 
      config = configparser.ConfigParser()
 
236
      config = ConfigParser.ConfigParser()
239
237
      config.readfp(open('defaults.cfg'))
240
238
      config.read(['site.cfg', os.path.expanduser('~/.myapp.cfg')])
241
239
 
375
373
 
376
374
An example of writing to a configuration file::
377
375
 
378
 
   import configparser
379
 
 
380
 
   config = configparser.RawConfigParser()
381
 
   
 
376
   import ConfigParser
 
377
 
 
378
   config = ConfigParser.RawConfigParser()
 
379
 
382
380
   # When adding sections or items, add them in the reverse order of
383
381
   # how you want them to be displayed in the actual file.
384
382
   # In addition, please note that using RawConfigParser's and the raw
393
391
   config.set('Section1', 'baz', 'fun')
394
392
   config.set('Section1', 'bar', 'Python')
395
393
   config.set('Section1', 'foo', '%(bar)s is %(baz)s!')
396
 
   
 
394
 
397
395
   # Writing our configuration file to 'example.cfg'
398
396
   with open('example.cfg', 'wb') as configfile:
399
397
       config.write(configfile)
400
398
 
401
399
An example of reading the configuration file again::
402
400
 
403
 
   import configparser
 
401
   import ConfigParser
404
402
 
405
 
   config = configparser.RawConfigParser()
 
403
   config = ConfigParser.RawConfigParser()
406
404
   config.read('example.cfg')
407
405
 
408
406
   # getfloat() raises an exception if the value is not a float
419
417
To get interpolation, you will need to use a :class:`ConfigParser` or
420
418
:class:`SafeConfigParser`::
421
419
 
422
 
   import configparser
 
420
   import ConfigParser
423
421
 
424
 
   config = configparser.ConfigParser()
 
422
   config = ConfigParser.ConfigParser()
425
423
   config.read('example.cfg')
426
424
 
427
425
   # Set the third, optional argument of get to 1 if you wish to use raw mode.
433
431
   print config.get('Section1', 'foo', 0, {'bar': 'Documentation',
434
432
                                           'baz': 'evil'})
435
433
 
436
 
Defaults are available in all three types of ConfigParsers. They are used in 
 
434
Defaults are available in all three types of ConfigParsers. They are used in
437
435
interpolation if an option used is not defined elsewhere. ::
438
436
 
439
 
   import configparser
 
437
   import ConfigParser
440
438
 
441
439
   # New instance with 'bar' and 'baz' defaulting to 'Life' and 'hard' each
442
 
   config = configparser.SafeConfigParser({'bar': 'Life', 'baz': 'hard'})
 
440
   config = ConfigParser.SafeConfigParser({'bar': 'Life', 'baz': 'hard'})
443
441
   config.read('example.cfg')
444
 
   
 
442
 
445
443
   print config.get('Section1', 'foo') # -> "Python is fun!"
446
444
   config.remove_option('Section1', 'bar')
447
445
   config.remove_option('Section1', 'baz')
452
450
   def opt_move(config, section1, section2, option):
453
451
       try:
454
452
           config.set(section2, option, config.get(section1, option, 1))
455
 
       except configparser.NoSectionError:
 
453
       except ConfigParser.NoSectionError:
456
454
           # Create non-existent section
457
455
           config.add_section(section2)
458
456
           opt_move(config, section1, section2, option)