Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add test and docs on serializing results #49

Merged
merged 1 commit into from
Aug 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -354,6 +354,18 @@ Works()["W2023271753"].ngrams()
```


### Serialize

All results from PyAlex can be serialized. For example, save the results to a JSON file:

```python
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.
Expand Down
11 changes: 11 additions & 0 deletions tests/test_pyalex.py
Original file line number Diff line number Diff line change
Expand Up @@ -244,6 +244,17 @@ 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)


def test_ngrams_without_metadata():
r = Works()["W2023271753"].ngrams(return_meta=False)

Expand Down
Loading