Skip to content

Commit

Permalink
Add support for multi-item lookup
Browse files Browse the repository at this point in the history
  • Loading branch information
J535D165 committed Dec 9, 2022
1 parent 34a262e commit 036e174
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 2 deletions.
8 changes: 6 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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}
```

Expand Down Expand Up @@ -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
Expand Down
7 changes: 7 additions & 0 deletions pyalex/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
8 changes: 8 additions & 0 deletions tests/test_pyalex.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down

0 comments on commit 036e174

Please sign in to comment.