2010-03-05 75 views
40

許多蟒蛇的IDE將啓動你喜歡的模板:什麼是最好的Python庫模塊框架代碼?

print 'hello world' 

這還不夠......因此,這裏是我的骨架代碼得到這個問題開始:

我的模塊骨架,短版本:

#!/usr/bin/env python 

""" 
Module Docstring 
""" 

# 
## Code goes here. 
# 

def test(): 
    """Testing Docstring""" 
    pass 

if __name__=='__main__': 
    test() 

,並

我的模塊骨架,長的版本:

#!/usr/bin/env python 
# -*- coding: ascii -*- 

""" 
Module Docstring 
Docstrings: http://www.python.org/dev/peps/pep-0257/ 
""" 

__author__ = 'Joe Author ([email protected])' 
__copyright__ = 'Copyright (c) 2009-2010 Joe Author' 
__license__ = 'New-style BSD' 
__vcs_id__ = '$Id$' 
__version__ = '1.2.3' #Versioning: http://www.python.org/dev/peps/pep-0386/ 

# 
## Code goes here. 
# 

def test(): 
    """ Testing Docstring""" 
    pass 

if __name__=='__main__': 
    test() 

注(PEP 8UTF-8):

""" 
===MODULE TYPE=== 
Since the vast majority of my modules are "library" types, I have constructed 
this example skeleton as such. For modules that act as the main entry for 
running the full application, you would make changes such as running a main() 
function instead of the test() function in __main__. 

===VERSIONING=== 
The following practice, specified in PEP 8, no longer makes sense: 

    __version__ = '$Revision: 1.2.3 $' 

For two reasons: 
    (1) Distributed version control systems make it neccessary to include more 
     than just a revision number. E.g. author name and revision number. 
    (2) It's a revision number not a version number. 


Instead, the __vcs_id__ variable is being adopted. This expands to, for 
example: 
    __vcs_id__ = '$Id: example.py,v 1.1.1.1 2001/07/21 22:14:04 goodger Exp $' 


===VCS DATE=== 
Likewise, the date variable has been removed: 

    __date__ = '$Date: 2009/01/02 20:19:18 $' 


===CHARACTER ENCODING=== 
If the coding is explicitly specified, then it should be set to the default 
setting of ASCII. This can be modified if necessary (rarely in practice). 
Defaulting to UTF-8 can cause anomalies with editors that have poor unicode 
support. 

""" 

替代骨架,長的版本:(代碼改編自DasIch的答案)

#!/usr/bin/env python 
# -*- coding: ascii -*- 

""" 
package.module 
~~~~~~~~~~~~~ 

A description which can be long and explain the complete 
functionality of this module even with indented code examples. 
Class/Function however should not be documented here. 

:copyright: year by my name, see AUTHORS for more details 
:license: license_name, see LICENSE for more details 
""" 

# 
## Code goes here. 
# 

def test(): 
    """ """ 
    pass 

if __name__=='__main__': 
    test() 

有很多政治公衆人物的提出編碼風格建議。我是否錯過了任何重要的最佳實踐?什麼是最好的Python模塊框架代碼?

更新

給我任何類型的,你喜歡的「最佳」。告訴我們您用什麼指標來評估「最佳」。

+4

「最好」似乎有點含糊,也許你可以澄清你的問題。 – Francesco 2010-03-05 14:19:00

+0

我希望得到最佳基線骨架代碼的共識。否則,我會解決只知道其他人使用。 – user213060 2010-03-05 14:37:39

+0

@ user213060:「最佳基線」。 「最好」是什麼意思?最短?最快的?簡單?大多數功能?最大?大多數文檔?最接近Django? WSGI? 「最好」是什麼意思?請用最好的定義更新你的問題。請不要對您擁有的問題添加評論。 – 2010-03-05 15:20:10

回答

18
#!/usr/bin/env python 
# coding: utf-8 
""" 
    package.module 
    ~~~~~~~~~~~~~ 

    A description which can be long and explain the complete 
    functionality of this module even with indented code examples. 
    Class/Function however should not be documented here. 

    :copyright: year by my name, see AUTHORS for more details 
    :license: license_name, see LICENSE for more details 
""" 

模塊可能會或可能不會包含main功能,所以這不是模板的一部分。

+1

有可用想法的最獨特的貢獻。正在尋找像這樣更獨特的答案。做得好。 – user213060 2010-03-15 13:24:12

+0

我認爲省略編碼線要好得多,除非你真的需要它。這樣,您就可以避免將非ascii字符偶然粘貼到字符串文字中,而不會意識到它(即使最終將字符串從例如Word粘貼到Python文件中時出現其他錯誤) – ThiefMaster 2015-05-02 12:51:51

1

根據程序的性質,您可以考慮選擇許可證並將其放在文件的開頭。

12

它往往是可取的設定

#coding=<coding> 

在第二行。像

#coding=utf8 

例如。這是詳細的替代方案

# -*- coding: <encoding name> -*- 

有關更多信息,請參閱PEP-263


編輯完整答案:取決於情況。如果對於一些內部項目來說,簡單就好一點。但是,我幾乎總是有

def main(): 
    #code 
    pass 

if __name__=="__main__": 
    main() 

如果我打算公佈的代碼,我添加適當的文件和許可條款以及所提到的編碼指令。

shebang(#!/usr/bin/env python)僅適用於本意爲可執行文件的文件。

+1

這是utf-8用破折號,IIRC(從我+1 :-)) – Francesco 2010-03-05 14:25:17

+2

@Francesco:看起來像utf8是一個有效的別名:http://docs.python.org/library/codecs.html - python拋出如果您設置了無效編碼,則會出錯 – 2010-03-05 14:31:20

+0

@Otto:謝謝,我忘了它。 – Francesco 2010-03-05 14:49:46

4

返回的東西是最好的做法太(這裏稱爲參數):

... 
import sys 

def main(args): 
    return 0 

if __name__=='__main__': 
    sys.exit(main(sys.argv)) 

但它變得比簡單的「hello world」更復雜。

+0

這是一個有趣的觀點。 – user213060 2010-03-05 15:22:59

0

如何:

... 
import sys 

def main(*args): 
    return 0 

if __name__=='__main__': 
    sys.exit(main(*sys.argv[1:])) 

那麼你當然修改骨架主要以反映實際參數(文件名後):

def main(arg1, arg2, *args): 
    ... 

(驚訝我們不能在評論中使用降價...)

+0

對於仍然直接使用sys.argv的用戶,您是否聽說過http://code.google.com/p/pyopt/? – user213060 2010-03-05 19:53:19

+1

僅僅爲了舉例代碼(這是在正確模塊中通常使用的主要函數)添加更多的依賴關係並不總是明智的。 – 2010-03-06 13:50:29

+1

>(驚訝,我們不能在評論中使用降價...) *一些*降價在評論中起作用,但在我的書中不夠。 – 2010-03-08 15:16:05

2

我會說最好的是最簡單的比滿足您的要求。您投入「骨架」的數據越多,您可能會獲得的數據越陳舊或無意義或錯誤。

  • if __name__=='__main__':零件在模塊中不需要,只是一個模塊。測試可能是模塊的一部分,但即使如此,它們也可以被稱爲外部。直接調用模塊通常很不方便(或者不可能,例如,當使用相對導入時)。
  • Python解釋器通常會說需要編碼信息的時候,並不是每一個代碼都需要非ASCII字符。

合理的最低恕我直言是模塊開頭的文檔字符串。在你的問題和其他答案中提到的其他作品也常常有用,但決不是強制性的。

+0

我同意默認utf-8是一個糟糕的選擇。我不同意測試。我認爲這很好,要求包含測試,或者至少有一個存根,就在模塊中的第一個版本。然後,隨着模塊成熟,可將測試移至外部模塊。總的來說,欣賞輸入。 – user213060 2010-03-05 20:31:30

+0

至於「最簡單的滿足要求」,當Windows用戶開始時忽略'#!/ usr/bin/env python'時,我會畏縮不前。這可能導致腳本由默認情況下由bash解釋器運行。非常不好或奇怪的事情可能發生然後... – user213060 2010-03-05 20:33:38

+0

'#!/ usr/bin/python'(我不喜歡/ bin/env版本由於各種原因,但這是脫離主題)是需要爲Python腳本可執行文件)不是模塊。並非每個模塊都需要成爲可執行文件。 – 2010-03-05 20:41:34

4

模塊不可執行,所以他們不應該有一個shebang。

Docstrings很好。

編碼是有用的。

作爲包裝元數據的一部分,元數據(如作者,版權,版本和許可證最好存儲在setup.py中)。使用__(metadata)__模塊屬性是一個過時的做法,因爲它早於Python打包元數據的時間。如果代碼具有足夠的短暫性而不保證打包,那麼您將需要任何元數據是不可靠的。

其他功能,如test()或__main__黑客我沒有足夠的使用以保證包含在模塊模板中。

因此唯一需要的模板是:

# -*- coding: ascii -*- 
""" 
""" 

尼斯和簡單。

+1

將模塊骨架模板想象成可以消除常見類型代碼的重新輸入。如果你要看看你寫過的最後100個模塊,我相信你會發現在這方面這個骨架已經縮短了。 – user213060 2010-03-08 17:57:11

相關問題