• Zeth will be attending PyCon UK on the 12th to 14th September 2008.

Recurring monthly events in Python

9 August 2008

I was working on something in one of my little Django sites and wondered how you make a recurring monthly event in Python? What I mean by recurring event is "every fourth Saturday" or "every first and second Wednesday" and so on.

I did not want to make a dependency on some huge calender server module like Calcore or Twisted's caldav. All I wanted was a function that accepts "every fourth Saturday" and returns me an actual date that I can use for scheduling things.

A quick google didn't come up with anything, so I decided to do it myself. Here are my first and second attempts. The first attempt just works it out mathematically, the second attempt uses a module from the Python standard library.

"""Helper for recurring date."""

DAYS = [
'Monday',
'Tuesday',
'Wednesday',
'Thursday',
'Friday',
'Saturday',
'Sunday',
]

from datetime import date

def eventdate(year, month, target_day, target_ordinal):
    """Convert a human event date to a real date.
    For example, 'the third Thursday of the month'
    the target_ordinal is 3 and the target_day is 'Thursday'.
    """

    day = DAYS.index(target_day.title())

    match = 0
    for i in range(1, 32):
        try:
            if date(year, month, i).weekday() == day:
                match += 1
                if match == target_ordinal:
                    return date(year, month, i)

        except ValueError:
            return None

def main():
    """Example when called directly."""
    today = date.today()
    if today.month == 12:
        year = today.year + 1
        month = 1
    else:
        year = today.year
        month = today.month + 1
    print "Next Month's Linux group is", eventdate(year, month, 'Thursday', 3)
    print "Next Month's Python group is", eventdate(year, month, 'Saturday', 4)

# start the ball rolling
if __name__ == "__main__":
    main()

That worked quite fine, but like all good Python programmers, I want to be as efficient (/lazy) as possible, surely the standard library can do this for me? Well I found that the calendar module will return a matrix of dates organised by week and day. This works as follows:

"""Event helpers."""

def eventdate(year, month, target_day, target_ordinal):
    """Convert a human event date to a real date.
    For example, 'the third Thursday of the month'
    the target_ordinal is 3 and the target_day is 'Thursday'.
    """

    import calendar
    day = getattr(calendar, target_day.upper())
    cal = calendar.Calendar()
    return cal.monthdatescalendar(year, month)[target_ordinal - 1][day]

def main():
    """Example when called directly."""
    from datetime import date
    today = date.today()
    if today.month == 12:
        year = today.year + 1
        month = 1
    else:
        year = today.year
        month = today.month + 1
    print "Next Month's Linux group is", eventdate(year, month, 'Thursday', 3)
    print "Next Month's Python group is", eventdate(year, month, 'Saturday', 4)

# start the ball rolling

if __name__ == "__main__":
    main()

This seems to work identically as the above but in less lines of code. I still get the feeling I am trying too hard and I am missing something obvious, but maybe I am just being too much of perfectionist (as always).

If anyone knows or can work out a more efficient method, please do let me know.

1 Stavros says...

You should look at http://labix.org/python-dateutil, it does this and much more.

Posted at 7:14 p.m. on August 9, 2008


2 Tom Lynn says...

DAYS = ['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday',
        'Saturday']

def eventdate(year, month, target_day, target_ordinal):
    import sqlite3
    db = sqlite3.connect(':memory:')
    query = ("select date('%d-01-01', '%d months', '%d days', 'weekday %d');"
             % (year, month-1, 7*(target_ordinal-1), DAYS.index(target_day)))
    return db.execute(query).fetchone()[0]

Posted at 3:01 p.m. on September 24, 2008


What do you have to say?

Show Editing Help


PyCon UK

About

Hello, my name is Zeth, I'll be your host here.

Command Line Warriors is about taking control of your own technology, it looks at our experiences of computing; especially using GNU/Linux, the Python programming language, the command-line and issues such as techno-ethics, best practices and whatever is cool now. If you take control of your technology then you are a Warrior too!

This site is your site too which means that you can contribute and get involved. You can leave comments using the facility provided. For me, the comments and discussions are by far the best part of the site. So please do have your say!

Latest Discussions

Tringi

December 1, 2008
Hi, I am far from your league, but instead of [20. Nd5], why not just play Qd8? :-) Wouldn't it be only Qd8 Qd8 then, or am I missing something?
Ruy Lopez, Berlin defence, open variation part three

Tringi

November 30, 2008
...oh, I meant "Qe8 Qe8" in my previous post, sry ;-)
Ruy Lopez, Berlin defence, open variation part three

Cruze

November 29, 2008
Buy discount professional health products online.
Include ODF support in the Linux Standard Base?

Mike

November 29, 2008
>The most useful xmlstarlet tool for me has been the XML validator, >which tests whether your documents are well formed or not. You >use the tool as follows: >xmlstarlet val ...
My God, it's Full of XML

Giacomo

November 29, 2008
Er, "elif test `ls "$with_xqilla"/libxqilla*.so 2>/dev/null | wc -l` -gt 0 ; then" should now be "elif test `ls "$with_xqilla"/libxqilla.so* 2>/dev/null | wc -l` -gt 0 ; then", as the ...
Native XML storage with Berkeley DB XML - part one

Felipe Coury

November 23, 2008
What do I have to say? Only this: "THANK YOU"! Awesome!
SFTP in Python: Really Simple SSH

fmv

November 19, 2008
just a real db SAMPLE please
Native XML storage with Berkeley DB XML - part one

Very helpful

November 12, 2008
but i need more help. I'm have to execute the sudo command after I log in. What do I need to do to enter the password after the sudo command ...
SFTP in Python: Really Simple SSH

blz

November 12, 2008
I buy 99% of PEP8, except: I don't like the line spacing rules... I can't read the code when it's too close together - it looks congested and I can't ...
Twelve commandments for Beautiful Python code

Zeth

November 11, 2008
Hi Ioxs, I said above *"I will give an example of a standard directive, then an example of a third-party directive"*, so the image directive is the example of a ...
An Introduction to ReStructuredText

loxs

November 9, 2008
Hello, Are you sure about the sourcecode directive, because I didn't manage to make it work. And it doesn't work with the online renderers too.
An Introduction to ReStructuredText