/[james]/currency/!Currency/py/Rates.py
ViewVC logotype

Contents of /currency/!Currency/py/Rates.py

Parent Directory Parent Directory | Revision Log Revision Log


Revision 12 - (show annotations) (download) (as text)
Mon Feb 10 18:25:55 2003 UTC (21 years, 8 months ago) by james
File MIME type: text/x-python
File size: 1645 byte(s)
Initial import.

1 # > !Currency.py.Rates/py
2
3 import pickle
4 import re
5 import string
6 import urllib
7
8
9 _re_date = re.compile(r"EFFECTIVE *(\d\d-\w\w\w-\d\d)")
10 _re_rate = re.compile(r"([A-Z]{3,3}) ([^0-9]+?) +([0-9.]+)? +([0-9.]+)( +)?([^0-9]*)$")
11
12
13 def load(cache):
14 global _date, _rates
15 c = open(cache, 'r')
16 _date = pickle.load(c)
17 _rates = pickle.load(c)
18 c.close()
19
20
21 def update(url, cache, us, dollar):
22 global _date, _rates
23 rates = [(us, dollar, 1.0)]
24 u = urllib.urlopen(url)
25 c = open(cache, 'w')
26 while 1:
27 l = u.readline()
28 m = _re_date.match(l)
29 if m:
30 break
31 _date = m.group(1)
32 while 1:
33 l = u.readline()
34 if l[:4] == 'CODE':
35 break
36 l = u.readline()
37 while 1:
38 l = u.readline()
39 m = _re_rate.match(l[:-2])
40 if m is None:
41 break
42 rates.append((string.capwords(string.lower(m.group(2))),
43 string.capwords(string.lower(m.group(6))),
44 eval(m.group(4))))
45 u.close()
46 pickle.dump(_date, c, 1)
47 _rates = rates
48 pickle.dump(_rates, c, 1)
49 c.close()
50
51
52 def currencies(format):
53 return map(lambda rate, format = format:
54 format % {'country': rate[0], 'currency': rate[1], 'rate': rate[2]},
55 _rates)
56
57
58 def currency(format, i):
59 return format % {'country': _rates[i][0], 'currency': _rates[i][1], 'rate': _rates[i][2]}
60
61
62 def date():
63 return _date
64
65
66 def convert(currency, value):
67 conversion = []
68 base = float(value) / _rates[currency][2]
69 for rate in _rates:
70 conversion.append(base * rate[2])
71 return conversion

  ViewVC Help
Powered by ViewVC 1.1.26