Skip to content

Commit 1b736fe

Browse files
authored
Add test and docs on serializing results (#49)
1 parent 391c16a commit 1b736fe

File tree

2 files changed

+23
-0
lines changed

2 files changed

+23
-0
lines changed

README.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -354,6 +354,18 @@ Works()["W2023271753"].ngrams()
354354
```
355355

356356

357+
### Serialize
358+
359+
All results from PyAlex can be serialized. For example, save the results to a JSON file:
360+
361+
```python
362+
with open(Path("works.json"), "w") as f:
363+
json.dump(Works().get(), f)
364+
365+
with open(Path("works.json")) as f:
366+
works = [Work(w) for w in json.load(f)]
367+
```
368+
357369
## Code snippets
358370

359371
A list of awesome use cases of the OpenAlex dataset.

tests/test_pyalex.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -244,6 +244,17 @@ def test_serializable(tmpdir):
244244
assert "W4238809453" in json.load(f)["id"]
245245

246246

247+
def test_serializable_list(tmpdir):
248+
with open(Path(tmpdir, "test.json"), "w") as f:
249+
json.dump(Works().get(), f)
250+
251+
with open(Path(tmpdir, "test.json")) as f:
252+
works = [Work(w) for w in json.load(f)]
253+
254+
assert len(works) == 25
255+
assert all(isinstance(w, Work) for w in works)
256+
257+
247258
def test_ngrams_without_metadata():
248259
r = Works()["W2023271753"].ngrams(return_meta=False)
249260

0 commit comments

Comments
 (0)