Preparing git repo for final project
This commit is contained in:
33
problems/pset6/lines/lines.py
Normal file
33
problems/pset6/lines/lines.py
Normal file
@@ -0,0 +1,33 @@
|
||||
import os
|
||||
import sys
|
||||
|
||||
|
||||
def main():
|
||||
try:
|
||||
file_name = sys.argv[1]
|
||||
except IndexError:
|
||||
print("Provide a file name from command line!")
|
||||
sys.exit(1)
|
||||
if len(sys.argv) > 2:
|
||||
print("Too many command-line arguments")
|
||||
sys.exit(1)
|
||||
if not os.path.isfile(file_name): # Or try opening and catch FileNotFoundError
|
||||
print(f"The file `{file_name}` does not exist!")
|
||||
sys.exit(1)
|
||||
if not file_name.endswith('.py'):
|
||||
print(f"The file `{file_name}` is not a python script (does not end with the proper suffix)!")
|
||||
sys.exit(1)
|
||||
line_count = count_lines(file_name)
|
||||
print(line_count)
|
||||
|
||||
|
||||
def count_lines(file_name: str) -> int:
|
||||
line_count = 0
|
||||
with open(file_name, 'r') as file_object:
|
||||
for line in file_object:
|
||||
if len(line.strip()) > 0 and not line.strip().startswith('#'):
|
||||
line_count += 1
|
||||
return line_count
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
13
problems/pset6/lines/test_lines.py
Normal file
13
problems/pset6/lines/test_lines.py
Normal file
@@ -0,0 +1,13 @@
|
||||
from lines import count_lines
|
||||
|
||||
|
||||
def main():
|
||||
test_count_lines()
|
||||
|
||||
|
||||
def test_count_lines():
|
||||
assert count_lines('lines.py') == 28
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
Reference in New Issue
Block a user