Created
May 30, 2011 06:28
-
-
Save jbking/998517 to your computer and use it in GitHub Desktop.
Generate simple year month list
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# -*- coding:utf-8 -*- | |
from datetime import date | |
class YearMonth(object): | |
""" List year/month from specified to today """ | |
def __init__(self, year, month, format='%Y/%m'): | |
self.year = year | |
self.month = month | |
self.format = format | |
def __iter__(self): | |
""" Generate year/month list dynamically. """ | |
specified_month = date(self.year, self.month, 1) | |
month = date.today().replace(day=1) | |
while month >= specified_month: | |
s = month.strftime(self.format) | |
yield (s, s) | |
y = month.year | |
m = month.month | |
if m == 1: | |
month = month.replace(year=(y - 1), month=12) | |
else: | |
month = month.replace(month=(m - 1)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment