Skip to content

Commit

Permalink
Add test and docs on serializing results (#49)
Browse files Browse the repository at this point in the history
  • Loading branch information
J535D165 authored Aug 26, 2024
1 parent 391c16a commit 1b736fe
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 0 deletions.
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

0 comments on commit 1b736fe

Please sign in to comment.