我可以得到隐式字符串加入python中的lint错误吗?

问题描述:

是否有某种方法可以在文字列表中的字符串中找到丢失逗号的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,该怎么办?你需要工具来告诉你这个吗?你在问题中提到的关切是一样的。 –

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逗号 –

我不知道是什么样的小号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以达到您的目的。