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 天獨 閱讀(392) 評論(0)  編輯  收藏 所屬分類: Python

          只有注冊用戶登錄后才能發表評論。


          網站導航:
           
          主站蜘蛛池模板: 河南省| 木里| 乌审旗| 寻乌县| 西宁市| 绥宁县| 潮安县| 平塘县| 鸡西市| 寻乌县| 中方县| 辉县市| 盐边县| 固始县| 利川市| 西充县| 博兴县| 河南省| 开封县| 舟山市| 图木舒克市| 江山市| 喀喇沁旗| 彩票| 蓬溪县| 青阳县| 鲁甸县| 兰西县| 额济纳旗| 乌鲁木齐市| 兴义市| 元氏县| 湘乡市| 辛集市| 广丰县| 南澳县| 易门县| 法库县| 新巴尔虎左旗| 勃利县| 儋州市|