-
Notifications
You must be signed in to change notification settings - Fork 0
/
bb_trivia.py
56 lines (45 loc) · 1.7 KB
/
bb_trivia.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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
#! python3
"""This module implements methods required to open a YAML file of Blood Bowl
trivia comments, and randomly select and print one."""
import argparse
import random
import yaml
################################################################################
class TriviaFile:
"""Class encapsulates interactions with the trivia YAML file."""
def __init__(self, filename):
self.filename = filename
self.fileread = False
self.trivia = []
def read(self):
"""Reads and populates the trivia list."""
with open(self.filename, "r") as f:
blob = yaml.safe_load(f)
self.trivia = blob["trivia"]
self.fileread = True
@property
def select(self):
"""Returning a string assignment version (since __str__ is meant to be
used with print and other methods.)"""
if not self.fileread:
self.read()
return random.choice(self.trivia)
def __str__(self):
"""Given a list of trivia facts, select one randomly and returns it so
it may be printed."""
if not self.fileread:
self.read()
return random.choice(self.trivia)
################################################################################
def main():
"""Main command line entry point"""
parser = argparse.ArgumentParser(
prog="bb_trivia", description="Prints out a random Blood Bowl trivia fact."
)
parser.add_argument("filename", help="The trivia data file (YAML format).")
args = parser.parse_args()
tfile = TriviaFile(args.filename)
print(tfile)
################################################################################
if __name__ == "__main__":
main()