>>105741719I asked my friend Google Gemini, the genius, to add support for punctuation.
import keyword
import sys
import re
reserved_words = keyword.kwlist
def sanitize_for_function_name(word):
word = word.replace('.', '_fullstop')
word = word.replace(';', '_semicolon')
word = word.replace(':', '_colon')
word = word.replace('?', '_questionmark')
word = word.replace('!', '_exclamation')
word = word.replace(',', '_comma')
word = word.replace('-', '_dash')
word = re.sub(r'[^a-zA-Z0-9_]', '', word)
return word.lower()
inp_words = sys.argv[1:]
functions_and_words = []
for word in inp_words:
sanitized_word = sanitize_for_function_name(word)
if sanitized_word in reserved_words:
sanitized_word += '_func'
functions_and_words.append((sanitized_word, word))
paren_count = 0
with open('newpoem.py', 'w') as f:
for sanitized_word, original_word in functions_and_words:
paren_count += 1
f.write(f"def {sanitized_word}(x): return '{original_word}' + ' ' + x\n")
for i, (sanitized_word, _) in enumerate(functions_and_words):
if i == 0:
f.write(f'print({sanitized_word}(')
elif i < len(functions_and_words) - 1:
f.write(f"{sanitized_word}(")
else:
f.write(f"{sanitized_word}(''){')'* paren_count}")
print(f"Created newpoem.py with {len(functions_and_words)} functions based on input words.")
print("You can now import this file and use the functions defined in it.")
print("Example usage: from newpoem import word1, word2")
print("Then call the functions like: print(word1('your text here'))")
from newpoem import *
Just
python3 dynamic_poem.py whoop ! zoop ? doop - poop .