免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
楼主: xichen
打印 上一主题 下一主题

每周一题——读写配置文件。 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2005-08-20 16:56 |显示全部楼层 |倒序浏览
群里面的朋友提到是否应该出点题目给新加入python的朋友。我想值得一试。

本周题目如下:
读写配置文件是经常碰到操作,请大家写出自己读写配置文件的程序,要求:
1、配置文件可以以#作为注释,并不处理。
2、所写的程序应以import导入使用。
3、有自测试代码。
4、有注释。

希望大家踊跃参加。

论坛徽章:
0
2 [报告]
发表于 2005-08-23 19:58 |显示全部楼层

每周一题——读写配置文件。

好象关注的人多,但是真正动手写的人少.一方面新手老说没有项目可以联系,一方面却不愿意花精神和时间来些,真让人郁闷.
可能被老师教惯了吧.总想google到最直接的答案.

论坛徽章:
0
3 [报告]
发表于 2005-08-24 15:13 |显示全部楼层

每周一题——读写配置文件。

下周一我贴出我写的。

论坛徽章:
0
4 [报告]
发表于 2005-08-28 21:42 |显示全部楼层

每周一题——读写配置文件。

Python 的两个用于单元测试的标准模块:unittest和doctest。这些模块扩展了用来确认函数内部的先置条件和后置条件的内置 assert 语句的能力。
由于模块是被import的,那么需要在直接运行该模块的时候检查必须的环境是否具备,并运行一段测试代码确保程序无错误.
一般采用

if __name__ == "__main__":
    测试代码
这样的方法.

论坛徽章:
0
5 [报告]
发表于 2005-08-29 11:44 |显示全部楼层

每周一题——读写配置文件。

只是我的看法而已,其实各位写的都有自己的特色。

config.py,这个是模块
  1. # -*- coding: cp936 -*-

  2. """
  3. 本程序为MIT授权
  4. 作者:梅劲松
  5. """

  6. """
  7. The MIT License

  8. Copyright (c) 2005 stephen.cn@gmail.com

  9. Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

  10. The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

  11. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.


  12. Read config module.

  13. """

  14. __all__ = ['read_config']
  15. __version__ = '1.0'

  16. def read(file=''):
  17.     '''File name. String'''
  18.     if file=='':
  19.         return ('不能打开配置文件,请检查目录中是否有%s这个文件。\n' % file)
  20.     else:
  21.         config = get(file)
  22.         if not config:
  23.             return ('不能打开配置文件,请检查目录中是否有%s这个文件。\n' % file)
  24.         else:
  25.             return config
  26.             
  27. def get(file):
  28.     try:
  29.             fd = open(file)
  30.     except:
  31.             return None
  32.     lines = fd.readlines()
  33.     config = {}
  34.     for i in lines:
  35.             i = i.strip()
  36.             if i.find('=') < 0:
  37.                     continue
  38.             if i[0] == '#':
  39.                     continue
  40.             var, value = i.split('=',1)
  41.             var = var.strip()
  42.             value = value.split('=')
  43.             config[var] = value
  44.     return config


  45. def _test():
  46.     import doctest, config
  47.     return doctest.testmod(config)


  48. if __name__ == "__main__":
  49.     _test()
复制代码


然后还有test.txt这个用来测试的文件

  1. name=梅劲松
  2. age=32
  3. sex=男
  4. iq=傻瓜
复制代码

然后测试方式为在msdos方式下:

  1. D:\>;python config.py -v
  2. Running config.__doc__
  3. 0 of 0 examples failed in config.__doc__
  4. Running config._test.__doc__
  5. 0 of 0 examples failed in config._test.__doc__
  6. Running config.get.__doc__
  7. 0 of 0 examples failed in config.get.__doc__
  8. Running config.read.__doc__
  9. 0 of 0 examples failed in config.read.__doc__
  10. 4 items had no tests:
  11.     config
  12.     config._test
  13.     config.get
  14.     config.read
  15. 0 tests in 4 items.
  16. 0 passed and 0 failed.
  17. Test passed.

  18. D:\>;
复制代码

使用方法为

  1. D:\>;python
  2. Python 2.3.3 (#51, Dec 18 2003, 20:22:39) [MSC v.1200 32 bit (Intel)] on win32
  3. Type "help", "copyright", "credits" or "license" for more information.
  4. >;>;>; import config
  5. >;>;>; conf=config.read('test.txt')
  6. >;>;>; conf['ai']
  7. ['\xc9\xb5\xb9\xcf']
  8. >;>;>;
复制代码


欢迎大家指正。
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP