python標準庫

文本處理

  • string: 提供了字符集:ascii_lowercase, ascii_uppercase, digits, hexdigits, punctuation
  • re: 正則表達式支持(pattern, string):match, search, findall, sub, split, finditer

數據結構

  • datetime: 處理日期,建議用arrow代替
  • calendar: 日曆: Calendar
  • collections: 其餘數據結構: deque, Counter, defaultdict, namedtuple
  • heapq: 堆排序實現: nlargest, nsmallest, merge
  • bisect: 二分查找實現: bisect, insort
  • array: 數列實現: array
  • copy: 淺拷貝和深拷貝: copy, deepcopy
  • pprint: 漂亮地輸出文本: pprint
  • enum: 枚舉類的實現: Enum

數學

  • math: 數學函數庫,函數太多故不一一列舉
  • fractions: 分數運算: Fraction as F
  • random: 隨機數: choice, randint, randrange, sample, shuffle, gauss
  • statistics: 統計學函數: mean, median, mode, variance

函數式編程

  • itertools: 迭代器工具: permutations, combinations, product, chain, repeat, cycle, accumulate
  • functools: 函數工具: @wraps, reduce, partial, @lrucache, @singledispatch
  • operator: 基本運算符

文件目錄訪問

  • pathlib: 對路徑對象進行操做,完美替代os.path: Path
  • fileinput: 讀取一個或多個文件並處理行: input
  • filecmp: 比較兩個文件是否相同: cmp
  • tempfile:用來建立臨時文件,一關閉就自動刪除:TemporaryFile
  • linecache: 讀取文件的行,緩存優化: getline, getlines
  • shutil: 文件操做: copy, copytree, rmtree, move, make_archive

數據持久化

  • pickle: 文件pickle序列化: dump, dumps, load, loads
  • sqlite3: sqlite數據庫接口

文件格式

  • csv: 處理csv文件: reader, writeheader, writerow
  • configparser: 處理配置文件: ConfigParser, get, sections

密碼學

  • hashlib: 哈希加密算法: sha256, hexdigest
  • secrets:密鑰生成:token_bytes, token_hex, token_urlsafe

操做系統

  • os: 操做系統,具體看文檔
  • io: 在內存中讀寫str和bytes: StringIO, BytesIO, write, get_value
  • time: 計時器: time, sleep, strftime
  • argparse, getopt: 命令行處理,建議用click或docopt代替
  • logging: 打日誌: debug, info, warning, error, critical,建議用loguru代替
  • getpass: 獲取用戶輸入的密碼: getpass
  • platform: 提供跨平臺支持: uname, system

併發執行

  • threading: 多線程模型: Thread, start, join
  • multiprocessing: 多進程模型: Pool, map, Process
  • concurrent.futures: 異步執行模型: ThreadPoolExecutor, ProcessPoolExecutor
  • subprocess: 子進程管理: run
  • sched: 調度工具,建議用schedule代替
  • queue: 同步隊列: Queue

進程間通訊和網絡

  • asyncio: 異步IO, eventloop, 協程: get_event_loop, run_until_complete, wait, async和await關鍵字

網絡數據處理

  • email: 處理email,建議用yagmail代替
  • json: 處理json: dumps, loads
  • base64: 處理base64編碼: b64encode, b64decode

結構化標記語言工具

  • html: 轉義html: escape, unescape
  • html.parser: 解析html,建議用parsel代替

網絡協議支持

  • webbrowser: 打開瀏覽器: open
  • wsgiref: 實現WSGI接口
  • uuid: 通用惟一識別碼: uuid1, uuid3, uuid4, uuid5
  • ftplib, poplib, imaplib, nntplib, smptlib, telnetlib: 實現各類網絡協議

其他庫用requests代替html

程序框架

  • turtle: 畫圖工具
  • cmd: 實現交互式shell
  • shlex: 利用shell的語法分割字符串: split

GUI

  • tkinter:可用pysimplegui代替,超好用

開發工具

  • typing: 類型註解,可配合mypy對項目進行靜態類型檢查
  • pydoc: 查閱模塊文檔: python -m pydoc [name]
  • doctest: 文檔測試: python -m doctest [pyfile]
  • unittest: 單元測試: python -m unittest [pyfile]

DEBUG和性能優化

  • pdb: Python Debugger: python -m pdb [pyfile]
  • cProfile: 分析程序性能: python -m cProfile [pyfile]
  • timeit: 檢測代碼運行時間: python -m timeit [pyfile]

軟件打包發佈

  • setuptools: 編寫setup.py專用: setup, find_packages
  • venv: 建立虛擬環境,建議用pipenv代替

Python運行時服務

  • sys: 系統環境交互: argv, path, exit, stderr, stdin, stdout
  • builtins: 全部的內置函數和類,默認引進(還有一個boltons擴充了許多有用的函數和類)
  • __main__: 頂層運行環境,使得python文件既能夠獨立運行,也能夠當作模塊導入到其餘文件。
  • warnings: 警告功能(代碼過期等): warn
  • contextlib: 上下文管理器實現: @contextmanager
  • inspect:用於獲取對象的各類信息(自省)

自定義Python解釋器

  • code: 實現自定義的Python解釋器(好比Scrapy的shell): interact
相關文章
相關標籤/搜索