diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index 52fae4a..099c678 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -8,7 +8,7 @@ repos: - id: check-yaml - id: check-added-large-files - repo: https://github.com/astral-sh/ruff-pre-commit - rev: v0.5.7 + rev: v0.6.7 hooks: - id: ruff args: [--fix] diff --git a/README.md b/README.md index ea70093..1e80a5a 100644 --- a/README.md +++ b/README.md @@ -102,7 +102,7 @@ Works()["W2741809807"]["open_access"] The previous works also for Authors, Sources, Institutions, Concepts and Topics ```python -Authors()["A2887243803"] +Authors()["A5027479191"] Authors()["https://orcid.org/0000-0002-4297-0502"] # same ``` @@ -115,7 +115,6 @@ Works().random() Authors().random() Sources().random() Institutions().random() -Concepts().random() Topics().random() Publishers().random() Funders().random() @@ -354,11 +353,27 @@ Works()["W2023271753"].ngrams() ``` +### Serialize + +All results from PyAlex can be serialized. For example, save the results to a JSON file: + +```python +import json +from pathlib import Path +from pyalex import Work + +with open(Path("works.json"), "w") as f: + json.dump(Works().get(), f) + +with open(Path("works.json")) as f: + works = [Work(w) for w in json.load(f)] +``` + ## Code snippets A list of awesome use cases of the OpenAlex dataset. -### Cited publications (referenced works) +### Cited publications (works referenced by this paper, outgoing citations) ```python from pyalex import Works @@ -369,6 +384,13 @@ w = Works()["W2741809807"] Works()[w["referenced_works"]] ``` +### Citing publications (other works that reference this paper, incoming citations) + +``` +from pyalex import Works +Works().filter(cites="W2741809807").get() +``` + ### Get works of a single author ```python @@ -427,6 +449,7 @@ R users can use the excellent [OpenAlexR](https://github.com/ropensci/openalexR) > This library is a community contribution. The authors of this Python library aren't affiliated with OpenAlex. +This library is maintained by [J535D165](https://github.com/J535D165) and [PeterLombaers](https://github.com/PeterLombaers). Feel free to reach out with questions, remarks, and suggestions. The -[issue tracker](/issues) is a good starting point. You can also email me at +[issue tracker](/issues) is a good starting point. You can also reach out via [jonathandebruinos@gmail.com](mailto:jonathandebruinos@gmail.com). diff --git a/pyalex/api.py b/pyalex/api.py index 44e254b..61ef20d 100644 --- a/pyalex/api.py +++ b/pyalex/api.py @@ -23,7 +23,7 @@ def __setattr__(self, key, value): config = AlexConfig( email=None, api_key=None, - user_agent="pyalex/{__version__}", + user_agent=f"pyalex/{__version__}", openalex_url="https://api.openalex.org", max_retries=0, retry_backoff_factor=0.1, diff --git a/tests/test_pyalex.py b/tests/test_pyalex.py index 2820a0c..d4cc772 100644 --- a/tests/test_pyalex.py +++ b/tests/test_pyalex.py @@ -216,7 +216,8 @@ def test_referenced_works(): ) assert set([w["id"] for w in w_new]).difference(set(w["referenced_works"])) == set() - # assert set(w["referenced_works"]).difference(set([w["id"] for w in w_new])) == set() + # assert set(w["referenced_works"]).difference(set([w["id"] for w in w_new])) + # == set() assert m["count"] < len(w["referenced_works"]) @@ -252,12 +253,25 @@ def test_serializable(tmpdir): assert "W4238809453" in json.load(f)["id"] +def test_serializable_list(tmpdir): + with open(Path(tmpdir, "test.json"), "w") as f: + json.dump(Works().get(), f) + + with open(Path(tmpdir, "test.json")) as f: + works = [Work(w) for w in json.load(f)] + + assert len(works) == 25 + assert all(isinstance(w, Work) for w in works) + + +@pytest.mark.skip("This test is not working due to unavailable API.") def test_ngrams_without_metadata(): r = Works()["W2023271753"].ngrams(return_meta=False) assert len(r) == 1068 +@pytest.mark.skip("This test is not working due to unavailable API.") def test_ngrams_with_metadata(): r, meta = Works()["W2023271753"].ngrams(return_meta=True)