From 036e1746a879be324e812485c83d373d144319ff Mon Sep 17 00:00:00 2001 From: Jonathan de Bruin Date: Fri, 9 Dec 2022 20:57:59 +0100 Subject: [PATCH] Add support for multi-item lookup --- README.md | 8 ++++++-- pyalex/api.py | 7 +++++++ tests/test_pyalex.py | 8 ++++++++ 3 files changed, 21 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 1fdf6ab..ed2c459 100644 --- a/README.md +++ b/README.md @@ -124,14 +124,18 @@ Please respect the legal constraints when using this feature. ### Get lists of entities +```python +results = Works().get() +``` + For list of enities, you can return the result as well as the metadata. By default, only the results are returned. ```python results, meta = Concepts().get(return_meta=True) -print(meta) ``` ```python +print(meta) {'count': 65073, 'db_response_time_ms': 16, 'page': 1, 'per_page': 25} ``` @@ -235,7 +239,7 @@ from pyalex import Works # the work to extract the referenced works of w = Works()["W2741809807"] -Works().filter(openalex_id="|".join(w["referenced_works"])).get() +Works()[w["referenced_works"]] ``` ### Dataset publications in the global south diff --git a/pyalex/api.py b/pyalex/api.py index 36df0d1..2590dfb 100644 --- a/pyalex/api.py +++ b/pyalex/api.py @@ -135,8 +135,15 @@ def __init__(self, params={}): self.params = params + def _get_multi_items(self, record_list): + + return self.filter(openalex_id="|".join(record_list)).get() + def __getitem__(self, record_id): + if isinstance(record_id, list): + return self._get_multi_items(record_id) + url = self.url + "/" + record_id res = requests.get( diff --git a/tests/test_pyalex.py b/tests/test_pyalex.py index a7464c5..aced28b 100644 --- a/tests/test_pyalex.py +++ b/tests/test_pyalex.py @@ -86,6 +86,14 @@ def test_random_works(): assert isinstance(Works().random(), dict) +def test_multi_works(): + + # the work to extract the referenced works of + w = Works()["W2741809807"] + + assert len(Works()[w["referenced_works"]]) == 25 + + def test_works_multifilter(): r = requests.get(