zoukankan      html  css  js  c++  java
  • Python3 标准库概览

    一、操作系统接口
    1、os 模块提供了不少与操作系统关联的函数:

    # python3.6
    Python 3.6.8 (default, Nov 16 2020, 16:55:22)
    [GCC 4.8.5 20150623 (Red Hat 4.8.5-44)] on linux
    Type "help", "copyright", "credits" or "license" for more information.
    >>> import os
    >>> os.getcwd()  # 返回当前目录路径
    >>> os.chdir('/server/accesslogs')  # 修改当前目录
    >>> os.system('mkdir today')  # 执行系统命令mkdir创建目录
    '/data/db_tools/scripts'
    
    # <returns a list of all module functions> 
    >>> dir(os)
    ['CLD_CONTINUED', 'CLD_DUMPED', 'CLD_EXITED', 'CLD_TRAPPED', 'DirEntry', 'EX_CANTCREAT', 'EX_CONFIG', 'EX_DATAERR', 'EX_IOERR', 'EX_NOHOST', 'EX_NOINPUT', 'EX_NOPERM', 'EX_NOUSER', 'EX_OK', 'EX_OSERR', 'EX_OSFILE', 'EX_PROTOCOL', 'EX_SOFTWARE', 'EX_TEMPFAIL', 'EX_UNAVAILABLE', 'EX_USAGE', 'F_LOCK', 'F_OK', 'F_TEST', 'F_TLOCK', 'F_ULOCK', 'GRND_NONBLOCK', 'GRND_RANDOM', 'MutableMapping', 'NGROUPS_MAX', 'O_ACCMODE', 'O_APPEND', 'O_ASYNC', 'O_CLOEXEC', 'O_CREAT', 'O_DIRECT', 'O_DIRECTORY', 'O_DSYNC', 'O_EXCL', 'O_LARGEFILE', 'O_NDELAY', 'O_NOATIME', 'O_NOCTTY', 'O_NOFOLLOW', 'O_NONBLOCK', 'O_PATH', 'O_RDONLY', 'O_RDWR', 'O_RSYNC', 'O_SYNC', 'O_TMPFILE', 'O_TRUNC', 'O_WRONLY', 'POSIX_FADV_DONTNEED', 'POSIX_FADV_NOREUSE', 'POSIX_FADV_NORMAL', 'POSIX_FADV_RANDOM', 'POSIX_FADV_SEQUENTIAL', 'POSIX_FADV_WILLNEED', 'PRIO_PGRP', 'PRIO_PROCESS', 'PRIO_USER', 'P_ALL', 'P_NOWAIT', 'P_NOWAITO', 'P_PGID', 'P_PID', 'P_WAIT', 'PathLike', 'RTLD_DEEPBIND', 'RTLD_GLOBAL', 'RTLD_LAZY', 'RTLD_LOCAL', 'RTLD_NODELETE', 'RTLD_NOLOAD', 'RTLD_NOW', 'R_OK', 'SCHED_BATCH', 'SCHED_FIFO', 'SCHED_IDLE', 'SCHED_OTHER', 'SCHED_RESET_ON_FORK', 'SCHED_RR', 'SEEK_CUR', 'SEEK_DATA', 'SEEK_END', 'SEEK_HOLE', 'SEEK_SET', 'ST_APPEND', 'ST_MANDLOCK', 'ST_NOATIME', 'ST_NODEV', 'ST_NODIRATIME', 'ST_NOEXEC', 'ST_NOSUID', 'ST_RDONLY', 'ST_RELATIME', 'ST_SYNCHRONOUS', 'ST_WRITE', 'TMP_MAX', 'WCONTINUED', 'WCOREDUMP', 'WEXITED', 'WEXITSTATUS', 'WIFCONTINUED', 'WIFEXITED', 'WIFSIGNALED', 'WIFSTOPPED', 'WNOHANG', 'WNOWAIT', 'WSTOPPED', 'WSTOPSIG', 'WTERMSIG', 'WUNTRACED', 'W_OK', 'XATTR_CREATE', 'XATTR_REPLACE', 'XATTR_SIZE_MAX', 'X_OK', '_Environ', '__all__', '__builtins__', '__cached__', '__doc__', '__file__', '__loader__', '__name__', '__package__', '__spec__', '_execvpe', '_exists', '_exit', '_fspath', '_fwalk', '_get_exports_list', '_putenv', '_spawnvef', '_unsetenv', '_wrap_close', 'abc', 'abort', 'access', 'altsep', 'chdir', 'chmod', 'chown', 'chroot', 'close', 'closerange', 'confstr', 'confstr_names', 'cpu_count', 'ctermid', 'curdir', 'defpath', 'device_encoding', 'devnull', 'dup', 'dup2', 'environ', 'environb', 'errno', 'error', 'execl', 'execle', 'execlp', 'execlpe', 'execv', 'execve', 'execvp', 'execvpe', 'extsep', 'fchdir', 'fchmod', 'fchown', 'fdatasync', 'fdopen', 'fork', 'forkpty', 'fpathconf', 'fsdecode', 'fsencode', 'fspath', 'fstat', 'fstatvfs', 'fsync', 'ftruncate', 'fwalk', 'get_blocking', 'get_exec_path', 'get_inheritable', 'get_terminal_size', 'getcwd', 'getcwdb', 'getegid', 'getenv', 'getenvb', 'geteuid', 'getgid', 'getgrouplist', 'getgroups', 'getloadavg', 'getlogin', 'getpgid', 'getpgrp', 'getpid', 'getppid', 'getpriority', 'getrandom', 'getresgid', 'getresuid', 'getsid', 'getuid', 'getxattr', 'initgroups', 'isatty', 'kill', 'killpg', 'lchown', 'linesep', 'link', 'listdir', 'listxattr', 'lockf', 'lseek', 'lstat', 'major', 'makedev', 'makedirs', 'minor', 'mkdir', 'mkfifo', 'mknod', 'name', 'nice', 'open', 'openpty', 'pardir', 'path', 'pathconf', 'pathconf_names', 'pathsep', 'pipe', 'pipe2', 'popen', 'posix_fadvise', 'posix_fallocate', 'pread', 'putenv', 'pwrite', 'read', 'readlink', 'readv', 'remove', 'removedirs', 'removexattr', 'rename', 'renames', 'replace', 'rmdir', 'scandir', 'sched_get_priority_max', 'sched_get_priority_min', 'sched_getaffinity', 'sched_getparam', 'sched_getscheduler', 'sched_param', 'sched_rr_get_interval', 'sched_setaffinity', 'sched_setparam', 'sched_setscheduler', 'sched_yield', 'sendfile', 'sep', 'set_blocking', 'set_inheritable', 'setegid', 'seteuid', 'setgid', 'setgroups', 'setpgid', 'setpgrp', 'setpriority', 'setregid', 'setresgid', 'setresuid', 'setreuid', 'setsid', 'setuid', 'setxattr', 'spawnl', 'spawnle', 'spawnlp', 'spawnlpe', 'spawnv', 'spawnve', 'spawnvp', 'spawnvpe', 'st', 'stat', 'stat_float_times', 'stat_result', 'statvfs', 'statvfs_result', 'strerror', 'supports_bytes_environ', 'supports_dir_fd', 'supports_effective_ids', 'supports_fd', 'supports_follow_symlinks', 'symlink', 'sync', 'sys', 'sysconf', 'sysconf_names', 'system', 'tcgetpgrp', 'tcsetpgrp', 'terminal_size', 'times', 'times_result', 'truncate', 'ttyname', 'umask', 'uname', 'uname_result', 'unlink', 'unsetenv', 'urandom', 'utime', 'wait', 'wait3', 'wait4', 'waitid', 'waitid_result', 'waitpid', 'walk', 'write', 'writev']

    2、针对日常的文件和目录管理任务,:mod:shutil 模块提供了一个易于使用的高级接口:

    # python3.6
    Python 3.6.8 (default, Nov 16 2020, 16:55:22)
    [GCC 4.8.5 20150623 (Red Hat 4.8.5-44)] on linux
    Type "help", "copyright", "credits" or "license" for more information.
    >>>
    >>>
    >>>
    >>> import shutil
    >>> shutil.copyfile('/data/db_tools/scripts/yoon.sql','/data/db_tools/scripts/hank.sql')
    '/data/db_tools/scripts/hank.sql'
    
    >>> shutil.move('/data/db_tools/scripts/yoon.sql','/data/db_tools/scripts/hankyoon.sql')
    '/data/db_tools/scripts/hankyoon.sql'

    二、文件通配符
    glob模块提供了一个函数用于从目录通配符搜索中生成文件列表:

    # python3.6
    Python 3.6.8 (default, Nov 16 2020, 16:55:22)
    [GCC 4.8.5 20150623 (Red Hat 4.8.5-44)] on linux
    Type "help", "copyright", "credits" or "license" for more information.
    >>> import glob
    >>> glob.glob('*.sql')  # 当前目录/data/db_tools/scripts下的 .sql 文件
    ['hankyoon.sql']

    三、命令行参数
    通用工具脚本经常调用命令行参数。这些命令行参数以链表形式存储于 sys 模块的 argv 变量。例如在命令行中执行 "python demo.py one two three" 后可以得到以下输出结果:

    # cat demo.py
    #!/usr/bin/env python
    #coding:utf-8
    import sys
    import os
    hank=sys.argv[1]
    hank1=sys.argv[2]
    hank2=sys.argv[3]
    print(hank,hank1,hank2)
    
    # python3.6 demo.py one two three
    one two three

    四、错误输出重定向和程序终止

    sys 还有 stdin,stdout 和 stderr 属性,即使在 stdout 被重定向时,后者也可以用于显示警告和错误信息。
    >>> sys.stderr.write('Warning, log file not found starting a new one
    ')
    Warning, log file not found starting a new one
    
    大多脚本的定向终止都使用 "sys.exit()"

    五、字符串正则匹配
    1、re模块为高级字符串处理提供了正则表达式工具。对于复杂的匹配和处理,正则表达式提供了简洁、优化的解决方案:

    >>> import re
    >>> re.findall(r'f[a-z]*', 'which foot or hand fell fastest')
    ['foot', 'fell', 'fastest']
    >>> re.sub(r'([a-z]+) 1', r'1', 'cat in the the hat')
    'cat in the hat'

    2、如果只需要简单的功能,应该首先考虑字符串方法,因为它们非常简单,易于阅读和调试:

    >>> 'tea for too'.replace('too', 'two')
    'tea for two'

    六、数学
    1、math模块为浮点运算提供了对底层C函数库的访问:

    >>> import math
    >>> math.cos(math.pi / 4)
    0.70710678118654757
    >>> math.log(1024, 2)
    10.0

    2、random提供了生成随机数的工具:

    >>> import random
    >>> random.choice(['apple', 'pear', 'banana'])
    'apple'
    >>> random.sample(range(100), 10)   # sampling without replacement
    [30, 83, 16, 4, 8, 81, 41, 50, 18, 33]
    >>> random.random()    # random float
    0.17970987693706186
    >>> random.randrange(6)    # random integer chosen from range(6)
    4

    七、访问 互联网
    有几个模块用于访问互联网以及处理网络通信协议。其中最简单的两个是用于处理从 urls 接收的数据的 urllib.request 以及用于发送电子邮件的 smtplib:

    >>> from urllib.request import urlopen
    >>> for line in urlopen('http://tycho.usno.navy.mil/cgi-bin/timer.pl'):
    ...     line = line.decode('utf-8')  # Decoding the binary data to text.
    ...     if 'EST' in line or 'EDT' in line:  # look for Eastern Time
    ...         print(line)
    
    <BR>Nov. 25, 09:43:32 PM EST
    
    >>> import smtplib
    >>> server = smtplib.SMTP('localhost')
    >>> server.sendmail('soothsayer@example.org', 'jcaesar@example.org',
    ... """To: jcaesar@example.org
    ... From: soothsayer@example.org
    ...
    ... Beware the Ides of March.
    ... """)
    >>> server.quit()
    注意第二个例子需要本地有一个在运行的邮件服务器。
    #处理get请求,不传data,则为get请求
    
    import urllib
    from urllib.request import urlopen
    from urllib.parse import urlencode
    
    url='http://www.xxx.com/login'
    data={"username":"admin","password":123456}
    req_data=urlencode(data)#将字典类型的请求数据转变为url编码
    res=urlopen(url+'?'+req_data)#通过urlopen方法访问拼接好的url
    res=res.read().decode()#read()方法是读取返回数据内容,decode是转换返回数据的bytes格式为str
    
    print(res)
    #处理post请求,如果传了data,则为post请求
    
    import urllib
    from urllib.request import Request
    from urllib.parse import urlencode
    
    url='http://www.xxx.com/login'
    data={"username":"admin","password":123456}
    data=urlencode(data)#将字典类型的请求数据转变为url编码
    data=data.encode('ascii')#将url编码类型的请求数据转变为bytes类型
    req_data=Request(url,data)#将url和请求数据处理为一个Request对象,供urlopen调用
    with urlopen(req_data) as res:
        res=res.read().decode()#read()方法是读取返回数据内容,decode是转换返回数据的bytes格式为str
    
    print(res)

    八、日期和时间
    datetime模块为日期和时间处理同时提供了简单和复杂的方法。
    支持日期和时间算法的同时,实现的重点放在更有效的处理和格式化输出。
    该模块还支持时区处理:

    >>> # dates are easily constructed and formatted
    >>> from datetime import date
    >>> now = date.today()
    >>> now
    datetime.date(2003, 12, 2)
    >>> now.strftime("%m-%d-%y. %d %b %Y is a %A on the %d day of %B.")
    '12-02-03. 02 Dec 2003 is a Tuesday on the 02 day of December.'
    
    >>> # dates support calendar arithmetic
    >>> birthday = date(1964, 7, 31)
    >>> age = now - birthday
    >>> age.days
    14368

    时间和日期补充
    常用时间处理方法

    今天 today = datetime.date.today()
    昨天 yesterday = today - datetime.timedelta(days=1)
    上个月 last_month = today.month - 1 if today.month - 1 else 12
    当前时间戳 time_stamp = time.time()
    时间戳转datetime datetime.datetime.fromtimestamp(time_stamp)
    datetime转时间戳 int(time.mktime(today.timetuple()))
    datetime转字符串 today_str = today.strftime("%Y-%m-%d")
    字符串转datetime today = datetime.datetime.strptime(today_str, "%Y-%m-%d")
    补时差 today + datetime.timedelta(hours=8)

    九、数据压缩
    以下模块直接支持通用的数据打包和压缩格式:zlib,gzip,bz2,zipfile,以及 tarfile。

    >>> import zlib
    >>> s = b'witch which has which witches wrist watch'
    >>> len(s)
    41
    >>> t = zlib.compress(s)
    >>> len(t)
    37
    >>> zlib.decompress(t)
    b'witch which has which witches wrist watch'
    >>> zlib.crc32(s)
    226805979

    十、性能度量
    有些用户对了解解决同一问题的不同方法之间的性能差异很感兴趣。Python 提供了一个度量工具,为这些问题提供了直接答案。
    例如,使用元组封装和拆封来交换元素看起来要比使用传统的方法要诱人的多,timeit 证明了现代的方法更快一些。

    >>> from timeit import Timer
    >>> Timer('t=a; a=b; b=t', 'a=1; b=2').timeit()
    0.57535828626024577
    >>> Timer('a,b = b,a', 'a=1; b=2').timeit()
    0.54962537085770791
    相对于 timeit 的细粒度,:mod:profile 和 pstats 模块提供了针对更大代码块的时间度量工具。

    十一、测试模块
    开发高质量软件的方法之一是为每一个函数开发测试代码,并且在开发过程中经常进行测试
    doctest模块提供了一个工具,扫描模块并根据程序中内嵌的文档字符串执行测试。
    测试构造如同简单的将它的输出结果剪切并粘贴到文档字符串中。
    通过用户提供的例子,它强化了文档,允许 doctest 模块确认代码的结果是否与文档一致:

    def average(values):
        """Computes the arithmetic mean of a list of numbers.
    
        >>> print(average([20, 30, 70]))
        40.0
        """
        return sum(values) / len(values)
    
    import doctest
    doctest.testmod()   # 自动验证嵌入测试
    
    unittest模块不像 doctest模块那么容易使用,不过它可以在一个独立的文件里提供一个更全面的测试集:
    import unittest
    
    class TestStatisticalFunctions(unittest.TestCase):
    
        def test_average(self):
            self.assertEqual(average([20, 30, 70]), 40.0)
            self.assertEqual(round(average([1, 5, 7]), 1), 4.3)
            self.assertRaises(ZeroDivisionError, average, [])
            self.assertRaises(TypeError, average, 20, 30, 70)
    
    unittest.main() # Calling from the command line invokes all tests
  • 相关阅读:
    request.getParameter() 、 request.getInputStream()和request.getReader() 使用体会
    HTTP之Content-Length
    关于spring3中No Session found for current thread!and Transaction的配置和管理(转)
    Java数据类型和MySql数据类型对应一览
    Spring MVC 解读——View,ViewResolver(转)
    LeetCode 441. Arranging Coins
    LeetCode 415. Add Strings
    LeetCode 400. Nth Digit
    LeetCode 367. Valid Perfect Square
    LeetCode 326. Power of Three
  • 原文地址:https://www.cnblogs.com/hankyoon/p/14799160.html
Copyright © 2011-2022 走看看