Python的冷技巧小技巧

2021-07-10 07:46:11 字數 3296 閱讀 7815

記得剛開始工作的時候,老大給我們上 c++ 基礎課,告訴我們字串字面量可以換行(如下**),感覺真是如夢如幻。

[python]view plain

copy

#include 

int main(int argc, char** argv)    

輸出:

[plain]view plain

copy

hello world.  

後來在寫了很久的 python 以後,才知道 python 其實也可以的:

[python]view plain

copy

>>> t = ('hello'

... ' '

... 'world')  

>>> t  

'hello world'

這個特性很有用,能夠把超長的**優雅地分為幾行。記得以前在拼 sql 語言、寫日誌條目的時候總為**行長度超過 78 感到糾結(見我們的程式設計規範:

現在沒有壓力啦。

在寫 absolute32(見:的測試**的時候,為了讓測試**相容 python2.x/3.x 兩大版本,引入了一砣醜陋的**:

[python]view plain

copy

if sys.version < '3':  

exec("chinese = unicode('賴勇浩', 'utf-8')")  

else:  

exec("chinese = '賴勇浩'")  

這是因為在 python2.x 中

[python]view plain

copy

chinese = '賴勇浩'

的編碼不是 unicode 的,而在 python3.x 中取消了字串字面量的字首 u,所以

[python]view plain

copy

chinese = u'賴勇浩'

又直接語法錯誤,當時只好寫下了 exec 的**根據不同的版本來進行編譯。後來才知道 python2.6 中引入了 unicode_literals,可以很方便地寫 2.x/3.x 相容的**:

[python]view plain

copy

>>> x = '中國'

>>> x  

'\xe4\xb8\xad\xe5\x9b\xbd'

>>> from __future__ import unicode_literals  

>>> y = '中國'

>>> y  

u'\u4e2d\u56fd'

這樣,我那砣醜**也可以美化掉啦!

先來看兩句**

[python]view plain

copy

record = cursor.execute('select * from tbl where id = 123456').fecth_all()[0]  

msg = struct.unpack('!i', buff)[0]  

[python]view plain

copy

record, = cursor.execute('select * from tbl where id = 123456').fecth_all()  

msg, = struct.unpack('!i', buff)  

注意 record 和 msg 後面的逗號,可以利用它讓 python 把這個賦值語句理解為多對多的賦值,但可省去後續的 [0] 了。

這種寫法,據我以前的同事測試,比使用 [0] 要慢上 20% 左右。如果你很在意,建議使用之前的寫法。另外,兩種寫法到底哪個更漂亮、更 pythonic,也沒有定論,但我喜歡後者。

用 configpaser 是解釋 ini 檔案的利器,因為有這麼好用庫,所以我寫的專案基本上都是用 ini 檔案來配置的。configparser 支援 default 節,寫在 default 節中的配置項可以作為實參替換後續的引用,比如在我之前提過的「

棋牌onweb」專案中,我們使用這樣的配置檔案:

[python]view plain

copy

[default]  

base_path= /home/qipai-v1/src/server  

[policy]  

switch = on  

port = 18843

path = %(base_path)s/hall/config/flashpolicy.xml  

其中 policy.path 的值取出來的時候就變成了 /home/qipai-v1/src/server/hall/config/flashpolicy.xml,configparser 會自動為你做好替換。

棋牌onweb」專案中,所有的遊戲程序都由乙個叫 desk 的程式來服務的,它通過命令列傳入的引數載入不同的外掛程式實現不同的業務邏輯。比如 desk --game-name=doudizhu 可以執行乙個鬥地主的服務,desk --game-name=xiangqi 則是中國象棋。顯然,為了方便通過日誌分析錯誤,有必要把日誌按遊戲、房間和桌子分離開來,比如 xiangqi-1-10.log 表示象棋遊戲的房間1桌子10的程序的日誌,這時候如果使用內建的 logging 模組來記錄日誌,我們可以這樣配置它:

[python]view plain

copy

[handler_game]  

class=handlers.timedrotatingfilehandler  

formatter=comm  

args=('log/%(game_name)s-%(rid)d-%(did)d.log', 'midnight', 1, 30)  

在程式執行的時候,我們這樣初始化 logging:

[python]view plain

copy

logging.config.fileconfig(conf, defaults = dict(game_name = game_name, rid = rid, did = did))  

把 rid 和 did 通過命令列引數傳過來即可。之所以能夠這樣做,完全因為 logging 也是使用 configparser 來解釋它的配置檔案的呀,python 吸引人之一的地方就是這樣的:盡量重用,更加靈活。

兩個 Python 的冷技巧

記得剛開始工作的時候,老大給我們上 c 基礎課,告訴我們字串字面量可以換行 如下 感覺真是如夢如幻。include int main int argc,char argv 輸出 hello world.後來在寫了很久的 python 以後,才知道 python 其實也可以的 t hello worl...

python管理技巧 Python小技巧整理

一 python小工具 進入相應目錄 2 字串轉換為json root mysql m echo python m json.tool job developer name 1mx male 3 批量驗證第三方庫的安裝 python c import paramiko 二 pip的高階用法 1 安裝...

bpython使用技巧 Python 小技巧

python 隱秘的角落 1.args 和 kwargs 的使用 此處一定要注意 python 中有預設值得引數一定一定要放在沒有預設值的後邊。args 是以元組型別傳入 kwargs 是字典型別傳入 def parameter learn parameter 1,parameter 2 none,...