【问题标题】:python script - counting lines of code in the scriptpython脚本 - 计算脚本中的代码行数
【发布时间】:2016-05-06 07:27:10
【问题描述】:

我在我的树莓派上运行一个 python 脚本,我只是想知道是否有任何命令可以用来计算我的脚本中有多少行。

问候

【问题讨论】:

  • 从脚本中?
  • 您使用的是哪个编辑器?大多数编辑器都具有显示行号的功能,但默认情况下通常不会开启。
  • 这是对 pi 的 shell 访问,我正在使用 nano .. wc -l 命令对其进行编辑:)

标签: python raspberry-pi raspbian lines-of-code


【解决方案1】:

您可以使用wc 命令:

wc -l yourScript.py

【讨论】:

    【解决方案2】:

    您可以get the path of the script being run 然后读取文件,计算所有既不为空也不以# 符号或'''""" 开头的行(所有这些都表示cmets):

    #!/usr/bin/env python3
    
    '''
    This script prints out all significant lines of code contained within 
    itself.
    '''
    
    import os
    import re
    
    SINGLE_LINE_COMMENT_DELIMITER = "#"
    MULTILINE_COMMENT_DELIMITER_PATTERN = re.compile("['\"]{3}")
    
    class SignficantLineParser(object):
    def __init__(self):
        self.in_comment_section = False
    
    def parse(self, line):
        line = line.strip()
        if self.in_comment_section:
            if line.startswith(SINGLE_LINE_COMMENT_DELIMITER):
                return False
            else:
                if MULTILINE_COMMENT_DELIMITER_PATTERN.match(line):
                    # Exiting multi-line comment
                    self.in_comment_section = False             
        elif line:
            if line.startswith(SINGLE_LINE_COMMENT_DELIMITER):
                return False
            else:
                if MULTILINE_COMMENT_DELIMITER_PATTERN.match(line):
                    # Entering multi-line comment
                    self.in_comment_section = True
                    return False
                else:
                    return True
        else:
            return False
    
    
    script_path = os.path.realpath(__file__)
    with open(script_path, 'r') as inf:
        parser = SignficantLineParser()
        significant_lines = 0
        for line in inf:
            if parser.parse(line):
                significant_lines += 1
                print("Significant line: " + line, end="")
    
    print("\n\nSignificant line count: %d" % significant_lines)
    

    打印出来:

    Significant line: import os
    Significant line: import re
    Significant line: SINGLE_LINE_COMMENT_DELIMITER = "#"
    Significant line: MULTILINE_COMMENT_DELIMITER_PATTERN = re.compile("['\"]{3}")
    Significant line: class SignficantLineParser(object):
    Significant line:   def __init__(self):
    Significant line:       self.in_comment_section = False
    Significant line:   def parse(self, line):
    Significant line:       line = line.strip()
    Significant line:       if self.in_comment_section:
    Significant line:           if line.startswith(SINGLE_LINE_COMMENT_DELIMITER):
    Significant line:               return False
    Significant line:           else:
    Significant line:               if MULTILINE_COMMENT_DELIMITER_PATTERN.match(line):
    Significant line:                   self.in_comment_section = False             
    Significant line:       elif line:
    Significant line:           if line.startswith(SINGLE_LINE_COMMENT_DELIMITER):
    Significant line:               return False
    Significant line:           else:
    Significant line:               if MULTILINE_COMMENT_DELIMITER_PATTERN.match(line):
    Significant line:                   self.in_comment_section = True
    Significant line:                   return False
    Significant line:               else:
    Significant line:                   return True
    Significant line:       else:
    Significant line:           return False
    Significant line: script_path = os.path.realpath(__file__)
    Significant line: with open(script_path, 'r') as inf:
    Significant line:   parser = SignficantLineParser()
    Significant line:   significant_lines = 0
    Significant line:   for line in inf:
    Significant line:           if parser.parse(line):
    Significant line:               significant_lines += 1
    Significant line:               print("Significant line: " + line, end="")
    Significant line: print("\n\nSignificant line count: %d" % significant_lines)
    
    Significant line count: 35
    

    【讨论】:

    • 哦!谢谢你..虽然我想要的更简单;只有一个命令行。但是我不得不承认这给了我一个支持我的脚本的想法。谢谢你:)
    猜你喜欢
    • 1970-01-01
    • 2012-09-19
    • 1970-01-01
    • 2019-12-29
    • 2015-10-05
    • 1970-01-01
    • 1970-01-01
    • 2013-01-02
    • 2023-03-12
    相关资源
    最近更新 更多