Skip to content

Commit

Permalink
utils: make load_schema faster through memoization
Browse files Browse the repository at this point in the history
Signed-off-by: Micha Moskovic <michamos@gmail.com>
  • Loading branch information
michamos committed Nov 25, 2020
1 parent cac063c commit 9ce4557
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 4 deletions.
20 changes: 16 additions & 4 deletions inspire_schemas/utils.py
Expand Up @@ -660,20 +660,32 @@ def _schema_to_normalized_path(schema):
raise SchemaNotFound(schema=schema)


def load_schema(schema_name, resolved=False):
def load_schema(schema_name, resolved=False, _cache={}):
"""Load the given schema from wherever it's installed.
Args:
schema_name(str): Name of the schema to load, for example 'authors'.
resolved(bool): If True will return the resolved schema, that is with
all the $refs replaced by their targets.
_cache(dict): Private argument used for memoization.
Returns:
dict: the schema with the given name.
"""
schema_data = ''
with open(get_schema_path(schema_name, resolved)) as schema_fd:
schema_data = json.loads(schema_fd.read())
if schema_name in _cache:
return _cache[schema_name]

schema_path = get_schema_path(schema_name, resolved)
if schema_path in _cache:
schema_data = _cache[schema_path]
_cache[schema_name] = schema_data
return schema_data

with open(schema_path) as schema_fd:
schema_data = json.load(schema_fd)

_cache[schema_name] = schema_data
_cache[schema_path] = schema_data

return schema_data

Expand Down
7 changes: 7 additions & 0 deletions tests/unit/test_utils.py
Expand Up @@ -230,6 +230,13 @@ def test_load_schema_with_schema_key(mock_get_schema_path, mock_open):
assert loaded_schema == myschema


def test_load_schema_uses_memoization():
schema = utils.load_schema("authors")
schema2 = utils.load_schema("authors.json")

assert schema is schema2


def test_split_page_artid_page_range():
page_string = '451-487'
result = utils.split_page_artid(page_string)
Expand Down

0 comments on commit 9ce4557

Please sign in to comment.