ming2281
2015-06-08 20:33:12 +08:00
缓存一个URL在模块级别,题主是这样写的吗?
```
_URL = xxx
def get(refresh=False):
if refresh:
get.reates = {}
if get.retes:
return get.rates
with urllib.request.urlopen(_URL) as f:
for line in f:
line = line.rstrip().decode('utf-8')
if not line or line.startswith(("#", "Date")):
continue
name, currency,*rest = re.split(r"\s*,\s*, line)
key = "{}{})".format(name, currency)
try:
get.rates[key] = float(rest[-1])
except ValueError as err:
print "error:{}: {}".format(err, line)
return get.rates
get.rates = {}
```