2016-11-09 21 views
10

是否有某種方法可以在文字列表中的字符串中找到丟失逗號的lint錯誤?我可以得到隱式字符串加入python中的lint錯誤嗎?

例子:

exceptions = ["banana", "pineapple", "apple" 
       "pen"] 

你可能會認爲這個列表包含4個項目,但說實話! 「蘋果」和「筆」加入「蘋果筆」。

我很害怕這些省略的逗號。有一些皮棉工具可以幫助我找到它們嗎?

實施例2:

exceptions = ["Carmichael", 
       "Vanessa"  # <--- Spot the missing comma 
       "Ford"] 
+0

''foo「」bar「'foobar''也是如此。這是一個錯誤嗎?爲什麼? – 2016-11-09 09:10:17

+0

嗯是的,我想我也想爲這種情況下的皮棉錯誤。它創造了比它值得的更多的麻煩。另請參閱http://legacy.python.org/dev/peps/pep-3126/#concerns – Moberg

+2

我不知道這是否有任何工具?即使這樣做,爲什麼你需要這個?這是Python的行爲,你應該牢記這一點。總會有*什麼「IF」*?如果我寫了2代替3,該怎麼辦?你需要工具來告訴你這個嗎?你在問題中提到的關切是一樣的。 –

回答

-1

SublimeText3與插件flake8,即周圍的其他蟒棉絨插件的包裝,可以修復它。

否則,你可以做一個腳本,計數((數「)/ 2)-1和逗號在一條線上,如果結果不匹配,加上昏迷

編輯:

我在說什麼的Explanaition:

def countQuotes(string): 
     return string.count('"') 
    def countCommas(string): 
     return string.count(',') 
    files = os.listdir('your/directory') 
    for filename in files: 
    if filename.endswith(".py"): 
     with fileinput.FileInput("your/directory"+"/"+filename, inplace=True, backup='.bak') as fileContent: 
     for line in fileContent: 
      if '"' in line: 
       numQuotes = countQuotes(line) 
       numCommas = countCommas(line) 
       if(numQuotes == 2 and ']' in line): 
        if(numCommas != 0): 
         #error, must delete a comma in right place and print line 
        else: 
         print(line) 
       if(numQuotes == 2 and ']' not in line): 
        if(numCommas != 1): 
         #error, must add a comma in right place and print line 
        else: 
         print(line) 
       if(numQuotes > 2): 
        if(numCommas > (numQuotes//2)-1) 
         #error, must delete a comma in right place and print line 
        elif(numCommas < (numQuotes//2)-1) 
         #error, must add a comma in right place and print line 
        else: 
         print(line) 
      else: 
       print(line) 

這種方法必須工作,只是覺得,你必須插入或刪除逗號,終於有你想要的格式

+0

這種簡單的計數不起作用。在例子和'[「汽車」,「自行車」]比較問題' – Moberg

+0

不幸的是沒有在工作中使用崇高。也許我應該。 – Moberg

+0

@moberg我認爲它仍然適用於你的情況,4「,1逗號 –

0

我不知道是什麼樣的小號wece分析工具,所以我只能提出一個建議。然而,評論太長,所以我寫了一個概念驗證腳本。

這個想法是用Python的tokenize模塊來看源代碼,該模塊從Python表達式生成令牌。如果格式良好的Python代碼包含隱式連續的字符串文字,則它將顯示爲STRING標記,然後是NL

例如,我們使用以下源文件source.py作爲測試用例。

x = ("a" 
     "b" # some trailing spaces 
# Coment line 
"c" 
"" 
    # The following is an explicit continuation 
    "d" \ 
    "e") 

上的文件運行命令python check.py < source.py產生:

1:8: implicit continuation: 
x = ("a" 

    ~~~^ 
2:35: implicit continuation: 
     "b" # some trailing spaces 

           ~~~^ 
4:3: implicit continuation: 
"c" 

~~~^ 
5:2: implicit continuation: 
"" 

^

程序,check.py,僅僅是一個驗證的概念,它不檢查語法錯誤或其它邊緣情況:

import sys 
import tokenize 


LOOKNEXT = False 
tok_gen = tokenize.generate_tokens(sys.stdin.readline) 
for tok, tok_str, start, end, line_text in tok_gen: 
    if tok == tokenize.STRING: 
     LOOKNEXT = True 
     continue 
    if LOOKNEXT and (tok == tokenize.NL): 
      warn_header = "%d:%d: implicit continuation: " % start 
      print >> sys.stderr, warn_header 
      print >> sys.stderr, line_text 
      indents = start[1] - 3 
      if indents >= 0: 
       print >> sys.stderr, "%s~~~^" % (" " * indents) 
      else: 
       print >> sys.stderr, "%s^" % (" " * start[1]) 
    LOOKNEXT = False 

希望這個想法可能會幫助您擴展您的lint工具或IDE以達到您的目的。

相關問題