2 # Display Latin famous phrases in the terminal - python version
11 def main(args=sys.argv[1:]):
13 parser = argparse.ArgumentParser(
14 description="Latin famous phrases in the terminal.")
15 parser.add_argument("-i", "--id",
17 help="print the id of the phrase.")
18 parser.add_argument("-l", "--latin",
20 help="print the Latin phrase (default)")
21 parser.add_argument("-e", "--english",
23 help="print the English translation.")
24 parser.add_argument("-n", "--notes",
26 help="print any notes on phrase.")
27 parser.add_argument("-m", "--min",
30 help="set the minimum length of the Latin phrase.")
31 parser.add_argument("-M", "--max",
34 help="set the maximum length of Latin phrase.")
35 parser.add_argument("-p", "--num",
37 help="print number of possibilities within constraints.")
38 parser.add_argument("-f", "--file",
39 help="set the location of the phrase file.")
40 parser.add_argument("-o", "--open",
42 help="specify the id of a specific phrase to print.")
43 args = parser.parse_args()
49 phrase_file = args.file
50 if os.path.isfile("phrases.csv"):
51 phrase_file = "phrases.csv"
52 elif os.path.isfile("/usr/local/share/phrases/phrases.csv"):
53 phrase_file = "/usr/local/share/phrases/phrases.csv"
55 sys.exit("cannot fine phrase database!")
57 # convert csv file into list
58 with open(phrase_file) as f:
59 reader = csv.reader(f)
60 next(reader, None) # skip header
61 all_lines = list(reader)
64 # iterate through all the phrases
69 try: # generate a shortlist of phrases of the right length
70 if args.max >= int(row[4]) >= args.min:
71 right_length.append(row[0])
72 except: # skip malformed rows without exiting
75 try: # choose a random id from the shortlist
76 chosen = int(right_length[random.randint(0, len(right_length) - 1)])
78 sys.exit("no phrase within the given parameters!")
80 # Output as specified in flags
82 if int(row[0]) == chosen:
100 print(len(right_length))
103 if __name__ == "__main__":