Last active
December 16, 2015 12:58
-
-
Save CamDavidsonPilon/5438033 to your computer and use it in GitHub Desktop.
using data from https://gist.github.com/shanselman/5422230 as `filename`, will create a random comment.
This file contains 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 re | |
import random | |
CURLY_RE = re.compile( "\{(.*?)\}" ) | |
def spam( filename ): | |
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