我的python学习之路---optparse源代码学习
2012-04-03 11:43
453 查看
1、模块和类型可以复制给变量,如
>>> import os
>>> my_os=os
>>> my_os.chdir
<built-in function chdir>
2、尽可能的将输入以参数的形式输入,便于后续替换和扩展
即使是option类型也是通过参数传递给OptionParser构造函数的,便于后续更新
option实例代表单个option
3、动态创建对象属性
option的take_action函数将dest以属性的方式附加到vaules
def take_action(self, action, dest, opt, value, values, parser):
if action == "store":
setattr(values, dest, value)
elif action == "store_const":
setattr(values, dest, self.const)
elif action == "store_true":
setattr(values, dest, True)
4、错误和正常结束分离,发生错误时调用的函数一致
OptionParser有exit和error两个函数,当发生异常时,调用error函数,也可以不处理,由调用者自己来处理。
try:
stop = self._process_args(largs, rargs, values)
except (BadOptionError, OptionValueError) as err:
self.error(str(err))
正常结束时使用exit
elif action == "help":
parser.print_help()
parser.exit()
elif action == "version":
parser.print_version()
parser.exit()
两个函数
def exit(self, status=0, msg=None):
if msg:
sys.stderr.write(msg)
sys.exit(status)
def error(self, msg):
"""error(msg : string)
Print a usage message incorporating 'msg' to stderr and exit.
If you override this in a subclass, it should not return -- it
should either exit or raise an exception.
"""
self.print_usage(sys.stderr)
self.exit(2, "%s: error: %s\n" % (self.get_prog_name(), msg))
5、书写自己的异常处理类
上面提到的BadOptionError, OptionValueError都是自定义异常类型;
自定义类型很简单:
class OptParseError (Exception):
def __init__(self, msg):
self.msg = msg
def __str__(self):
return self.msg
class OptionError (OptParseError):
"""
Raised if an Option instance is created with invalid or
inconsistent arguments.
"""
def __init__(self, msg, option):
self.msg = msg
self.option_id = str(option)
def __str__(self):
if self.option_id:
return "option %s: %s" % (self.option_id, self.msg)
else:
return self.msg
>>> import os
>>> my_os=os
>>> my_os.chdir
<built-in function chdir>
2、尽可能的将输入以参数的形式输入,便于后续替换和扩展
即使是option类型也是通过参数传递给OptionParser构造函数的,便于后续更新
option实例代表单个option
3、动态创建对象属性
option的take_action函数将dest以属性的方式附加到vaules
def take_action(self, action, dest, opt, value, values, parser):
if action == "store":
setattr(values, dest, value)
elif action == "store_const":
setattr(values, dest, self.const)
elif action == "store_true":
setattr(values, dest, True)
4、错误和正常结束分离,发生错误时调用的函数一致
OptionParser有exit和error两个函数,当发生异常时,调用error函数,也可以不处理,由调用者自己来处理。
try:
stop = self._process_args(largs, rargs, values)
except (BadOptionError, OptionValueError) as err:
self.error(str(err))
正常结束时使用exit
elif action == "help":
parser.print_help()
parser.exit()
elif action == "version":
parser.print_version()
parser.exit()
两个函数
def exit(self, status=0, msg=None):
if msg:
sys.stderr.write(msg)
sys.exit(status)
def error(self, msg):
"""error(msg : string)
Print a usage message incorporating 'msg' to stderr and exit.
If you override this in a subclass, it should not return -- it
should either exit or raise an exception.
"""
self.print_usage(sys.stderr)
self.exit(2, "%s: error: %s\n" % (self.get_prog_name(), msg))
5、书写自己的异常处理类
上面提到的BadOptionError, OptionValueError都是自定义异常类型;
自定义类型很简单:
class OptParseError (Exception):
def __init__(self, msg):
self.msg = msg
def __str__(self):
return self.msg
class OptionError (OptParseError):
"""
Raised if an Option instance is created with invalid or
inconsistent arguments.
"""
def __init__(self, msg, option):
self.msg = msg
self.option_id = str(option)
def __str__(self):
if self.option_id:
return "option %s: %s" % (self.option_id, self.msg)
else:
return self.msg
相关文章推荐
- 我的Python学习之路之环境准备
- 王亟亟的Python学习之路(二)-项目创建以及要点提及
- Python学习之路17——字典
- Python学习之路(三):序列
- Python小白学习之路(十)—【函数】【函数返回值】【函数参数】
- Python 学习之路-基本的概念(二)
- 我的python学习之路-03
- Python小白学习之路(十六)—【内置函数一】
- Python学习之路
- 我的Python学习之路之基本语法-函数
- 从 Python到Tensorflow 学习之路(四)
- python 学习之路(函数与函数式编程)
- Python学习之路7——Python对象
- 【人生苦短,我用python】学习之路1
- Python3内置模块详解1 (Python 之路 Day5 - 常用模块学习 )
- python学习之路--hook(钩子原理和使用)
- python学习之路:语法基础
- python学习之路-变量
- 我的python学习之路----运行脚本文件(windows)
- python学习之路-13