-
Notifications
You must be signed in to change notification settings - Fork 12
/
aggregate.py
39 lines (31 loc) · 932 Bytes
/
aggregate.py
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
# Word list aggregate script
# Copyright (c) Jeremy Rifkin 2015 - 2019
import os
import sys
import time
def main():
# Check that files were given
if len(sys.argv) == 1:
sys.stderr.write("Error: no files provided.\n")
sys.exit(1)
# Check that all files exist
files = sys.argv[1:]
for fpath in files:
if not os.path.isfile(fpath):
sys.stderr.write("Error: file \"{}\" does not exist.\n".format(fpath))
sys.stderr.write("Starting aggregator.\n")
start = time.time()
# would use a set but for some reason a dictionary gets better performance
words = {}
for fpath in files:
with open(fpath, "r") as f:
for line in f:
if line.strip() != "":
words[line.strip().capitalize()] = 0
words = sorted(words.keys())
for word in words:
print(word)
sys.stderr.write("Done.\n")
t = time.time() - start
sys.stderr.write("Excecution time: {:.02f} second{}.\n".format(t, "" if t == 1 else "s"))
main()