]> git.armaanb.net Git - phrases.git/blob - phrases.py
d1d934f2e324d9bf0cabd2b834e22d1103af8f1a
[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 random
7 import sys
8 import csv
9
10 def main(args=sys.argv[1:]):
11     # Argument parsing
12     parser = argparse.ArgumentParser(description="Latin famous phrases in the terminal.")
13     parser.add_argument("-e", "--english", action='store_true', help="Print the English translation.")
14     parser.add_argument("-i", "--id", action='store_true', help="Print the id of the phrase.")
15     parser.add_argument("-l", "--latin", action='store_true', help="Print the Latin phrase (default)")
16     parser.add_argument("-m", "--min", default=0, type=int, help="Set the minimum length of the Latin phrase")
17     parser.add_argument("-M", "--max", default=10000000, type=int, help="Set the maximum length of Latin phrase")
18     parser.add_argument("-n", "--notes", action='store_true', help="Print any notes on phrase")
19     args = parser.parse_args()
20
21     right_length = []
22
23     # Find phrases of the right size
24     with open('/usr/share/phrases/phrases.csv') as f:
25         reader = csv.reader(f)
26         all_lines = list(reader)
27         next(reader, None) # skip header
28         for row in all_lines:
29             try:
30                 if args.max >= int(row[4]) >= args.min: # generate a shortlist of phrases of the right length
31                     right_length.append(row[0])
32             except:
33                 pass # skip malformed rows
34
35         try:
36             chosen = int(right_length[random.randint(0, len(right_length) - 1)]) # choose a random id from the shortlist
37         except:
38             sys.exit("No phrase within the given parameters!")
39
40         # Output as specified in flags
41         if not (args.english or args.latin or args.notes):
42             print(all_lines[chosen][1])
43         else:
44             if args.id:
45                 print(all_lines[chosen][1])
46             if args.latin:
47                 print(all_lines[chosen][1])
48             if args.english:
49                 print(all_lines[chosen][2])
50             if args.notes:
51                 print(all_lines[chosen][3])
52
53 if __name__ == "__main__":
54     main()