Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

back gensim.downloader.load_info function by a cache #2545

Merged
merged 8 commits into from
Jul 7, 2019
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 31 additions & 1 deletion gensim/downloader.py
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,36 @@ def _calculate_md5_checksum(fname):
return hash_md5.hexdigest()


def _load_info(url=DATA_LIST_URL, encoding='utf-8'):
"""Load dataset information from the network.

If the network access fails, fall back to a local cache. This cache gets
updated each time a network request _succeeds_.
"""
cache_path = os.path.join(base_dir, 'information.json')

try:
info_bytes = urlopen(DATA_LIST_URL).read()
except (OSError, IOError) as err:
#
# The exception raised by urlopen differs between Py2 and Py3.
#
# https://docs.python.org/3/library/urllib.error.html
# https://docs.python.org/2/library/urllib.html
#
logger.error('caught non-fatal exception, see trace below')
logger.exception(err)
mpenkov marked this conversation as resolved.
Show resolved Hide resolved
logger.error('attempting to recover from local cache (%r)', cache_path)
mpenkov marked this conversation as resolved.
Show resolved Hide resolved
else:
with open(cache_path, 'wb') as fout:
piskvorky marked this conversation as resolved.
Show resolved Hide resolved
fout.write(info_bytes)

return json.loads(info_bytes.decode(encoding))
mpenkov marked this conversation as resolved.
Show resolved Hide resolved

with open(cache_path, 'rb') as fin:
mpenkov marked this conversation as resolved.
Show resolved Hide resolved
return json.loads(fin.read().decode(encoding))


def info(name=None, show_only_latest=True, name_only=False):
"""Provide the information related to model/dataset.

Expand Down Expand Up @@ -204,7 +234,7 @@ def info(name=None, show_only_latest=True, name_only=False):
>>> api.info() # retrieve information about all available datasets and models

"""
information = json.loads(urlopen(DATA_LIST_URL).read().decode("utf-8"))
information = _load_info()

if name is not None:
corpora = information['corpora']
Expand Down