-
-
Notifications
You must be signed in to change notification settings - Fork 88
Expand file tree
/
Copy pathcustom_wc.py
More file actions
66 lines (53 loc) · 2.13 KB
/
custom_wc.py
File metadata and controls
66 lines (53 loc) · 2.13 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
import argparse
import re
def format_number(number_array):
space_array = [3, 4, 4]
result = map(lambda number, space: str(number).rjust(space), number_array, space_array)
return "".join(list(result))
parser = argparse.ArgumentParser(
prog="custom wc in python",
description="trying to match behaviour in the actual wc"
)
parser.add_argument("-l", "--lines", action="store_true", help="count how many lines")
parser.add_argument("-w", "--words", action="store_true", help="count how many words")
parser.add_argument("-c", "--characters", action="store_true", help="count how many characters")
parser.add_argument("path", nargs="+", help="path to process")
args = parser.parse_args()
files_array = args.path
total_lines = 0
total_words = 0
total_characters = 0
total_numbers_row_array = []
for file_path in files_array:
with open(file_path) as file:
context = file.read()
lines = len(context.split("\n")) - 1
words = len(re.findall(r"\S+", context))
characters = len(context)
numbers_row_array = []
if (args.lines):
numbers_row_array.append(lines)
if (args.words):
numbers_row_array.append(words)
if (args.characters):
numbers_row_array.append(characters)
if (len(numbers_row_array) == 1 and len(files_array) == 1):
print(f'{numbers_row_array[0]} {file_path}')
elif (len(numbers_row_array) > 0):
print(f'{format_number(numbers_row_array)} {file_path}')
else:
print(format_number([lines, words, characters]), file_path)
total_lines += lines
total_words += words
total_characters += characters
if (len(files_array) > 1):
if (args.lines):
total_numbers_row_array.append(total_lines)
if (args.words):
total_numbers_row_array.append(total_words)
if (args.characters):
total_numbers_row_array.append(total_characters)
if (len(total_numbers_row_array) > 0):
print(f'{format_number(total_numbers_row_array)} total')
else:
print(format_number([total_lines, total_words, total_characters]), 'total')