[Tutor] reading configuration file

Eur van Andel eur at fiwihex.nl
Tue Nov 11 05:07:45 EST 2003


Hi all

I've made a prog that reads a configuration file. While it works, I think it is
butt-ugly and defintely not the way it should be done in Python.

I tried to understand dictionaries and I tried to make eval() work. I failed. 
What is the good Python way to do what I do below?

config file:

>-----------------------------------
# parameter settings for pump board and fan boards
# from six fans and pump controller

# pump controller settings
CO2_soll		 80
pump_1_manual_override    0  
pump_1_manual_speed     120
pump_2_manual_override    0  
pump_2_manual_speed     130 

# Fan board settings:
fan_manual_override    0  
fan_manual_speed     140 
>-----------------------------------

code:

>-----------------------------------

#! /usr/local/bin/python
####################################################################
#
#	program config.py
# 	version 0.1 09-11-2003
#
#	reads configuration variables
#	writes them to boards
#	keeps file date: if file is touched, will make new file
#
###################################################################

import sys
import os
from time import strftime, localtime, sleep
import random, fpformat
from xwisp_fwx3 import fwx
import string


def parse_line(s):
   global CO2_soll
   global pump_1_manual_override, pump_1_manual_speed
   global pump_2_manual_override, pump_2_manual_speed
   global fan_manual_override, fan_manual_speed
   ls = string.split(s)
   if   ls[0] == 'CO2_soll':
      CO2_soll = int(ls[1])
   elif ls[0] == 'pump_1_manual_override':
      pump_1_manual_override = int(ls[1])
   elif ls[0] == 'pump_1_manual_speed':
      pump_1_manual_speed = int(ls[1])
   elif ls[0] == 'pump_2_manual_override':
      pump_2_manual_override = int(ls[1])
   elif ls[0] == 'pump_2_manual_speed':
      pump_2_manual_speed = int(ls[1])
   elif ls[0] == 'fan_manual_override':
      fan_manual_override = int(ls[1])
   elif ls[0] == 'fan_manual_speed':
      fan_manual_speed = int(ls[1])


def read_config():
   config = open('config','r')
   line = config.readline()
   while line:
      line = string.strip(line)
      if len(line) > 0 and line[0] != "#":    # ignore comments and blank lines
         parse_line(line)   
      line = config.readline()
   config.close()


############################ START OF PROGRAM #######################
# initialisation of global variables 

# pumps and CO2
CO2_soll		      =  81  # ppm /10
pump_1_manual_override 	=   1  # 0 = auto, 1 = manual
pump_1_manual_speed 	= 121  # [0..255]
pump_2_manual_override 	=   1  
pump_2_manual_speed 	= 131 


# Fan board settings:
fan_manual_override    	=   1  # 0 = auto, 1 = manual
fan_manual_speed     	= 141



read_config()

print CO2_soll
print pump_1_manual_override, pump_1_manual_speed     
print pump_2_manual_override, pump_2_manual_speed
print fan_manual_override, fan_manual_speed 

 
#config_time_old = os.path.getmtime('config')
#print config_time_old

#config_time_old = os.path.getmtime('config')
#print config_time_old

#while True:
#   config_time = os.path.getmtime('config')
#   if config_time != config_time_old:
#      print config_time_old, config_time
#      make_configfile   
#      config_time_old = os.path.getmtime('config')


# parameter settings for pump board and fan boards
# from six fans and pump controller

# pump controller settings



--
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