2016-09-21 45 views
0

我一直在修補Invoke,但我遇到了一個很奇怪的情況,那就是似乎沒有真正的PEP指南。PEP8裝修風格超過推薦線長

調用允許您爲您定義的任何任務定義自己的CLI參數,並且您可以選擇向裝飾器提供「幫助註釋」。具體例子可以發現here

如果有更多的參數,我可以這樣做,但如果有很多任務,感覺有點奇怪。你們會做什麼樣的編碼風格?

help_for_hi = { 
    'name1': 'Name of Person 1', 
    'name2': 'Name of Person 2', 
    'name3': 'Name of Person 3', 
} 

@task(help=help_for_hi) 
def hi(ctx, name1, name2, name3): 
    """Say hi to three people.""" 
    print("Hi %s, %s, and %s!" % (name1, name2, name3)) 

修訂

按照要求,這是 '太長' 可能會樣子。

@task(help={'name1': 'Name of Person 1', 'name2': 'Name of Person 2', 'name3': 'Name of Person 3'}) 
def hi(ctx, name1, name2, name3): 
    """Say hi to three people.""" 
    print("Hi %s, %s, and %s!" % (name1, name2, name3)) 
+0

對不起,你能說出它太長時間後的樣子嗎?裝飾器表達式就像其他任何調用一樣,將其包裝到'(...)'中的多行中。 –

+0

帶參數的裝飾器只是帶有前綴「@」的函數調用 - 所以只需按照PEP8函數調用的指導原則進行。 – martineau

回答

2

你只是將裝飾器表達式分解成許多行,就像分解任何其他函數調用一樣。一個例子可以是:

@task(help={ 
    'name1': 'Name of Person 1', 
    'name2': 'Name of Person 2', 
    'name3': 'Name of Person 3'}) 
def hi(ctx, name1, name2, name3): 
    """Say hi to three people.""" 
    print("Hi %s, %s, and %s!" % (name1, name2, name3)) 

...我在日常工作中的代碼庫有不少@mock.patch.object寫出這樣。

顯然,將字典分解爲單獨的變量可以在這種情況下工作,就像您在問題中所做的一樣(我可能更喜歡假設變量命名爲:-)。

# Nothing wrong with this as far as I can tell ... 
help_for_hi = { 
    'name1': 'Name of Person 1', 
    'name2': 'Name of Person 2', 
    'name3': 'Name of Person 3', 
} 

@task(help=help_for_hi) 
def hi(ctx, name1, name2, name3): 
    """Say hi to three people.""" 
    print("Hi %s, %s, and %s!" % (name1, name2, name3))