From af605a80c79216985ac0da7305aaaa4622525141 Mon Sep 17 00:00:00 2001 From: Dave LeCompte Date: Sun, 28 Feb 2021 20:46:23 -0800 Subject: [PATCH] Port POETRY to Python So much spaghetti in the source BASIC! I've tried to make things more modular, but it's still a complicated machine. There's room for somebody else to refactor this to be cleaner, if they feel like it. --- 70 Poetry/python/poetry.py | 160 +++++++++++++++++++++++++++++++++++++ 1 file changed, 160 insertions(+) create mode 100644 70 Poetry/python/poetry.py diff --git a/70 Poetry/python/poetry.py b/70 Poetry/python/poetry.py new file mode 100644 index 00000000..9e9a1af1 --- /dev/null +++ b/70 Poetry/python/poetry.py @@ -0,0 +1,160 @@ +""" +POETRY + +A poetry generator + +Ported by Dave LeCompte +""" + +""" +PORTING EDITORIAL NOTE: + +The original code is a pretty convoluted mesh of GOTOs and global +state. This adaptation pulls things apart into phrases, but I have +left the variables as globals, which makes goes against decades of +wisdom that global state is bad. +""" + +PAGE_WIDTH = 64 + +import random + +# globals +u = 0 +i = 0 +j = 0 +k = 0 +phrase = 1 +line = "" + +def print_centered(msg): + spaces = " " * ((PAGE_WIDTH - len(msg)) // 2) + print (spaces + msg) + +def process_phrase_1(): + global line + + line_1_options = ["MIDNIGHT DREARY", + "FIERY EYES", + "BIRD OR FIEND", + "THING OF EVIL", + "PROPHET"] + + line = line + line_1_options[i] + return line + +def process_phrase_2(): + global line + global u + + line_2_options = [ + ("BEGUILING ME", 2), + ("THRILLED ME", None), + ("STILL SITTING....", None), + ("NEVER FLITTING", 2), + ("BURNED", None)] + words, u_modifier = line_2_options[i] + line += words + if not (u_modifier is None): + u = u_modifier + +def process_phrase_3(): + global line + + phrases = [ + (False, "AND MY SOUL"), + (False, "DARKNESS THERE"), + (False, "SHALL BE LIFTED"), + (False, "QUOTH THE RAVEN"), + (True, "SIGN OF PARTING"), + ] + + only_if_u, words = phrases[i] + if (not only_if_u) or (u > 0): + line = line + words + +def process_phrase_4(): + global line + + phrases = [ + ("NOTHING MORE"), + ("YET AGAIN"), + ("SLOWLY CREEPING"), + ("...EVERMORE"), + ("NEVERMORE")] + + line += phrases[i] + +def maybe_comma(): + # line 210 + global u + global line + + if len(line) > 0 and line[-1] == ".": + # don't follow a period with a comma, ever + return + + if u != 0 and random.random() <= 0.19: + line += ", " + u = 2 + # line 212 + if random.random() <= 0.65: + line += " " + u += 1 + else: + # line 214 + print(line) + line = "" + u = 0 + + +def pick_phrase(): + global phrase + global line + global i, j, k + + i = random.randint(0, 4) + j += 1 + k += 1 + + if u <= 0 and (j % 2) != 0: + # random indentation is fun! + line += " " * 5 + phrase = j + 1 + + +def main(): + print_centered("POETRY") + print_centered("CREATIVE COMPUTING MORRISTOWN, NEW JERSEY") + print() + print() + print() + + global line, phrase, j, k, u + + phrase_processors = { + 1: process_phrase_1, + 2: process_phrase_2, + 3: process_phrase_3, + 4: process_phrase_4 + } + + while True: + if phrase >= 1 and phrase <= 4: + phrase_processors[phrase]() + maybe_comma() + elif phrase == 5: + j = 0 + print(line) + line = "" + if k > 20: + print() + u = 0 + k = 0 + else: + phrase = 2 + continue + pick_phrase() + +if __name__ == "__main__": + main()