]> git.armaanb.net Git - phrases.git/blob - phrases.py
add argument parsing, fix program logic
[phrases.git] / phrases.py
1 #!/usr/bin/env python3
2 # Display Latin famous phrases in the terminal
3 # Armaan Bhojwani 2020
4
5 import argparse
6 import csv
7 import random
8 import sys
9
10 def main(args=sys.argv[1:]):
11     # Argument parsing
12     parser = argparse.ArgumentParser(
13         description="Latin famous phrases in the terminal.")
14     parser.add_argument("-i", "--id",
15                         action='store_true',
16                         help="print the id of the phrase.")
17     parser.add_argument("-l", "--latin",
18                         action='store_true',
19                         help="print the Latin phrase (default)")
20     parser.add_argument("-e", "--english",
21                         action='store_true',
22                         help="print the English translation.")
23     parser.add_argument("-n", "--notes",
24                         action='store_true',
25                         help="print any notes on phrase")
26     parser.add_argument("-m", "--min",
27                         default=0,
28                         type=int,
29                         help="set the minimum length of the Latin phrase")
30     parser.add_argument("-M", "--max",
31                         default=10000000,
32                         type=int,
33                         help="set the maximum length of Latin phrase")
34     parser.add_argument("-p", "--num",
35                         action='store_true',
36                         help="print number of possibilities within constraints")
37     parser.add_argument("-f", "--file",
38                         default="/usr/share/phrases/phrases.csv",
39                         help="set the location of the phrase file")
40     args = parser.parse_args()
41
42     right_length = []
43
44     # convert csv file into list
45     with open(args.file) as f:
46         reader = csv.reader(f)
47         next(reader, None) # skip header
48         all_lines = list(reader) 
49     f.close()
50
51     # iterate through all the phrases
52     for row in all_lines:
53         try: # generate a shortlist of phrases of the right length
54             if args.max >= int(row[4]) >= args.min:
55                 right_length.append(row[0])
56         except: # skip malformed rows without exiting
57             pass
58
59     try: # choose a random id from the shortlist
60         chosen = int(right_length[random.randint(0, len(right_length) - 1)])
61     except:
62         sys.exit("No phrase within the given parameters!")
63
64     # Output as specified in flags
65     for row in all_lines:
66         if int(row[0]) == chosen:
67             if not (args.id
68                     or args.latin
69                     or args.english
70                     or args.notes
71                     or args.num):
72                 print(row[1])
73             else:
74                 if args.id:
75                     print(row[1])
76                 if args.latin:
77                     print(row[1])
78                 if args.english:
79                     print(row[2])
80                 if args.notes:
81                     print(row[3])
82                 if args.num:
83                     print(len(right_length))
84
85 if __name__ == "__main__":
86     main()