Created
April 4, 2013 19:28
-
-
Save csinchok/5313395 to your computer and use it in GitHub Desktop.
Simple grammar generator
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import random | |
class Announcement(object): | |
def __init__(self, article): | |
self.grammars['article'] = [article] | |
start = ['"%(article)s" is a %(hyperbole)s, %(name)s!'] | |
grammars = { | |
'hyperbole': [ | |
"Hot Potato", | |
"Screamin' Green Been", | |
"Deviled Egg" | |
], | |
'name': [ | |
'folks', | |
'ladies and gents', | |
'bros' | |
] | |
} | |
def __getitem__(self, key): | |
if key in self.grammars: | |
return random.choice(self.grammars[key]) | |
else: | |
raise KeyError | |
def __str__(self): | |
return random.choice(self.start) % self | |
announcement = Announcement("Some testing article") | |
print(announcement) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment