1.imp模塊==========>重新加載已加載過的模塊方法
import imp imp.reload(mymod) # 重新加載已經加載過的mymod模塊
2.ctypes模塊=======>加載動態庫方法
from ctypes # 加載由C語言編譯的libdead_loop.so動態庫檔案,并回傳一個動態庫物件 lib = ctypes.cdll.LoadLibrary("libdead_loop.so") lib.DeadLoop() # 可以直接執行C語言編譯后的動態庫中的函式
3.copy模塊=========>淺拷貝和深拷貝方法
import copy # 匯入copy模塊 li = [3.1, 3.2] li1 = [1, 2, li] # [1, 2, [3.1, 3.2]] li2 = copy.copy(li1) # [1, 2, [3.1, 3.2]]淺拷貝 id(li2[2]) == id(li1[2]) # True li3 = copy.deepcopy(li1) # [1, 2, [3.1, 3.2]]深拷貝 id(li3[2]) == id(li1[2]) # False
4.contextlib模塊===>背景關系管理裝飾器方法
from contextlib import contextmanager @contextmanager # 裝飾后保證了使用with管理my_open函式執行時產生例外一定會呼叫close,同理可可以裝飾類 def my_open(path, mode): f = open(path, mode) # 通過 yield 將函式分割成兩部分,yield 之前的陳述句在 __enter__ 方法中執行 # yield 之后的陳述句在 __exit__ 方法中執行,緊跟在 yield 后面的值是函式的回傳值 yield f f.close() with my_open('out.txt', 'w') as f: f.write("hello , the simplest context manager")
5.urllib模塊=======>url編解碼方法
import urllib.parse # Python3 url編碼 print(urllib.parse.quote("回應頭 回應體")) # %E5%93%8D%E5%BA%94%E5%A4%B4%20%E5%93%8D%E5%BA%94%E4%BD%93 # Python3 url解碼 print(urllib.parse.unquote("%E5%93%8D%E5%BA%94%E5%A4%B4%20%E5%93%8D%E5%BA%94%E4%BD%93")) # 回應頭 回應體
6.traceback模塊====>更專業的列印例外資訊方法
import traceback a = 10 try: b = a / '2' except TypeError: traceback.print_exc() print("---end---") """執行結果 Traceback (most recent call last): File "<ipython-input-1-929daf19ab90>", line 5, in <module> b = a / '2' TypeError: unsupported operand type(s) for /: 'int' and 'str' ---end--- """
7.keyword模塊======>串列形式列出Python中的所有關鍵字方法
import keyword print(keyword.kwlist) # 回傳一個包含Python關鍵字的串列
8.types模塊========>判斷一個物件是方法還是函式
from types import FunctionType from types import MethodType class MyClass: def test(self): print("方法被呼叫") def func(): print("函式被呼叫") my_obj = MyClass() # 判斷一個物件是否是函式 print(isinstance(func,FunctionType)) # True # 類名.方法名是函式 print(isinstance(MyClass.test,FunctionType)) # True # 判斷一個物件是否是函式 print(isinstance(obj.test,FunctionType)) # False # 實體.方法名是方法 print(isinstance(obj.test,MethodType)) # True
9.getpass模塊======>隱式輸入密碼方法
In [1]: import getpass In [2]: passwd = getpass.getpass() # 在輸入時會有密碼輸入提示,且加密輸入 Password: In [3]: passwd1 = getpass.getpass("確認密碼") # 可以指定提示語 確認密碼: In [4]: passwd, passwd1 Out[4]: ('123456', '123456')
10.functools模塊===>對引數序列中元素進行累積方法
from functools import reduce num1 = [1, 2, 3, 4, 5] # reduce方法遍歷合并可迭代物件中的每個元素 msg = reduce(lambda i, j: i + j, num1, 10) print(msg) # 25
11.subprocess模塊==>執行系統命令方法
import subprocess # 語法: res = subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE, stderr=subprocess.subprocess.PIPE) # cmd: 代表系統命令 # shell=True: 代表這條命令是系統命令,告訴作業系統將cmd當做系統命令去執行 # stdout: 是執行系統命令后用于保存正確結果的一個管道 # stderr: 是執行系統命令后用于保存錯誤結果的一個管道 r = subprocess.Popen("pwd", shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE) print("正確的回傳結果是: %s" % r.stdout.read().decode("utf-8")) # 正確的回傳結果是: /Users/tangxuecheng r = subprocess.Popen("pwds", shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE) print("錯誤的回傳結果是: %s" % r.stderr.read().decode("utf-8")) # 錯誤的回傳結果是: /bin/sh: pwds: command not found
12.struct模塊======>將一個不定長的int資料打包成一個固定4位的bytes型別方法
import struct b_len = struct.pack("i", 30) print(b_len) # b'\x1e\x00\x00\x00' b_len1 = struct.pack("i", 3000) print(b_len1) # b'\xb8\x0b\x00\x00' num1 = struct.unpack("i", b_len) print(num1) # (30,) num2 = struct.unpack("i", b_len1) print(num2) # (3000,)
13.hmac模塊========>md5加密演算法方法
import hmac md5_obj = hmac.new("鹽".encode("utf-8"), "用戶密碼".encode("utf-8")) r = md5_obj.digest() print(r) # b'\xa5\xc4#\x19\xa4\xae\x83\x887m\xa5\xaeC\x0c\xc6j'
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/47280.html
標籤:Python
