由于经常要查看python帮助文档,需要先输入python进入shell交互环境后再依次导入模块并调用help()函数才能查看python的帮助文档,何不动手写一个封装的脚本呢,然后添加为系统命令如pyhelp,直接运行之会更方便快捷。
代码如下
#! /usr/bin/env python
# coding=utf-8
import sys
try:
input = raw_input
except:
pass
def show_help_info():
msg = "\033[01mHelp: \033[0m"
module = input(msg).strip()
if not module:
return
if module.lower() == 'quit' or module.lower() == 'q':
sys.exit(0)
#try:
# table = str.maketrans({'.':None, '_':None})
#except:
# import string
# table = string.maketrans('._', '00')
#if not module.translate(table).isalnum():
digits = '0123456789'
letters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz'
valid_chars = digits + letters + "._"
for chr in module:
if chr not in valid_chars:
print('ERROR: %s: invalid module or function name' % module)
return
try:
import_module = 'import %s' % module
exec(import_module)
print('import %s' % module)
except ImportError as reason:
# import function name would cause ImportError
# goto call help function name
pass
except Exception as reason:
print(reason)
try:
help('%s' % module)
except Exception as reason:
print(reason)
if __name__ == '__main__':
version = '.'.join([str(v) for v in sys.version_info[:3]])
print('Python %s' % version)
while True:
show_help_info()
然后将以上代码保存为pyhelp.py
然后在~/.bashrc中写入
function pyhelp()
{
python pyhelp.py $*
}
然后source ~/.bashrc
以后直接执行pyhelp命令就可以了