Skip to content

Instantly share code, notes, and snippets.

@dbolser
Created March 27, 2019 17:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dbolser/ed918feb3ef794f263746aa941e3c41a to your computer and use it in GitHub Desktop.
Save dbolser/ed918feb3ef794f263746aa941e3c41a to your computer and use it in GitHub Desktop.
#!/usr/bin/env python
# -1) Why wouldn't I use wc?
from pathlib import Path
# 0) Hopefully making things more maintainable...
import argparse
parser = argparse.ArgumentParser(
description='Check text file length is below a limit.')
parser.add_argument(
'file', type=Path, help='a file to check')
parser.add_argument(
'--limit', type=int, help='file length limit (default is 100,000,000)',
default=10**8)
args = parser.parse_args()
# 1) Using csv is just weird (and memory inefficient).
# 2) I'm fairly sure you want 10**8, not 10^8 (bit shift).
# 3) In any case, setting the 'limit' to a parameter.
# 4) Throwing an exception may be overkill here, but it helps maintainability
# 5) Fixing the function to return the actual line count
# 6) I think os is more maintainable than pathlib (debateable I
# guess)... I'm not getting any benefit from pathlib the way I've
# written it currently...
# 7) Stubbed out testing for pytest
# 8) Added a type check on the count function. Not sure if this is pythonic...
# 9) Added function descriptions
def count_lines(file=None, limit=100000000):
"""Return the number of lines in a given file. Return an exception if
the length of the file is above a given limit.
"""
if isinstance(file, Path):
lines = 0
with file.open() as file:
for line in file:
lines = lines + 1
if lines > limit:
raise Exception('File is greater than ' + str(limit) + '.')
return lines
else:
raise AttributeError
if __name__ == '__main__':
file = args.file
if file.is_file():
lines = count_lines(file, args.limit)
print(lines)
else:
print(0)
exit(1)
def test_count_lines():
"""TODO: I could 'mock' a file here and do some tests. However, I
should tidy away the argparse into a function first...
"""
pass
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment