I am running a python script on my raspberry pi and I was just wondering if there is any command that I can use that counts how many lines are in my script.
Regards
I am running a python script on my raspberry pi and I was just wondering if there is any command that I can use that counts how many lines are in my script.
Regards
You can get the path of the script being run and then read the file, counting all lines which are neither empty nor start with a #
symbol or either '''
or """
(all of which denote comments):
#!/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)
This prints out:
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