[Tutor] troubles with ConfigParser

Eur van Andel eur at fiwihex.nl
Sat Nov 15 21:56:37 EST 2003


Hi

I did use the configparser and it is working great. There are some troubles,
though, especially when writing an INI file. Reading is just fine.

here is my code:


#       program settings4.py
import sys
import ConfigParser
 

if __name__=="__main__":

   st = ConfigParser.ConfigParser()   

   # get pump controller parameters 
   st.add_section('co2')
   st.set('co2', 'sollwert', 77)
   
   st.add_section('pump1')
   st.add_section('pump2')
   st.set('pump1', 'max_speed', 17)
   st.set('pump2', 'max_speed', 27)
   
   st.set('pump1', 'manual_speed', 37)
   st.set('pump2', 'manual_speed', 47)
   st.set('pump1', 'manual', 7)
   st.set('pump2', 'manual', 7)
   
   st.add_section('temps')
   st.set('temps', 'heat', 57)
   st.set('temps', 'cool', 67)

   
   # get fan settings
   for n in range(6):
      st.add_section('fan%1d' % n )
      st.set('fan%1d' % n, 'heat', 50+n)
      st.set('fan%1d' % n, 'cool', 60+n)

      st.set('fan%1d' % n, 'fan_manual_speed', n)
      st.set('fan%1d' % n, 'fan_manual', 10+n)     

      st.set('fan%1d' % n, 'dtai', 1)       
      st.set('fan%1d' % n, 'dtao', 1)      
      st.set('fan%1d' % n, 'dtwl', 1)       
      st.set('fan%1d' % n, 'dtwh', 1)


   for sect in st.sections():
      print '[', sect, ']'
      for opt in st.options(sect):
          print opt
#          print st.get(sect, opt)
      print

#   st.write('settings.ini')

#################

If I unhash either of the two lines, I run into trouble:

>Traceback (most recent call last):
>  File "st_try.py", line 78, in ?
>    print st.get(sect, opt)
>  File "/usr/local/lib/python2.2/ConfigParser.py", line 280, in get
>    return self._interpolate(section, option, value, d)
>  File "/usr/local/lib/python2.2/ConfigParser.py", line 288, in _interpolate
>    if value.find("%(") != -1:
>AttributeError: 'int' object has no attribute 'find'
??? 

It seems the get() doesn't work. Did I use the set() function wrong?


>Traceback (most recent call last):
>  File "st_try.py", line 81, in ?
>    st.write('settings.ini')
>  File "/usr/local/lib/python2.2/ConfigParser.py", line 351, in write
>    fp.write("[%s]\n" % section)
>AttributeError: 'str' object has no attribute 'write'
???

Should this file exist? (It does) Should I open it first? (I tried)

--
Ir. E.E. van Andel, Fine Wire Heat Exchangers, Fiwihex B.V. www.fiwihex.com
Wierdensestraat 74, NL-7604 BK  Almelo, The Netherlands   eur at fiwihex.nl
phone +31-546-491106  fax +31-546-491107  mobile +31-653-286573  



More information about the Tutor mailing list