diff --git a/visualizer/plotting.py b/visualizer/plotting.py index 6166b97cb9e8d645f6c05d40373dcfc8053ed23a..a9ac17b502ae908293f530e937935cca96e004b6 100644 --- a/visualizer/plotting.py +++ b/visualizer/plotting.py @@ -10,7 +10,7 @@ import numpy as np from .data import read_data from .parameters import get_param, meas_type, year_type -from .records import get_station, read_stations +from .records import get_station plt.style.use('ggplot') plt.rcParams['axes.xmargin'] = 0 @@ -129,11 +129,10 @@ class Overlay(Plotter): def plot(cls): num_datasets = 2 datasets = tuple(SimpleNamespace() for _ in range(num_datasets)) - stations = read_stations() for n, dset in enumerate(datasets, start=1): dset.station_id = get_param(f'station{n}') dset.year = get_param(f'year{n}', to=year_type) - dset.station = get_station(dset.station_id, stations=stations) + dset.station = get_station(dset.station_id) dset.name = dset.station['name'] meas = get_param('measurement', to=meas_type) diff --git a/visualizer/records.py b/visualizer/records.py index 779f50986fd11ed5b241b7b90ae7c9570abe2aa2..44d709de6443559b479275ac7273bd8087361027 100644 --- a/visualizer/records.py +++ b/visualizer/records.py @@ -1,24 +1,24 @@ """Read and parse records of station data.""" +from functools import lru_cache import json from flask import abort +@lru_cache(maxsize=None) def read_stations(): """Read amrdcrecords.json.""" with open('amrdcrecords.json') as f: return json.load(f) -def get_station(station_id, stations=None): +def get_station(station_id): """Get a station record by ID. Calls `abort(404)` if none is found. """ - if stations is None: - stations = read_stations() - for station in stations: + for station in read_stations(): if station['id'] == station_id: return station abort(404)