python脚本 - 计算脚本中的代码行

时间:2016-05-06 07:27:10

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

我正在我的覆盆子pi上运行一个python脚本,我只是想知道是否有任何可以使用的命令可以计算我的脚本中有多少行。

此致

2 个答案:

答案 0 :(得分:3)

您可以使用wc命令:

wc -l yourScript.py

答案 1 :(得分:0)

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

#!/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