欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Python標(biāo)準(zhǔn)庫中隱藏的利器(示例詳解)

 更新時(shí)間:2023年11月13日 09:10:32   作者:wang_yb  
在命令行中直接使用Python標(biāo)準(zhǔn)庫的模塊,最大的好處就是就是不用寫代碼,就能使用其中的功能,當(dāng)臨時(shí)需要一些某些功能的時(shí)候,用這種方式會(huì)快捷,方便很多,這篇文章主要介紹了Python標(biāo)準(zhǔn)庫中隱藏的利器,需要的朋友可以參考下

Python安裝之后,其標(biāo)準(zhǔn)庫中有的模塊,不一定要通過代碼來引用,還可以直接在命令行中使用的。

在命令行中直接使用Python標(biāo)準(zhǔn)庫的模塊,最大的好處就是就是不用寫代碼,就能使用其中的功能,當(dāng)臨時(shí)需要一些某些功能的時(shí)候,用這種方式會(huì)快捷,方便很多。

1. 命令行中使用模塊

命令行中使用python標(biāo)準(zhǔn)庫的模塊,一般格式如下:

python -m <mod-name> <options>

其中,mod-name 是模塊的名稱;options 是模塊的參數(shù)。

本篇列舉的是我自己在命令行中常用的一些模塊,并不是所有可在命令行中可用的模塊。
其它好用的模塊,歡迎大家推薦。

2. http.server:靜態(tài)文件服務(wù)

http.server 模塊的參數(shù)主要有:

python -m http.server -h
usage: server.py [-h] [--cgi] [-b ADDRESS] [-d DIRECTORY] [-p VERSION] [port]
positional arguments:
  port                  bind to this port (default: 8000)
options:
  -h, --help            show this help message and exit
  --cgi                 run as CGI server
  -b ADDRESS, --bind ADDRESS
                        bind to this address (default: all interfaces)
  -d DIRECTORY, --directory DIRECTORY
                        serve this directory (default: current directory)
  -p VERSION, --protocol VERSION
                        conform to this HTTP version (default: HTTP/1.0)

主要的參數(shù):

  • -b:如果需要讓局域網(wǎng)的其他機(jī)器訪問,可以設(shè)置 -b 0.0.0.0
  • -d:設(shè)置靜態(tài)文件服務(wù)的根目錄

創(chuàng)建一個(gè)目錄作為靜態(tài)文件服務(wù)的根目錄,并放入一個(gè)index.html文件。
html文件內(nèi)容:

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>http.server</title>
  </head>
  <body>
    <h1>hello</h1>
    <br />
    <h1>python -m http.server</h1>
  </body>
</html>

啟動(dòng)服務(wù),效果如下:

python -m http.server -d ./sample-site

3. gzip:壓縮/解壓縮

gzip模塊可用來壓縮,解壓縮文件。

python -m gzip -h
usage: gzip.py [-h] [--fast | --best | -d] [file ...]
A simple command line interface for the gzip module: act like gzip, but do not delete the input file.
positional arguments:
  file
options:
  -h, --help        show this help message and exit
  --fast            compress faster
  --best            compress better
  -d, --decompress  act like gunzip instead of gzip

壓縮文件:

python -m gzip test.txt
# 會(huì)生成一個(gè) test.txt.gz 文件

解壓文件(-d 參數(shù)用來解壓):

python -m gzip -d test.txt.gz
# 會(huì)解壓出 test.txt 文件

4. base64:編碼解碼文件

當(dāng)我們臨時(shí)要用base64來編碼或解碼字符串的時(shí)候,可以用這個(gè)模塊。

python -m base64 -h
usage: D:\miniconda3\envs\databook\Lib\base64.py [-h|-d|-e|-u|-t] [file|-]
        -h: print this help message and exit
        -d, -u: decode
        -e: encode (default)
        -t: encode and decode string 'Aladdin:open sesame'

base64編碼一個(gè)字符串:

echo "abcdefg" | python -m base64
YWJjZGVmZw0K

解碼base64字符串:用上面編碼后的base64來還原。

echo "YWJjZGVmZw0K" | python -m base64 -d
abcdefg

5. json.tool:更好的顯示json結(jié)構(gòu)

這個(gè)工具對(duì)于經(jīng)常使用命令行的人來說,非常有用。
從命令行訪問某些API接口的時(shí)候,返回的json數(shù)據(jù)往往是壓縮在一行,很難閱讀。

json.tool模塊的參數(shù)很多,但是一般大部分情況下是不需要設(shè)置的,
使用參數(shù)的默認(rèn)值就可以了:

python -m json.tool -h
usage: python -m json.tool [-h] [--sort-keys] [--no-ensure-ascii] [--json-lines]
                           [--indent INDENT | --tab | --no-indent | --compact]
                           [infile] [outfile]
A simple command line interface for json module to validate and pretty-print JSON objects.
positional arguments:
  infile             a JSON file to be validated or pretty-printed
  outfile            write the output of infile to outfile
options:
  -h, --help         show this help message and exit
  --sort-keys        sort the output of dictionaries alphabetically by key
  --no-ensure-ascii  disable escaping of non-ASCII characters
  --json-lines       parse input using the JSON Lines format. Use with --no-indent or --compact to produce valid
                     JSON Lines output.
  --indent INDENT    separate items with newlines and use this number of spaces for indentation
  --tab              separate items with newlines and use tabs for indentation
  --no-indent        separate items with spaces rather than newlines
  --compact          suppress all whitespace separation (most compact)

比如下面的json字符串:

echo '{"code":0,"message":"success""data":[{"name":"harry"},{"name":"joe"}]}' | python -m json.tool
{
    "code": 0,
    "message": "success",
    "data": [
        {
            "name": "harry"
        },
        {
            "name": "joe"
        }
    ]
}

6. calendar:日歷信息

calendar這個(gè)模塊相當(dāng)于是個(gè)命令行下的日歷。
可以指定某一年的日歷(默認(rèn)是當(dāng)前年的):

python -m calendar 2022

也可以指定某一年某個(gè)月的日歷:

python -m calendar 2023 10

這個(gè)命令還可以把日歷轉(zhuǎn)換成HTML格式導(dǎo)出,具體可以看它的help信息。

7. ast:顯示代碼的抽象語法數(shù)

這個(gè)ast模塊就強(qiáng)大了,它可以將原始的python代碼轉(zhuǎn)換為抽象語法樹。
基于抽象語法樹,可以做一些底層的代碼分析,代碼生成,甚至轉(zhuǎn)換成其它語言的代碼等等。

ast模塊的參數(shù)不多,一般用默認(rèn)參數(shù)即可:

python -m ast -h
usage: python -m ast [-h] [-m {exec,single,eval,func_type}] [--no-type-comments] [-a] [-i INDENT] [infile]
positional arguments:
  infile                the file to parse; defaults to stdin
options:
  -h, --help            show this help message and exit
  -m {exec,single,eval,func_type}, --mode {exec,single,eval,func_type}
                        specify what kind of code must be parsed
  --no-type-comments    don't add information about type comments
  -a, --include-attributes
                        include attributes such as line numbers and column offsets
  -i INDENT, --indent INDENT
                        indentation of nodes (number of spaces)

下面構(gòu)造一個(gè)python代碼文件(main.py),內(nèi)容比較簡單,就是一個(gè)累加的功能。

# -*- coding: utf-8 -*-
def sum(start, end):
    sum = 0
    for i in range(start, end + 1):
        sum += i
    print("1+2+...+10 = {}".format(sum))
if __name__ == "__main__":
    sum(1, 10)

轉(zhuǎn)換之后的抽象語法樹為:

python -m ast main.py
Module(
   body=[
      FunctionDef(
         name='sum',
         args=arguments(
            posonlyargs=[],
            args=[
               arg(arg='start'),
               arg(arg='end')],
               ...省略...
         body=[
            Assign(
               targets=[
                  Name(id='sum', ctx=Store())],
               value=Constant(value=0)),
            For(
               target=Name(id='i', ctx=Store()),
               ...省略...
               orelse=[]),
            Expr(
               value=Call(
               ...省略...
                  keywords=[]))],
         decorator_list=[]),
      If(
         test=Compare(
               ...省略...
         orelse=[])],
   type_ignores=[])

轉(zhuǎn)換后的內(nèi)容比較長,中間我省略一些內(nèi)容。
對(duì)完整的內(nèi)容感興趣,可以自己試試轉(zhuǎn)換一個(gè)python代碼文件。

到此這篇關(guān)于Python標(biāo)準(zhǔn)庫中隱藏的利器的文章就介紹到這了,更多相關(guān)Python標(biāo)準(zhǔn)庫內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • python實(shí)現(xiàn)圖書館搶座(自動(dòng)預(yù)約)功能的示例代碼

    python實(shí)現(xiàn)圖書館搶座(自動(dòng)預(yù)約)功能的示例代碼

    這篇文章主要介紹了python實(shí)現(xiàn)圖書館搶座(自動(dòng)預(yù)約)功能,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2020-09-09
  • Python中的?pass?占位語句

    Python中的?pass?占位語句

    這篇文章主要介紹了Python中的?pass?占位語句,Python?pass是空語句,是為了保持程序結(jié)構(gòu)的完整性,下文具體的相關(guān)內(nèi)容介紹需要的小伙伴可以參考一下
    2022-04-04
  • Django如何簡單快速實(shí)現(xiàn)PUT、DELETE方法

    Django如何簡單快速實(shí)現(xiàn)PUT、DELETE方法

    這篇文章主要介紹了Django如何簡單快速實(shí)現(xiàn)PUT、DELETE方法,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2019-07-07
  • Python導(dǎo)入模塊時(shí)遇到的錯(cuò)誤分析

    Python導(dǎo)入模塊時(shí)遇到的錯(cuò)誤分析

    這篇文章主要給大家詳細(xì)解釋了在Python處理導(dǎo)入模塊的時(shí)候出現(xiàn)錯(cuò)誤以及具體的情況分析,非常的詳盡,有需要的小伙伴可以參考下
    2017-08-08
  • Python Queue模塊詳解

    Python Queue模塊詳解

    這篇文章主要介紹了Python Queue模塊詳解,需要的朋友可以參考下
    2014-11-11
  • 一文詳解Python中的zip函數(shù)

    一文詳解Python中的zip函數(shù)

    在Python中,處理數(shù)據(jù)時(shí)經(jīng)常需要同時(shí)遍歷多個(gè)序列,zip函數(shù)提供了一種簡潔的方式來組合這些序列,它用于將多個(gè)可迭代對(duì)象(如列表、元組等)的元素配對(duì),本文將給大家詳細(xì)介紹一下Python中的zip函數(shù),需要的朋友可以參考下
    2024-05-05
  • python實(shí)現(xiàn)KNN近鄰算法

    python實(shí)現(xiàn)KNN近鄰算法

    這篇文章主要介紹了python實(shí)現(xiàn)KNN近鄰算法的方法,幫助大家更好的利用python進(jìn)行機(jī)器學(xué)習(xí),感興趣的朋友可以了解下
    2020-12-12
  • Django多對(duì)多ManyToManyField字段的使用

    Django多對(duì)多ManyToManyField字段的使用

    Django是一個(gè)支持多對(duì)多關(guān)系的Web框架,可以在模型中定義多對(duì)多關(guān)系,本文主要介紹了Django多對(duì)多ManyToManyField字段的使用,感興趣的可以了解一下
    2023-12-12
  • python回調(diào)函數(shù)用法實(shí)例分析

    python回調(diào)函數(shù)用法實(shí)例分析

    這篇文章主要介紹了python回調(diào)函數(shù)用法,較為詳細(xì)的分析了常用的調(diào)用方式,并實(shí)例介紹了Python回調(diào)函數(shù)的使用技巧,需要的朋友可以參考下
    2015-05-05
  • Python使用moviepy讀取字幕srt文件報(bào)錯(cuò)的解決方法詳解

    Python使用moviepy讀取字幕srt文件報(bào)錯(cuò)的解決方法詳解

    這篇文章主要為大家詳細(xì)介紹了Python使用moviepy讀取字幕srt文件報(bào)錯(cuò)‘gbk‘?codec?can‘t?decode的兩種解決辦法,有需要的小伙伴可以跟隨小編一起學(xué)習(xí)一下
    2024-01-01

最新評(píng)論