returns a iterator of months in the last year starting from this month

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
def month_iter(format):
"""
returns a iterator of months in the last year starting from this month
Example:
>>> m = month_iter('%b %Y')
>>> m.next()
'Aug 2009'
>>> m.next()
'Sep 2009'
"""
now = datetime.now()
year = now.year - 1
for month in xrange(now.month, now.month + 13):
if month > 12:
month -= 12
year = now.year
date = datetime(year, month, 1)
yield date.strftime(format)