Saturday, June 12, 2010

Here is Python code to get the BSP daily (Monday to Friday) key rates and other statistics. It is assumed that the web pages are stored as html files. The program is written so that it will do the job correctly. It is not elegant nor a speed demon. The output may be viewed at our Economics page:

Economic Indicators Page

# -*- coding: utf-8 -*-

from glob import *

def between(line,  before,  after):
       f = line.find(before)
       if f >= 0:
           s = line[f+len(before): ]
           return s[:s.find(after)]
       else:
           return ""
   
def linefeeder(s=None):
    if s:
       lines = s.plit("\n")
    for line in lines:
        yield line


def mdy(d):
    """
    Reformats date d to form month.day.year 
    """
    day,  month, year = d.split()
    amonths = ["January", "February",'March',  "April",  "May",  "June",  "July", "August", 
                        "September",  "October",  "November",  "December"]
    return "%02d.%02d.%s" % (amonths.index(month)+1,  int(day),  year)
   
state = 0
out = []
for fname  in glob("*bsp*.html"):
    bspdata = open(fname).read()
    s = ""
    state = 0
    for line in bspdata.split("\n"):
        if "

" in line: date = between (line, "

", "

") if date: s+=" " + mdy(date) + "" state = 1 continue if state == 1: if "keystat/day99.htm" in line: state = 2 continue if state == 2: #dollar rate. dollarrate = between(line, "right\">", "

" ) if dollarrate: s+="" + dollarrate +"" state= 3 continue if state == 3: if "bspratesdds" in line: state = 4 continue if state == 4: reporate = between(line, '

', '

') if reporate: s+=""+ reporate + "" state = 5 continue if state == 5: if "Reverse Repo Rate" in line: state = 6 continue if state == 6: revreporate= between(line, "right\">", "

") if revreporate: s+=""+ revreporate+"" state = 7 continue if state == 7: if "Inflation Rate" in line: state = 8 continue if state == 8: infrate = between(line, "right\">", "

") if infrate: s+=""+ infrate+ "" state = 9 continue if state == 9: if "NEER" in line: state = 10 continue if state == 10: NEER = between(line, "right\">", "

").strip() if NEER: s+=""+ NEER+ "" state = 11 continue if state == 11: if "91-day" in line: state = 12 continue if state == 12: d91 = between(line, "right\">", "

") if d91: s+=""+ d91+ "" state = 13 continue if state == 13: if "Gold" in line: state = 14 continue if state == 14: gold = between(line, "right\">", "

") if gold: s+=""+ gold+"" state = 15 continue if state == 15: if "Silver" in line: state = 16 continue if state == 16: silver = between(line, "right\">", "

") if silver: s+= ""+ silver +" " state = 0 out.append(s) break out.sort(reverse=True) print out[0] for i in range(len(out)): if out[i] != out[i-1]: print i, ":", out[i]

We plan to make the code cleaner by using the linefeeder function, but this can wait as we have more important things to do.

No comments:

Post a Comment