Q:

python best way to determine time ranges

Forgive a novice question, but I have the following snippet in code:
timePeriod = '7'
timeDiff = today - dateLast
if timeDiff.days > 90:
    timePeriod = '180'
elif timeDiff.days > 30:
    timePeriod = '90'
elif timeDiff.days > 7:
    timePeriod = '30'
# Use timePeriod in call...

And it just seems terribly inefficient.  There must be a better way to assign timePeriod using the datetime library, but I haven't found one.  Part of the problem is that the code is so generic, searching for answers yields thousands of unrelated results.  I've contemplated:
timePeriod = '180' if timeDiff.days > 90 else '90' if timeDiff.days > 30 else... 

But this doesn't utilize the power of python that I've seen elsewhere.
Is there an obvious 'correct' solution here that I'm not seeing?  Or is my original solution 'good enough' and I shouldn't be messing with it?
Much thanks!

A:

Hat tip to J.F. Sebastian who pointed me in this direction.  To anyone who's interested in the solution I went with:
from bisect import bisect
...
periods = [0, 7, 30, 90, 180]
timeDiff = (today - dateLast).days
timePeriod = str(periods[bisect(periods[:-1], timeDiff)])

This is tested for all positive values of timeDiff (the [:-1] caps the result to '180', prevents out of bounds reference).

