This script calulates rpietila's trendline for any given date >= 2009-01. I hope it works as expected

I made a minor tweak to make this compatible with python 2.6 and earlier. (printf needs a positional argument specified explicitly ('{0}' rather than '{}'))
import argparse
from calendar import monthrange
from datetime import date, datetime
a = 0.092
b = -2.9124
def get_x(year, month=None, day=None):
assert(2009 <= year)
if month is None:
assert(day is None)
month = 6.5
numof_months = (year - 2009) * 12 + month
if day is None:
return numof_months
numof_days = monthrange(year, month)[1]
return numof_months + (day - 0.5) / numof_days - 0.5
def price(dt):
x = get_x(*dt)
return 10 ** (a * x + b)
def date_tuple(str):
month, day = None, None
try:
dt = datetime.strptime(str, '%Y-%m-%d')
year, month, day = dt.year, dt.month, dt.day
except ValueError:
try:
dt = datetime.strptime(str, '%Y-%m')
year, month = dt.year, dt.month
except ValueError:
dt = datetime.strptime(str, '%Y')
year = dt.year
return year, month, day
def main():
parser = argparse.ArgumentParser()
parser.add_argument('date', type=date_tuple, nargs='?',
help='YYYY-MM-DD or YYYY-MM or YYYY')
args = parser.parse_args()
if args.date is None:
dt = date.today()
args.date = (dt.year, dt.month, dt.day)
print '{0:.2f}'.format(price(args.date))
if __name__ == '__main__':
main()