Python 经验总结

This is all the knowledge learned from others’ code. We can learn it together.

# 包含在这个博客里面的内容都是简单的模块使用,是本人亲自试用过的代码,因为Python的模块众多,
# 其他没有涉及到模块是本人未曾涉足的模块,本博客里面的代码只是简单的使用,不涉及极其复杂的编程。

1、StringIO模块,将输入测字符串作为流返回,可以进行迭代,示例如下:

# the regular expression below used for split string to a list by one or more whitespace.

    pass_regular_expression = r'UEC-%d\s+\|' % uec_index
    # below just think it return a string, It's part code of mine.
    ret = self.execute_command(rrc_log_ret_cmd, pass_regexp=pass_regular_expression)
    for line in StringIo(ret):
        if 'rrc' in line and not found_rrc_flag:
            found_rrc_flag = True
            column_of_rrc = re.split(r'\s+', line.strip()).index('rrc')
            continue
        if re.findall(pass_regular_expression, line):
            ret_state = re.split(r'\s+', line.strip())[column_of_rrc + 1]
            break
    # --------------------------------------------------------------------------
    # re.search
    re.search(':author: (?P<author>.+)', module.__doc__).group('author').decode('utf-8', 'ignore')
    # http://www.crifan.com/detailed_explanation_about_python_regular_express_named_group/

2、re模块,正则表达式模块,示例如下:

# the regular expression below used for split string to a list by one or more whitespace.

    pass_regular_expression = r'UEC-%d\s+\|' % uec_index
    # below just think it return a string, It's part code of mine.
    ret = self.execute_command(rrc_log_ret_cmd, pass_regexp=pass_regular_expression)
    for line in StringIo(ret):
        if 'rrc' in line and not found_rrc_flag:
            found_rrc_flag = True
            column_of_rrc = re.split(r'\s+', line.strip()).index('rrc')
            continue
        if re.findall(pass_regular_expression, line):
            ret_state = re.split(r'\s+', line.strip())[column_of_rrc + 1]
            break
    # -------------------------------------------------------------
    # re.search
    re.search(':author: (?P<author>.+)', module.__doc__).group('author').decode('utf-8', 'ignore')
    # http://www.crifan.com/detailed_explanation_about_python_regular_express_named_group/

3、subprocess模块,用于启动操作系统上的shell命令行,并可以返回执行的结果

# with the subprocess to execute the command. 
# Popen.returncode 
# The child return code, set by poll() and wait() (and indirectly by communicate()). A None 
# value indicates that the process hasn’t terminated yet.
decode_result = subprocess.Popen(decode_cmd, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
start = datetime.datetime.now()
decode_log = "start to decode log.\n\r"
while decode_result.poll() is None:
    for line in decode_result.stdout.readlines():
        decode_log += line
    time.sleep(1)
    if (datetime.datetime.now() - start).seconds > timeout:
        failed_reason = "decode run timeout"
        self._log.error(failed_reason)
        rc = decode_result.returncode
        self._log.info("timeout,return code is %s" % rc)
        istimeout = True
        ctypes.windll.kernel32.TerminateProcess(int(decode_result._handle), -1)
        break   

4、collections集合相关

# the use is special
from collections import Counter
print Counter("hello")
>>> Counter({'l': 2, 'h': 1, 'e': 1, 'o': 1})   

5、 glob模块

# list all the file enb.py, can with wildchar, like * etc.
# glob.glob('*.py') can list all file under current path endwiths .py
file_list = glob.glob("%s/*/enb.py" % os.path.dirname(os.path.abspath(__file__))) 

6、1) os.path模块

# This used for deal with file and directory
os.path.dirname('C:/a/b') --> 'C:/a'
os.path.basename('C:/a/b') --> 'b'

2) os.system(command)函数

# This method can execute the command which in string type, and return the exit statu, # which in linux 0 often stands for success! cmd_str = 'ps -ef | egrep "egate|edaemon|sim_" | egrep "$EDAEMONPORT|$EGATEPORT" | \ awk \'{print $2}\' | xargs -i sudo kill -9 {}' log_ret_message = '' if 0 == os.system(cmd_str):
    log_ret_message = 'The command executed success! %s'%cmd_str
else:
    log_ret_message = 'The command executed failed! %s'%cmd_str

7、sys模块

# get the system local coding infomation
>>> import sys
>>> sys.getdefaultencoding()
'ascii'
>>> reload(sys)
<module 'sys' (built-in)>
>>> sys.setdefaultencoding('utf-8')
>>> sys.getdefaultencoding()

8、chardet模块

# detect the coing infomation
>>> import chardet
>>> f = open('test.txt','r')
>>> result = chardet.detect(f.read())
>>> result
{'confidence': 0.99, 'encoding': 'utf-8'}

9、importlib模块

# This module used for import the module which you want by code itself.
# The module name should be in full path name contains package name
# like os.path.join etc..
importlib.import_module("The module name which you want import")

10、time模块

 # get the time from a baseline in seconds, we can use this module to calculate 
# how much time does our program cost.
import time
start_time = time.time()
# here some program which you want to konw how long have your program stay there
end_time = time.time()
during_time = end_time - start_time

11、pydoc模块

# this module used for extract the docstring from the python source code.
# we can use it just like below.
from pydoc import help
help(模块名)
# we can get more options, with the command in console or cmd window just like below
pydoc --help
# we can get the docstring into a html file in console or cmd window
pydoc -w 模块名

12、robot模块

# this module used for test automation. what within this module is lots of sub-modules.
# 最近学习使用sphinx抽取python源代码的docstring形成html文档,相较于pydoc的初步阶段,
# sphinx做的更为优秀,关于sphinx的使用准备在另外一个博客中记述,此处省略。
# sphinx生成网页版的界面良好的API,需要使用rst格式的文件,robot提供了提取docstring生成xml的robot.libdoc
# 模块,然后需要自己编码实现xml到rst的转换,之后通过sphinx生成html,当然sphinx提供了简单的
# .py到.rst的转换,但是提取的信息只是框架,docstring信息提取不全。
# robot模块的使用在另外博文中记述了,有兴趣可以查阅《测试自动化——robotframework》

13、unittest模块

# this module used for unit test单元测试
# 用法基本如下代码。
# -*- coding: utf-
import unittest
class Test(unittest.TestCase):

    def setUp(self):
        """set up"""
        self.iphy_instant = iphy()

    def tearDown(self):
        """none"""
        pass

    def test_control_rrc_log_on(self):
        ret_state = self.iphy_instant._extract_rrc_log_status(log_info=loginfo.                                                             RRC_LOG_ON_INFO,regexp=r'UEC-1\s+\|')
        self.assertEqual(ret_state, "on", "Enable rrc log function failed!")

    if __name__ == "__main__":
        unittest.main()

14、itertools

# we can learn by below blog:
# http://www.cnblogs.com/vamei/p/3174796.html
# http://blog.csdn.net/largetalk/article/details/6905378
# this module used for iter elements用于产生迭代器
import itertools as tool
a = range(10) #
print [i for i in tool.imap(lambda e:e+2, a)]

15、fabric

# This module can used for remote operate server with ssh
# 这个模块用于使用ssh远程操作一系列的服务器,运维管理
# http://blog.csdn.net/wklken/article/details/8719541

16、pexpect

# 这个模块用于模拟expect类型的脚本解释器,实现自动交互,比如自动
# 远程使用ssh, telnet连接服务器,并做一些操作,比如拷贝文件等
# 等。
# 具体参看下边博客http://www.cnblogs.com/ma6174/archive/2012/05/25/2508378.html

你可能感兴趣的:(python)