2013-01-09 23 views
0

使用optparse,我想從我調用add_option()的地方分開選項列表參數列表。我如何將文件打包到文件A中(然後在文件B中解壓縮),這樣才能起作用?該parser_options.append()作爲寫入線條將無法正常工作......如何將參數列表傳遞給Python中的另一個函數?

文件:

import file_b 
parser_options = [] 
parser_options.append(('-b', '--bootcount', type='string', dest='bootcount', default='', help='Number of times to repeat booting and testing, if applicable')) 
parser_options.append(('-d', '--duration', type='string', dest='duration', default='', help='Number of hours to run the test. Decimals OK')) 

my_object = file_b.B(parser_options) 

文件B臨危parser_options輸入:

import optparse 
class B: 
    def __init__(self, parser_options): 
     self.parser = optparse.OptionParser('MyTest Options') 
     if parser_options: 
      for option in parser_options: 
       self.parser.add_option(option) 

*編輯:固定爲使用ojbects

+1

'import file_A'?什麼是控制流程? – StoryTeller

+0

調用'build_parser()'在哪裏? – millimoose

+0

這只是一個片段,因爲有涉及的對象,這將是很多張貼。我只是想說出問題的味道。我需要將這些parser_options從一個對象傳遞到另一個對象。 –

回答

0

與其試圖將選項硬塞進某些數據結構中,在文件A中定義函數並不簡單t將選項添加到您給它的解析器中?

文件:

def addOptions(parser): 
    parser.add_option('-b', '--bootcount', type='string', dest='bootcount', default='', help='Number of times to repeat booting and testing, if applicable') 
    parser.add_option('-d', '--duration', type='string', dest='duration', default='', help='Number of hours to run the test. Decimals OK') 

文件B:

import optparse 
def build_parser(parser_options): 
    parser = optparse.OptionParser('MyTest Options') 
    if parser_options: 
     parser_options(parser) 

別處:

import file_a 
import file_b 
file_b.build_parser(file_a.addOptions) 
+0

是的,這會更簡單,但是我將parser_options傳遞給構造中的對象,因爲解析需要在__init__中發生。所以在構建之前我可以調用的對象中沒有任何東西。 –

+0

函數也是值。我會編輯更完整。 –

+0

我更新了示例以顯示問題...我沒有將它們兩者都導入的文件C –

0

你的問題是,你試圖通過關鍵字參數的元組。代碼('-b', '--bootcount', type='string', dest='bootcount', default='', help='Number of times to repeat booting and testing, if applicable')只在函數調用中合法,而不在其他任何地方。 type='string'位在元組中不合法!

如果要傳遞函數參數,則需要使用列表或元組作爲位置參數和關鍵字參數的字典。這裏是你可以通過改變你的單身元組包含args元組和kwargs字典中的元組做的一個方法:

parser_options = [] 
parser_options.append((('-b', '--bootcount'), 
         dict(type='string', dest='bootcount', default='', 
          help='Number of times to repeat booting and testing, if applicable'))) 
parser_options.append((('-d', '--duration'), 
         dict(type='string', dest='duration', default='', 
          help='Number of hours to run the test. Decimals OK'))) 

在你的其他的文件,你可以通過元組和字典的內容,以適當的功能使用***運營商來解壓縮參數:

class B: 
    def __init__(self, parser_options) 
     self.parser = optparse.OptionParser('MyTest Options') 
     if parser_options: 
      for args, kwargs in parser_options: 
       self.parser.add_option(*args, **kwargs) 
+0

是的,那是我正在尋找的結構。你回答了我最終提出的問題,菲爾弗洛斯特幫助我找到了一種更直接的方式來做我想做的事。謝謝! –

0

我結束了傳遞解析器在施工對象,這是很好的,因爲我可以將其命名從通話模塊:

import optparse 
parser = optparse.OptionParser('My Diagnostics') 
parser.add_option('-p', '--pbootcount', type='string', dest='pbootcount', default='testing1234', help=' blah blah') 
c = myobject.MyObject(parser) 
相關問題