Home > children, claire, geekery, python > Wait, How Old Is My Kid Again?

Wait, How Old Is My Kid Again?

April 22nd, 2008

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.

Similar Posts:

children, claire, geekery, python

  1. Andrew Dalke
    April 22nd, 2008 at 23:49 | #1

    It does give me a bit of a thrill seeing that people use my PyRSS2Gen package. Thanks for mentioning it! Looking around there are 23,000 Google hits for it, and it’s been packaged by quite a few people. Yet I’ve only received a handful of emails about it.

  2. April 23rd, 2008 at 00:01 | #2

    This is terribly dorky. I love it.

  3. April 23rd, 2008 at 10:22 | #3

    I can imagine a system where you have a feed like

    http://howoldismykid.com/2007/09/10/Claire.xml

    and parse the URL string (ad hoc! for anyone!) to give you what you need. e.g.

    http://howoldismykid.com/1809/02/12/Abraham-Lincoln.xml

    should work too.

  4. April 23rd, 2008 at 12:28 | #4

    @Edward Vielmetti: Cool idea! I’m not sure I’ve got time to do it, so go for it if you want to. (And if some miracle occurs and I do find enough free time for it, would you mind if I gave it a go?)

  5. April 24th, 2008 at 12:20 | #5

    Edward, Mike – check out an old toy of Matt Webb’s, the Light Cone RSS Feed. It does the thing with the date right there in the URL, for proper RESTy / multi-use goodness.

    http://interconnected.org/home/more/lightcone/

  6. April 27th, 2008 at 06:15 | #6

    @mike – please take my idea! I don’t know if I’ll have time or need for it myself enough to code it.

    the light cone thing is very cool. (I’m nearing 58 Eridani)

  1. No trackbacks yet.