Python-01基礎-13功能模塊

Python 基礎-13 功能模塊

Python2 和 Python3 並存

Python3 安裝配置

參考鏈接:
https://www.cnblogs.com/gaoyuechen/p/8006365.html

安裝完成後自帶 pip 等

# 下載包
wget https://www.python.org/ftp/python/3.6.0/Python-3.6.0.tgz
# 解壓
tar xf Python-3.6.0.tgz
# 配置安裝信息
 ./configure --prefix=/usr/local/python3/
# 編譯
make && make install
# 配置環境變量
新建文件
vim /etc/profile.d/python3.sh
export PATH=$PATH:/usr/local/python3/bin/
執行一下下面命令
export PATH=$PATH:/usr/local/python3/bin/
# 驗證
python3
Python 3.6.0 (default, Feb  1 2017, 14:56:52)
[GCC 4.8.5 20150623 (Red Hat 4.8.5-11)] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>>

pip3 安裝配置

env3 安裝配置

判斷數據類型

第一種:types
import types
type(x) is types.IntType # 判斷是否 int 類型
type(x) is types.StringType # 判斷是否 string 類型

第二種: 超級噁心的模式,不推薦
type(x) == type(1) # 判斷是否 int 類型
type(x) == type(“1”) # 判斷是否 string 類型

第三種: isinstance
isinstance(對象,類型名或類型列表或元祖) --> True/False
例如:isinstance(“lst”,(int,str,list)) # True
判斷類型 “lst” 在 類型列表中

Python 代碼建議

from .. import
優先使用import a  使用a.B
有節制使用from a import B
避免使用 from a import *

Python -m xxx.py -m 使得一個模塊像腳本一樣運行
name
package

Python 的* __ xx的區別
①*函數名 意味着,此函數用於類內部,不屬於 api ,等同於私有函數
函數名 ,用來避免子類覆蓋其內容,意味着此函數不能被重寫,繼承等。僅僅用於類的內部使用,
xx函數,用於 Python 調用
使用_one_underline 來表示該方法或屬性是私有的,不屬於 API;
當創建一個用於 python 調用或一些特殊情況時,使用
two_underline**;
使用**just_to_underlines,來避免子類的重寫!

使用 join 連接字符 ,join 連接字符更高高效比+ ,尤其是大規模的字符串連接
join 使用:
‘xx’.join([str1,str2]) = str1xxstr2
‘xx’.join([str1,str2,str3]) = str1xxstr2xxstr3

時間測試,代碼的性能分析
import timeit

生成測試所需的字符數組

格式化字符串優先使用.format,而不是%s 等

.format 的使用方法 1.位置符號
" select _ from {0} where 1=1 {2} ".format(xx,yy) 2.使用名稱
" select _ from {xx} where 1=1 {yy} ".format(xx=1,yy=1) 3.同過屬性
xx = 1
yy = 2
" select * from {xx} where 1=1 {yy} ".format 4.格式化元組的具體項
point = (1,3)
‘x:{0[0]}; y:{0[1]}’.format(point)
.format 的優勢
使用靈活,可以作爲參數傳遞,簡便直觀,%s 處理需要注意被格式化字符的格式而。format 不需要

通過字符串調用對象屬性

python 通過字符串調用對象屬性或方法的實例講解
有時候需要將屬性或方法作爲參數傳入,這個時候可以通過以下幾種方式用字符串調用對象屬性或方法

1.eval
In [634]: def getmethod(x,char='just for test'):
  ...:  return eval('str.%s' % x)(char)
  ...:
In [635]: getmethod('upper')
Out[635]: 'JUST FOR TEST'
2getattr
In [650]: def getmethod2(x, char='just for test'):
  ...:  return getattr(char, x)()
  ...:
In [651]: getmethod2('upper')
Out[651]: 'JUST FOR TEST'

獲取目錄下所有文件名稱

file_dir = "sss"
for root, dirs, files in os.walk(file_dir):
  print(root) #當前目錄路徑
  print(dirs) #當前路徑下所有子目錄
  print(files) #當前路徑下所有非目錄子文件
  for filename in files:
    file_path = os.path.join(file_dir,filename)
    print file_path

uuid [唯一標識符]

UUID: 通用唯一標識符 ( Universally Unique Identifier ),對於所有的 UUID 它可以保證在空間和時間上的唯一性.

它是通過 MAC 地址、 時間戳、 命名空間、 隨機數、 僞隨機數來保證生成 ID 的唯一性,,有着固定的大小( 128 bit 位 ),通常由 32 字節的字符串(十六進制)表示。

它的唯一性和一致性特點,使得可以無需註冊過程就能夠產生一個新的 UUID;
UUID 可以被用作多種用途, 既可以用來短時間內標記一個對象,也可以可靠的辨別網絡中的持久性對象。

import uuid
# uuid.uuid1 基於時間戳
uuid.uuid1([node[, clock_seq]])
node - 默認主機的硬件地址
clock_seq 默認隨機14位序列號
# uuid.uuid3  基於名字的MD5散列值
通過計算名字和命名空間的MD5散列值得到,保證了同一命名空間中不同名字的唯一性,
和不同命名空間的唯一性,但同一命名空間的同一名字生成相同的uuid。
uuid.uuid3(namespace, name)
# uuid.uuid4 基於隨機數
由僞隨機數得到,有一定重複概率,可以計算得到
# uuid.uuid5() 基於名稱的SHA-1散列值
使用 Secure Hash Algorithm 1 算法

總結:

  • 分佈式環境: 建議 uuid1
  • 名字唯一要求:建議 uuid3/uuid5

文字處理

Levenshtein 文字距離

import Levenshtein
str1 = ‘qwer1235’
str2 = ‘qwe1235r’

計算漢明距離,要求 str1 和 str2 必須長度一致。是描述兩個等長字串之間對應位置上不同字符的個數
Levenshtein.hamming(str1, str2) #

Levenshtein.distance(str1,str2)計算編輯距離。是描述一個字符串轉化成另一個字串最少的操作次數,在其中的操作包括插入、刪除、替換。
Levenshtein.distance(str1, str2)

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章