Tag Archives: code

Someone asked me recently to develop a content spinner algorithm which can take a document and produce variations of that document. I thought it was an interesting thing to think about, and very rarely do I get to think about algorithms like this in my normal day-to-day programming.

The document contains variation options with a special syntax.  for example:

{Hi|Hello|Good morning}, my name is Matt and I have {something {important|special} to say|a favorite book}.

The algorithm will recursively go through the string and generate a new string by choosing an option provided in curly braces separated by pipes.

import random
 
def spin(content):
    """takes a string like
 
    {Hi|Hello|Good morning}, my name is Matt and I have {something {important|special} to say|a favorite book}.
 
    and randomly selects from the options in curly braces
    to produce unique strings.
    """
    start = content.find('{')
    end = content.find('}')
 
    if start == -1 and end == -1:
        #none left
        return content
    elif start == -1:
        return content
    elif end == -1:
        raise "unbalanced brace"
    elif end < start:
        return content
    elif start < end:
        rest = spin(content[start+1:])
        end = rest.find('}')
        if end == -1:
            raise "unbalanced brace"
        return content[:start] + random.choice(rest[:end].split('|')) + spin(rest[end+1:])
 
if __name__=='__main__':
    print spin('{Hi|Hello|Good morning}, my name is Matt and I have {something {important|special} to say|a favorite book}.')

It is extremely useful to send emails from scripts.  Emails can alert you to errors as soon as they happen or can give you regular status updates about the running of your programs.

I have several scripts that run regularly to update various websites or scrape data from different places and quite often when dealing with the internet things change.  Code breaks constantly as the things they depend on change so to make sure everything continues to run it’s important to be notified when errors happen.

One of the greatest ways to do this is to have your programs send email messages to you.  I use Google’s Gmail SMTP server to relay my messages to me.  That way I don’t have to rely on having sendmail installed on the machine or hooking into something like MS Outlook to compose an email.

This small simple script uses smtplib to send simple text emails using Gmail’s SMTP service.

#!/usr/bin/python
 
import smtplib
from email.MIMEText import MIMEText
 
GMAIL_LOGIN = 'myemail@gmail.com'
GMAIL_PASSWORD = 'password'
 
 
def send_email(subject, message, from_addr=GMAIL_LOGIN, to_addr=GMAIL_LOGIN):
    msg = MIMEText(message)
    msg['Subject'] = subject
    msg['From'] = from_addr
    msg['To'] = to_addr
 
    server = smtplib.SMTP('smtp.gmail.com',587) #port 465 or 587
    server.ehlo()
    server.starttls()
    server.ehlo()
    server.login(GMAIL_LOGIN,GMAIL_PASSWORD)
    server.sendmail(from_addr, to_addr, msg.as_string())
    server.close()
 
 
if __name__=="__main__":
    send_email('test', 'This is a test email')