Switch statements again

Andrew Dalke adalke at mindspring.com
Wed Jan 15 18:12:16 EST 2003


Steven Scott wrote:
 >  my question is simply "what's the best way to do this in python?"

day_offsets = {
   MASK_SUNDAY: 6,
   MASK_MONDAY: 5,
   MASK_TUESDAY: 4,
   ...
   MASK_SATURDAY: 0,
}

  ...

try:
   dayOfWeek -= day_offsets[calDayOfWeekMask]
except KeyError:
   return FAIL

if calWeekNumber == LAST:
   day = occurence.getDaysInMonth()
   add_days = -((7 - dayOfWeek +
                 occurence.getDayOfWeek()) % 7)
else:
   day = 1 + ((calWeekNumber - 1) *7
   add_days = dayOfWeek + 7 - occurrence.getDayOfWeek()) % 7

occurence.setDay(day)
occurence.addDays(add_days)


I prefer to move the "setDay" and "addDays" method calls to
the end because it's easier to see that both routes end
up changing the occurence the same way.  YMMV.

					Andrew
					dalke at dalkescientific.com





More information about the Python-list mailing list