Skip to content

Commit 83fe44e

Browse files
committed
Add support for count method
1 parent ac4dd37 commit 83fe44e

File tree

3 files changed

+26
-9
lines changed

3 files changed

+26
-9
lines changed

README.md

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,16 @@ Please respect the legal constraints when using this feature.
133133
results = Works().get()
134134
```
135135

136-
For list of entities, you can return the result as well as the metadata. By default, only the results are returned.
136+
For lists of entities, you can also `count` the number of records found
137+
instead of returning the results. This also works for search queries and
138+
filters.
139+
140+
```python
141+
Works().count()
142+
# 10338153
143+
```
144+
145+
For lists of entities, you can return the result as well as the metadata. By default, only the results are returned.
137146

138147
```python
139148
results, meta = Concepts().get(return_meta=True)

pyalex/api.py

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -252,7 +252,10 @@ def url(self):
252252
if l_params:
253253
return self.url_collection + "?" + "&".join(l_params)
254254

255-
return self.url_collection
255+
def count(self):
256+
_, m = self.get(return_meta=True, per_page=1)
257+
258+
return m["count"]
256259

257260
def get(self, return_meta=False, page=None, per_page=None, cursor=None):
258261

@@ -295,21 +298,21 @@ def get(self, return_meta=False, page=None, per_page=None, cursor=None):
295298
return results
296299

297300
def paginate(self, per_page=None, cursor="*", n_max=10000):
298-
"""Used for paging results of large responses using cursor paging.
299-
300-
OpenAlex offers two methods for paging: basic paging and cursor paging.
301-
Both methods are supported by PyAlex, although cursor paging seems to be
301+
"""Used for paging results of large responses using cursor paging.
302+
303+
OpenAlex offers two methods for paging: basic paging and cursor paging.
304+
Both methods are supported by PyAlex, although cursor paging seems to be
302305
easier to implement and less error-prone.
303306
304307
Args:
305308
per_page (_type_, optional): Entries per page to return. Defaults to None.
306309
cursor (str, optional): _description_. Defaults to "*".
307-
n_max (int, optional): Number of max results (not pages) to return.
310+
n_max (int, optional): Number of max results (not pages) to return.
308311
Defaults to 10000.
309312
310313
Returns:
311-
CursorPaginator: Iterator to use for returning and processing each page
312-
result in sequence.
314+
CursorPaginator: Iterator to use for returning and processing each page
315+
result in sequence.
313316
"""
314317
return CursorPaginator(self, per_page=per_page, cursor=cursor, n_max=n_max)
315318

tests/test_pyalex.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,11 @@ def test_works():
5252
assert len(Works().filter(publication_year=2020).get()) == 25
5353

5454

55+
def test_works_count():
56+
57+
assert Works().filter(publication_year=2020).count() > 10_000_000
58+
59+
5560
def test_per_page():
5661

5762
assert len(Works().filter(publication_year=2020).get(per_page=200)) == 200

0 commit comments

Comments
 (0)