Just interested in any comments about the way the code is written. I am very new to python and programming. I did think about a func with a generator, but thought this approach was better.
from datetime import date
from datetime import timedelta
import calendar
import random
class rand_dates(object):
'''
returns a date range as tuple of a random start date in the current year and an end date + random.randint(term) param.
the end date can exceed the current year.
my usage: i want a random reservation date for a hotel stay.
A base year can also be passed as a param if another year is required other than the current year
'''
def __init__(self, term = 5, year=None):
self.term = term
self.year = year
def __iter__(self):
return self
def next(self):
td = date.today()
if self.year == None:
y = td.year
else:
y = self.year
m = random.randint(1,12)
d_in_month = calendar.monthrange(y,m)[1]
d = random.randint(1,d_in_month)
sd = date(y,m,d)
ed= sd + timedelta(days=random.randint(1,self.term))
return (sd,ed)