data design

Jussi Salmela tiedon_jano at hotmail.com
Thu Feb 1 12:47:29 EST 2007


James Stroud kirjoitti:
> 
> <snip> 
>
> For instance, I have a copy_files section of a configuration. In order 
> to know what goes with what you have to resort to gymnastics with the 
> option names
> 
> [copy_files]
> files_dir1 = this.file that.file
> path_dir1 = /some/path
> 
> files_dir2 = the_other.file yet_another.file
> path_dir2 = /some/other/path
> 
> <snip> 
> James

You don't have to. With a config file:

###
[copy_files]
/some/path = this.file that.file
C:\a windows\path with spaces= one.1 two.two
	a_continuation_line_starting_with_a_tab.xyz
       and_another_starting_with_a_some_spaces.abc
/some/other/path = the_other.file yet_another.file
###

the following program:

###
#!/usr/bin/python

import ConfigParser

config = ConfigParser.ConfigParser()
config.readfp(open(r'ConfigTest.INI'))
opts = config.options('copy_files')
print opts
print 'Files to be copied:'
for opt in opts:
     path = opt
     optVal = config.get('copy_files', opt)
     #print opt, optVal
     fileNames = optVal.split()
     ### The following lines are only needed for Windows
     ### because the use of ':' in Windows' file name's
     ### device part clashes with its use in ConfigParser
     pathParts = ''
     for ind in range(len(fileNames)):
         if fileNames[ind][-1] in ':=':
             path += ':' + pathParts + fileNames[ind][:-1]
             del fileNames[:ind+1]
             break
         pathParts += fileNames[ind] + ' '
     ### Windows dependent section ends
     print '    Path:', '>' + path + '<'
     for fn in fileNames:
         print '        >' + fn + '<'
###

produces the following output:

###
['c', '/some/other/path', '/some/path']
Files to be copied:
     Path: >c:\a windows\path with spaces<
         >one.1<
         >two.two<
         >a_continuation_line_starting_with_a_tab.xyz<
         >and_another_starting_with_a_some_spaces.abc<
     Path: >/some/other/path<
         >the_other.file<
         >yet_another.file<
     Path: >/some/path<
         >this.file<
         >that.file<

###

Cheers,
Jussi



More information about the Python-list mailing list