2012-11-15 71 views

回答

2

Bundle構造函數簽名如下所示(從the source at github):

def __init__(self, *contents, **options): 

這意味着內容可以被指定爲一系列的位置參數,如the example in the documentation

Bundle('common/inheritance.js', 'portal/js/common.js', 
    'portal/js/plot.js', 'portal/js/ticker.js', 
    filters='jsmin', 
    output='gen/packed.js') 

但這也意味着你可以使用Python的能力unpack argument lists。從該頁開始:

當參數已經在列表或元組中時,會出現相反的情況,但需要爲需要單獨位置參數的函數調用解壓縮。例如,內置的range()函數需要單獨的啓動和停止參數。如果它們是不可分開,在調用函數時與* - 運算符來解壓把參數列表或元組的

所以,你可以很容易地只寫上面的例子:

files = ['common/inheritance.js', 'portal/js/common.js', 
     'portal/js/plot.js', 'portal/js/ticker.js'] 
Bundle(*files, filters='jsmin', output='gen/packed.js') 

,當然你可以在捆綁之前過濾/切片/切割列表到你心中的內容。

+0

另外,我發現在Bundle args中混合使用數據類型時,必須將列表作爲最後一個非命名輸入。 Bundle('dir/**/*。js','otherdir/*。js',* files,filters ='jsmin', output ='gen/main.min.js') 再次感謝你的幫助。 –