2 # Display famous phrases in the terminal
10 def _random_int(start, end):
11 # Use system random if available, otherwise, use Python's
13 r = random.SystemRandom()
17 return r.randint(start, end)
19 def _read_fortunes(fortune_file):
20 f = open(fortune_file, 'r')
23 lines = [line.rstrip() for line in contents.split('\n')]
24 delim = re.compile(r'^%$')
28 def save_if_nonempty(buf):
29 fortune = '\n'.join(buf)
31 fortunes.append(fortune)
46 def get_random_fortune(fortune_file):
47 fortunes = list(_read_fortunes(fortune_file))
48 randomRecord = _random_int(0, len(fortunes) - 1)
49 randFortune = fortunes[randomRecord]
50 return randFortune.partition('\n')[0]
53 print(get_random_fortune("/usr/share/phrases/phrases"))