posts - 1,  comments - 1,  trackbacks - 0
9-7习题是要求些一個解析配置文件的类,用来解析Win32,POSIX,或着其他平台下的配置文件,我在这里只写了一個Win32文件的ini文件的解析
下面是我的实现代码:
  1 #coding=utf-8
  2 
  3 '''
  4 Created on 2010-7-12
  5 
  6 @author: Innate Solitary
  7 
  8 ini文件解析类
  9 '''
 10 
 11 import ConfigParser
 12 
 13 
 14 class ConfigFile(object):
 15     '''
 16     配置文件解析,处理的简单实现类
 17     '''
 18 
 19 
 20     def __init__(self, file_name):
 21         '''
 22         Constructor
 23         '''
 24         self.file_name = file_name
 25         self.file = file(file_name)
 26         self.cfg = ConfigParser.ConfigParser()
 27         self.read_handle = None
 28         self.write_handle = None
 29         self.sections = []
 30         self.items = {}
 31         self.init_flag = False
 32     
 33     def initialization(self):
 34         '''
 35         初始化配置文件
 36         '''
 37         try:
 38             self.read_handle = open(self.file_name, 'r')
 39             self.write_handle = open(self.file_name, 'w')
 40             self.cfg.readfp(self.read_handle)
 41             self.sections = self.cfg.sections()
 42             for section in self.sections:
 43                 self.items[section] = self.cfg.items(section)
 44             self.init_flag = True
 45         except Exception, e:
 46             print '初始化配置文件错误:', str(e)
 47             self.init_flag = False
 48         return self.init_flag
 49     
 50     def close(self):
 51         '''
 52         关闭配置文件
 53         '''
 54         self.read_handle.close()
 55         del self.file
 56         del self.read_handle
 57         del self.file_name
 58         del self.cfg
 59         del self.sections
 60         del self.items
 61     
 62     def get_value(self, section, key, default = ''):
 63         '''
 64         得到指定section下的key的值,如果沒有給其一個空字符串做为默认值,并加入文件中
 65         '''
 66         if not self.init_flag:
 67             raise Exception('未初始化配置文件,请在处理文件前调用initialization(self)进行配置文件初始化')
 68         
 69         
 70         if not self.cfg.has_section(section):
 71             self.cfg.add_section(section)
 72         if not self.cfg.has_option(section, key):
 73             self.cfg.set(section, key, default)
 74                 
 75         value = self.cfg.get(section, key)
 76         
 77         
 78         return value
 79     
 80     def set_value(self, section, key, value):
 81         '''
 82         设置值
 83         '''
 84         
 85         if not self.cfg.has_section(section):
 86             self.cfg.add_section(section);
 87         
 88         self.cfg.set(section, key, value)
 89     
 90     def write_to_file(self, file = None):
 91         if file != None and file.mode == 'r':
 92             raise Exception('文件模式不可写')
 93         if file == None:
 94             self.cfg.write(self.write_handle)
 95         else:
 96             self.cfg.write(file)
 97 
 98         
 99         
100         
101 
我在这里使用了ConfigParser模块,这里我的这段代码因为工作的原因只实现了一部分,并沒有把我当初想到的功能实现,但是习题9-7要求的功能都实现了,代码很简单个人覺沒有什么难得所以沒有加注释。

posted on 2010-07-15 12:47 天独 阅读(360) 评论(0)  编辑  收藏 所属分类: Python

只有注册用户登录后才能发表评论。


网站导航: