[Tutor] Assigning function keywords dynamically

Charlie Clark charlie@begeistert.org
Sun Jan 5 18:13:14 2003


Dear list,

I remember a discussion about switch/case statements in Python (I know they=
 
only exist in C) including my own contribution but haven't been able to 
find quite what I was looking for my usual sources.

I have got a little report generator that generates reports for people with=
 
different frequency (daily, weekly, monthly). What I want is a function 
that takes the frequency and returns the correct future date for the next 
run.

current implementation using mx.DateTime

from mx.DateTime import now(), DateTime.RelativeDateTime

def new_date(interval):
=09if interval =3D=3D "monatlich":
=09=09return now() + RelativeDateTime(months=3D+1)
=09elif interval =3D=3D "w=F6chentlich":
=09=09return now() + RelativeDateTime(weeks=3D+1)
=09elif interval =3D=3D "t=E4glich":
=09=09return now() + RelativeDateTime(days=3D+1)
=09else:
=09=09return "Invalid interval"

While this is fine it seems a little verbose and clumsy and not quite 
Python. What would be a better way of doing this? Also would it be a good 
idea to raise an exception in the else statement?

Thanx very much

Charlie