Skip to content

Instantly share code, notes, and snippets.

@mvasilkov
Forked from CamDavidsonPilon/snapple.py
Created April 22, 2013 20:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mvasilkov/5438070 to your computer and use it in GitHub Desktop.
Save mvasilkov/5438070 to your computer and use it in GitHub Desktop.
import re
import random
CURLY_RE = re.compile( "\{(.*?)\}" )
def spam( filename, n=1 ):
file = open(filename, "r")
all_templates = file.read().split( "|\n")
N = len( all_templates )
#randomly choose a spam template
spammy_template = all_templates[ random.randint(0, N-1) ]
formatted_spam = re.sub( CURLY_RE, "%s", spammy_template )
choices = []
for _template in re.findall( CURLY_RE, spammy_template):
_choices = _template.split("|")
choices.append( _choices[ random.randint( 0, len(_choices)-1 ) ] )
return formatted_spam%tuple(choices )
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment