[雪峰磁针石博客]python3快速入门教程9重要的标准库-高级

作者: oychw | 来源:发表于2018-06-20 22:23 被阅读78次

    [雪峰磁针石博客]python3快速入门教程

    输出格式

    reprlib模块为大型的或深度嵌套的容器提供了缩写显示的repr():

    #!python
    >>> import reprlib
    >>> reprlib.repr(set('supercalifragilisticexpialidocious'))
    "set(['a', 'c', 'd', 'e', 'f', 'g', ...])"
    

    pprint提供显示更好的print。

    #!python
    >>> import pprint
    >>> t = [[[['black', 'cyan'], 'white', ['green', 'red']], [['magenta',
    ...     'yellow'], 'blue']]]
    ...
    >>> pprint.pprint(t, width=30)
    [[[['black', 'cyan'],
       'white',
       ['green', 'red']],
      [['magenta', 'yellow'],
       'blue']]]
    

    textwrap模块格式化文本段落以适应设定的屏宽:

    #!python
    >>> import textwrap
    >>> doc = """The wrap() method is just like fill() except that it returns
    ... a list of strings instead of one big string with newlines to separate
    ... the wrapped lines."""
    ...
    >>> print(textwrap.fill(doc, width=40))
    The wrap() method is just like fill()
    except that it returns a list of strings
    instead of one big string with newlines
    to separate the wrapped lines.
    

    locale提供本地化支持:

    #!python
    >>> import locale
    >>> locale.setlocale(locale.LC_ALL, 'English_United States.1252')
    'English_United States.1252'
    >>> conv = locale.localeconv()          # get a mapping of conventions
    >>> x = 1234567.8
    >>> locale.format("%d", x, grouping=True)
    '1,234,567'
    >>> locale.format_string("%s%.*f", (conv['currency_symbol'],
    ...                      conv['frac_digits'], x), grouping=True)
    '$1,234,567.80'
    

    模板

    string提供了模版类Template,语法简单,用户可以在不进行改变的情况下定制他们的应用程序。

    使用$为开头的Python合法标识(数字、字母和下划线)作为占位符。如果后面不是空格,占位符外面需要添加大括号。 $$则表示原来的$:

    #!python
    >>> from string import Template
    >>> t = Template('${village}folk send $$10 to $cause.')
    >>> t.substitute(village='Nottingham', cause='the ditch fund')
    'Nottinghamfolk send $10 to the ditch fund.'
    

    当占位符在没有提供时,substitute()方法就会抛出KeyError 异常。 对于邮件合并风格的应用程序,用户提供的数据可能并完整,这时使用 safe_substitute()方法可能更适合:

    #!python
    >>> t = Template('Return the $item to $owner.')
    >>> d = dict(item='unladen swallow')
    >>> t.substitute(d)
    Traceback (most recent call last):
      ...
    KeyError: 'owner'
    >>> t.safe_substitute(d)
    'Return the unladen swallow to $owner.'
    

    模板子类可以指定自定义分隔符。例如,图像查看器的批量重命名工具可能选择使用百分号作为占位符,比如当前日期,图片序列号或文件格式:

    #!python
    >>> import time, os.path
    >>> photofiles = ['img_1074.jpg', 'img_1076.jpg', 'img_1077.jpg']
    >>> class BatchRename(Template):
    ...     delimiter = '%'
    >>> fmt = input('Enter rename style (%d-date %n-seqnum %f-format):  ')
    Enter rename style (%d-date %n-seqnum %f-format):  Ashley_%n%f
    
    >>> t = BatchRename(fmt)
    >>> date = time.strftime('%d%b%y')
    >>> for i, filename in enumerate(photofiles):
    ...     base, ext = os.path.splitext(filename)
    ...     newname = t.substitute(d=date, n=i, f=ext)
    ...     print('{0} --> {1}'.format(filename, newname))
    
    img_1074.jpg --> Ashley_0.jpg
    img_1076.jpg --> Ashley_1.jpg
    img_1077.jpg --> Ashley_2.jpg
    

    模板的另一个作用是把多样的输出格式细节从程序逻辑中分离出来。这样可定制XML 文件,纯文本报表和 HTML WEB 报表模板。

    [雪峰磁针石博客]python标准模块介绍-string:文本常量和模板

    使用二进制数据记录布局

    struct模块为变长的二进制记录格式提供了pack()和unpack()函数。下面的示例演示了在不使用zipfile 块的情况下如何查看ZIP 文件的头信息。 Pack码"H"和"I"分别表示2和4字节无符号数字, "<" 表明它们都是标准大小并且为little-endian字节序。

    #!python
    import struct
    
    with open('myfile.zip', 'rb') as f:
        data = f.read()
    
    start = 0
    for i in range(3):                      # show the first 3 file headers
        start += 14
        fields = struct.unpack('<IIIHH', data[start:start+16])
        crc32, comp_size, uncomp_size, filenamesize, extra_size = fields
    
        start += 16
        filename = data[start:start+filenamesize]
        start += filenamesize
        extra = data[start:start+extra_size]
        print(filename, hex(crc32), comp_size, uncomp_size)
    
        start += extra_size + comp_size     # skip to the next header
    

    多线程

    线程是分离无顺序依赖关系任务的技术。在某些任务运行于后台的时候应用程序会变得迟缓,线程可以提升速度。比如在 I/O 的同时其它线程可以并行计算。

    下例演示高级模块threading 如何在主程序运行的同时运行任务:

    #!python
    import threading, zipfile
    
    class AsyncZip(threading.Thread):
        def __init__(self, infile, outfile):
            threading.Thread.__init__(self)
            self.infile = infile
            self.outfile = outfile
        def run(self):
            f = zipfile.ZipFile(self.outfile, 'w', zipfile.ZIP_DEFLATED)
            f.write(self.infile)
            f.close()
            print('Finished background zip of:', self.infile)
    
    background = AsyncZip('mydata.txt', 'myarchive.zip')
    background.start()
    print('The main program continues to run in foreground.')
    
    background.join()    # Wait for the background task to finish
    print('Main program waited until background was done.')
    

    多线程应用程序的主要挑战是共享数据或其它资源。threading模块提供了许多同步原语,包括:锁,事件,状态变量和信号。

    尽管这些工具很强大,微小的设计错误也可能造成难以挽回的故障。因此,任务协调的首选方法是把对资源的所有访问集中在一个单独的线程中,然后使用queue模块服务其他线程的请求。使用Queue对象的应用程序更易于设计,更可读,并且更可靠。

    因为python的GIL,多线程只能占用以核的CPU。对CPU要求比较高的应用推荐使用multiprocessing。

    • 参考资料

    python库介绍-multiprocessing:多进程

    [并发和并行及异步与网络(Concurrency and Parallelism)])https://github.com/china-testing/python-api-tesing#%E5%B9%B6%E5%8F%91%E5%92%8C%E5%B9%B6%E8%A1%8C%E5%8F%8A%E5%BC%82%E6%AD%A5%E4%B8%8E%E7%BD%91%E7%BB%9Cconcurrency-and-parallelism)

    日志

    logging模块提供了完整和灵活的日志系统。

    #!python
    import logging
    logging.debug('Debugging information')
    logging.info('Informational message')
    logging.warning('Warning:config file %s not found', 'server.conf')
    logging.error('Error occurred')
    logging.critical('Critical error -- shutting down')
    

    输出如下:

    #!python
    WARNING:root:Warning:config file server.conf not found
    ERROR:root:Error occurred
    CRITICAL:root:Critical error -- shutting down
    

    默认捕获信息和调试消息不会输出,输出为准错误流。可输出信息到email,数据报文,socket或者HTTP服务器。过滤器可以基于DEBUG、INFO、WARNING、ERROR和CRITICAL选择不同的路由, 。

    日志系统可以直接在 Python 代码中定制,也可以不经过应用程序直接在一个用户可编辑的配置文件中加载。

    弱引用

    Python自动进行内存管理(对大多数的对象进行引用计数和垃圾回收), 在最后一个引用消失后,内存会很快释放。

    这个工作方式对大多数应用程序工作良好,但是偶尔对象被别的地方使用时会需要跟踪对象。仅仅为跟踪它们创建引用也会使其长期存在。 weakref 模块提供了不用创建引用的跟踪对象工具,一旦对象不需要时,它自动从弱引用表上删除并触发回调。典型的应用包括捕获难以构造的对象:

    #!python
    >>> import weakref, gc
    >>> class A:
    ...     def __init__(self, value):
    ...         self.value = value
    ...     def __repr__(self):
    ...         return str(self.value)
    ...
    >>> a = A(10)                   # create a reference
    >>> d = weakref.WeakValueDictionary()
    >>> d['primary'] = a            # does not create a reference
    >>> d['primary']                # fetch the object if it is still alive
    10
    >>> del a                       # remove the one reference
    >>> gc.collect()                # run garbage collection right away
    0
    >>> d['primary']                # entry was automatically removed
    Traceback (most recent call last):
      File "<stdin>", line 1, in <module>
        d['primary']                # entry was automatically removed
      File "C:/python36/lib/weakref.py", line 46, in __getitem__
        o = self.data[key]()
    KeyError: 'primary'
    

    列表工具

    很多数据结构会用到内置列表类型。然而有时可能需要不同性能的实现。

    array 块提供了类似列表的array()对象,它仅仅是存储数据更紧凑。以下的示例演存储双字节无符号整数的数组(类型编码)"H")而非存储16字节Python 整数对象的常规列表:

    #!python
    >>> from array import array
    >>> a = array('H', [4000, 10, 700, 22222])
    >>> sum(a)
    26932
    >>> a[1:3]
    array('H', [10, 700])
    

    collections模块提供了类似列表的deque()对象,它从左边append和pop更快,但是中间查询更慢。这些对象更适用于队列实现和广度优先的树搜索:

    #!python
    >>> from collections import deque
    >>> d = deque(["task1", "task2", "task3"])
    >>> d.append("task4")
    >>> print("Handling", d.popleft())
    Handling task1
    
    unsearched = deque([starting_node])
    def breadth_first_search(unsearched):
        node = unsearched.popleft()
        for m in gen_moves(node):
            if is_goal(m):
                return m
            unsearched.append(m)
    

    除了链表的替代实现,该库还提供了bisect这样的模块以操作有序链表:

    #!python
    >>> import bisect
    >>> scores = [(100, 'perl'), (200, 'tcl'), (400, 'lua'), (500, 'python')]
    >>> bisect.insort(scores, (300, 'ruby'))
    >>> scores
    [(100, 'perl'), (200, 'tcl'), (300, 'ruby'), (400, 'lua'), (500, 'python')]
    

    heapq模块提供了基于常规链表的堆实现。最小的值总是保持在0。这在希望循环访问最小元素但是不想执行完整列表排序的时候非常有用:

    #!python
    >>> from heapq import heapify, heappop, heappush
    >>> data = [1, 3, 5, 7, 9, 2, 4, 6, 8, 0]
    >>> heapify(data)                      # rearrange the list into heap order
    >>> heappush(data, -5)                 # add a new entry
    >>> [heappop(data) for i in range(3)]  # fetch the three smallest entries
    [-5, 0, 1]
    

    十进制浮点数算法

    decimal模块提供了Decimal数据类型用于浮点数计算。相比内置的二进制浮点数实现float,这个类型更适用于:

    • 金融应用和其它需要精确十进制表达的场合,
    • 控制精度,
    • 控制四舍五入
    • 重要数据,
    • 其他希望计算结果与手算相符的场合。

    例如对70分电话费的5%税计算,十进制浮点数和二进制浮点数计算结果的差别如下。如果在分值上舍入,这个差别就很大:

    #!python
    >>> from decimal import *
    >>> round(Decimal('0.70') * Decimal('1.05'), 2)
    Decimal('0.74')
    >>> round(.70 * 1.05, 2)
    0.73
    

    Decimal的结果总是保有结尾的0,自动从两位精度延伸到4位。Decimal重现了手工的数学运算,这就确保了二进制浮点数无法精确保有的数据精度。

    Decimal可以执行二进制浮点数无法进行的模运算和等值测试:

    #!python
    >>> Decimal('1.00') % Decimal('.10')
    Decimal('0.00')
    >>> 1.00 % 0.10
    0.09999999999999995
    
    >>> sum([Decimal('0.1')]*10) == Decimal('1.0')
    True
    >>> sum([0.1]*10) == 1.0
    False
    

    decimal提供了高精度算法:

    #!python
    >>> getcontext().prec = 36
    >>> Decimal(1) / Decimal(7)
    Decimal('0.142857142857142857142857142857142857')
    

    可爱的python测试开发库 请在github上点赞,谢谢!
    python中文库文档汇总
    接口自动化性能测试线上培训大纲
    python测试开发自动化测试数据分析人工智能自学每周一练
    [雪峰磁针石博客]python3标准库-中文版

    更多内容请关注 雪峰磁针石:简书

    • 技术支持qq群: 144081101(后期会录制视频存在该群群文件) 591302926 567351477 钉钉免费群:21745728

    • 道家技术-手相手诊看相中医等钉钉群21734177 qq群:391441566 184175668 338228106 看手相、面相、舌相、抽签、体质识别。服务费50元每人次起。请联系钉钉或者微信pythontesting

    https://docs.python.org/3/tutorial/stdlib2.html

    相关文章

      网友评论

        本文标题:[雪峰磁针石博客]python3快速入门教程9重要的标准库-高级

        本文链接:https://www.haomeiwen.com/subject/bwhfyftx.html