Time of Update: 2018-12-05
os.sep 可以取代作業系統特定的路徑分割符。os.name字串指示你正在使用的平台。比如對於Windows,它是'nt',而對於Linux/Unix使用者,它是'posix'。os.getcwd()函數得到當前工作目錄,即當前Python指令碼工作的目錄路徑。os.getenv()和os.putenv()函數分別用來讀取和設定環境變數。os.listdir()返回指定目錄下的所有檔案和目錄名。os.remove()函數用來刪除一個檔案。os.system()函數用來運行shell命令。os.
Time of Update: 2018-12-06
Module:mymodule.pyCode highlighting produced by Actipro CodeHighlighter
Time of Update: 2018-12-06
樣本1:LIST取得list長度len(list),向list追加元素list.append(),list排序list.sort(),刪除list元素 del list[index] 代碼Code highlighting produced by Actipro CodeHighlighter
Time of Update: 2018-12-05
下一個項目準備用Python+Django來完成,先做些技術準備。資料庫方面用Django已經實驗的差不多了,但是似乎效能很差。自己編譯了Mysql,用虛擬機器測試,產生1000個使用者,1000篇文章用時80秒,這種並發效能恐怕沒有辦法讓人滿意。(每篇文章需要單獨產生Tag和文章與Tag的對應關係,涉及8次資料庫操作。)花了一天時間實驗InnoDB和MyIsam的區別,但是似乎沒有影響,開不開Transaction都一樣。先不管它了,也許和我的機器硬體有關係,硬體的sync關不掉。看官方的說法
Time of Update: 2018-12-05
在RHEL6下使用Vim寫python指令碼發現不能使用中文注釋,後來發現是編碼問題[wu@server python]$ cat ex4.py #!/usr/bin/python#中文注釋#cars_numcars = 100#space_car_numspace_in_a_car = 40drivers = 30passengers = 90cars_not_driven = cars - driverscars_driven = driverscarpool_capacity =
Time of Update: 2018-12-05
Eventlet 是一個開源的高度伸縮性的Python網路編程庫。根據官方介紹大致特性如下:非阻塞I/O模型,使用epoll or libevent,關於epoll的優點,參見Linux的epoll模型和epoll精髓。協程(Coroutines)使得開發人員可以採用阻塞式的開發風格,卻能夠實現非阻塞I/O的效果隱式事件調度,使得可以在Python解譯器或者應用程式的某一部分去使用EventletEventlet 可以用來處理多線程方面的工作,它使用的是 green
Time of Update: 2018-12-05
在python中調用dll檔案中的介面比較簡單,執行個體代碼如下:如我們有一個test.dll檔案,內部定義如下:extern "C"{int __stdcall test( void* p, int len){ return len;}}在python中我們可以用以下兩種方式載入1.import ctypesdll = ctypes.windll.LoadLibrary( 'test.dll' )2.import ctypesdll = ctypes.WinDll(
Time of Update: 2018-12-05
Code highlighting produced by Actipro CodeHighlighter (freeware)http://www.CodeHighlighter.com/--> 1 def info(object, spacing=10, collapse=True): 2 """Print methods and doc strings. 3 4 Takes module, class, list, dictionary, or string.""
Time of Update: 2018-12-05
Python支援資料類型:整數,浮點數,複數,字串 複數在這裡虛數由一個 "j"尾碼表示,也可以通過complex(real, img)來建立。實部和虛部可以分別訪問。就像這樣:Code highlighting produced by Actipro CodeHighlighter (freeware)http://www.CodeHighlighter.com/-->>>> c = complex(5,3)>>> c.real5.0>&
Time of Update: 2018-12-05
1 函數的預設值為mutable類型時的問題和解決辦法Code highlighting produced by Actipro CodeHighlighter (freeware)http://www.CodeHighlighter.com/-->def f2(a, L=[]): L.append(a) return Lprint(f2(1))print(f2(2))print(f2(3))def f3(a, L=None): if L is None: L
Time of Update: 2018-12-05
__call__ Python中有一個有趣的文法,只要定義類型的時候,實現__call__函數,這個類型就成為可調用的。換句話說,我們可以把這個類型的對象當作函數來使用,相當於 重載了括弧運算子。class g_dpm(object):def __init__(self, g):self.g = gdef __call__(self, t):return (self.g*t**2)/2計算地球情境的時候,我們就可以令e_dpm = g_dpm(9.8),s =
Time of Update: 2018-12-05
__new__(cls[, ...])This method is only used for new-style classes (classes inheriting from object).Called to create a new instance of class cls. __new__ is a static method (special-cased so you need not declare it as such) that takes the class of
Time of Update: 2018-12-05
轉的別人的:Python具有強大的擴充能力,我列出了50個很棒的Python模組,包含幾乎所有的需要:比如Databases,GUIs,Images, Sound, OS interaction, Web,以及其他。推薦收藏。 Graphical interface wxPython http://wxpython.org Graphical interface pyGtk http://www.pygtk.org/ Graphical interface pyQT
Time of Update: 2018-12-05
Time of Update: 2018-12-06
IPython 的資料輸入(data-entry)的特徵是Tab自動完成功能,該功能在預設狀態下是開啟的。標準Python shell 編譯時間需要曾加 readline 支援特性,將具有自動完成功能,但需要輸入以下命令:載入rlcompleter和readline 。>>>import rlcompleter,readline>>>readline.parse_and_bind('tab:
Time of Update: 2018-12-06
當url地址含有中文,或者參數有中文的時候,這個算是很正常了,但是把這樣的url作為參數傳遞的時候(最常見的callback),需要把一些中文甚至′/’做一下編碼轉換。 一、urlencode urllib庫裡面有個urlencode函數,可以把key-value這樣的索引值對轉換成我們想要的格式,返回的是a=1&b=2這樣的字串,比如:>>> from urllib import urlencode>>> data = {... 'a':
Time of Update: 2018-12-06
關鍵字:Python UTF-8 GBK 中文 亂碼估計入門時都會遇到的。我是在windows下用的Python25內建的IDLE編輯啟動並執行,發現運行指令碼得出的結果有一些中文顯示是亂碼,但有一些是正常的。百思不得其解。首先查看了一下源檔案的編碼格式,是UTF-8。經過搜尋再搜尋,調試再調試,也換了幾個編譯器,發現比IDLE還糟糕(可能需要進行encode設定)。問題終於解決了,一共花了俺將近5個小時,寫在這裡,希望遇到問題的人能搜尋到這裡,不再重犯。亂碼原因:因為你的檔案聲明為utf-8,
Time of Update: 2018-12-06
對於python程式轉為exe檔案的方法,大家知道最多的可能就是py2exe了,不過這個是windows平台上的。在linux平台上可以使用python內建的freeze模組,用法如下:python <path of your freeze.py> [options] <entry file of your python program>要找到freeze.py,可以使用"locate freeze"比如:我有5個py檔案, gen目錄下(main.py,
Time of Update: 2018-12-06
對於c來說,我們可以用lint來檢查程式的一些隱含的問題。gcc中可以用-Wall選項來實現差不多lint的功能。對於python這樣一個動態語言來說,這種檢查就顯得更為重要了,因為有些代碼在做unit test的時候可能不會執行到,裡面的錯誤就很難被發現。我們可以用pylint來做一些仔細的檢查。pylint: ftp://ftp.logilab.fr/pub/pylint/pylint-0.12.1.tar.gzdependencies:logilab-common:
Time of Update: 2018-12-06
Yahoo!新成立Python開發人員中心 推動Python發展不管是有意還是無意,Yahoo!的這一舉措都會一定程度上推動Python在開發人員中的推廣。這個新的網站裡包括怎樣使用Python訪問Yahoo!的Web服務API,還包括一些用Python開發REST、JSON、XML和RSS的指南。在Slashdot的社區裡對Yahoo!的這一舉動討論熱烈,但多人對此表示認可。Python小傳:作為一個動態物件導向程式設計語言,Python可以被用來做多種類型的軟體開發。在與其它語言和工具的整合