the further adventures of

Mike Pirnat

a leaf on the wind

« Previous Post Next Post »

Wait, How Old Is My Kid Again?

The thing about being a relatively new parent is that life very quickly becomes a complete blur, and after a certain point you've no sense of what day it is, let alone how many weeks old your little bundle of joy is. This makes life tricky, since there are certain milestone weeks that are usually the harbingers of sudden shifts into higher levels of fussiness and sleep regression.

So what's a frazzled dad to do?

Well, I do have Python, and I seem to have Google Reader open an awful lot, and cron jobs are a lot better at remembering to do things than I am... So here's my fifteen-minute solution that I whipped up the other week in between putting Claire down for her morning nap and getting ready for work. (File names and URLs have been changed to protect the innocent.)

#!/usr/bin/env python

import datetime
from dateutil.rrule import rrule, DAILY, WEEKLY, MONTHLY
import PyRSS2Gen as RSS2

# You'll want to change all of these values, obviously...
KID_NAME = u'Claire'
BIRTHDAY = datetime.datetime(2007, 9, 10, 21, 57)
FEED_URL = 'http://yoursite/kids_age.xml'
FILENAME = 'your_path/kids_age.xml'

def periods_between(freq, start_date, end_date):
    rr = rrule(freq, dtstart=start_date)
    periods = len(rr.between(start_date, end_date))
    return periods

def format_entry_body(months, weeks, days):
    body = """<h1>Today, %(kid_name)s Is...</h1>
    <ul>
        <li>%(months)s months</li>
        <li>%(weeks)s weeks</li>
        <li>%(days)s days</li>;
    </ul>
    <p>They grow up so fast!</p>"""
    kid_name = KID_NAME    # so we can cheat with locals()
    return body % locals()

def make_rss(body):
    now = datetime.datetime.now()
    # Add a hash component to the item link so that the RSS reader
    # will recognize this as today's new entry...
    item_url = FEED_URL+'#'+now.strftime('%Y%m%d')
    rss = RSS2.RSS2(
        title=u"How Old Is %s?" % KID_NAME,
        link=FEED_URL,
        description=u"How old is %s in months, weeks, and days" \
            % KID_NAME,
        lastBuildDate=now,

        items = [
            RSS2.RSSItem(
                title=u"How old is %s today?" % KID_NAME,
                link=item_url,
                description=body,
                guid=RSS2.Guid(item_url),
                pubDate=now,
            )
        ]
    )
    return rss

def to_xml(rss):
    xml = rss.to_xml()
    # Make our xml at least a tiny bit human-readable
    xml = xml.replace('><', '>\n<')
    return xml

def main():
    now = datetime.datetime.now()
    months = periods_between(MONTHLY, BIRTHDAY, now)
    weeks = periods_between(WEEKLY, BIRTHDAY, now)
    days = periods_between(DAILY, BIRTHDAY, now)
    body = format_entry_body(months, weeks, days)
    rss = make_rss(body)
    xml = to_xml(rss)
    f = open(FILENAME, 'w')
    f.write(xml)
    f.close()

if __name__ == '__main__':
    main()

You will, of course, have to install dateutil (via easy_install) and PyRSS2Gen (the old-school tarball way) so that they can do the heavy lifting for you.

Then all that's left is to cron it to run daily, and point your favorite RSS reader at the feed. Voila! You're on top of exactly how old your kid is, and quickly on your way to becoming Parent of the Year.

blog comments powered by Disqus

« Previous Post Next Post »