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

Make item pickle smaller #1285

Merged
merged 3 commits into from
Dec 12, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
18 changes: 18 additions & 0 deletions pystac/item.py
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,24 @@ def __init__(
def __repr__(self) -> str:
return f"<Item id={self.id}>"

def __getstate__(self) -> dict[str, Any]:
"""Ensure that pystac does not encode too much information when pickling"""
d = self.__dict__.copy()

if all(link.get_href() for link in d["links"]):
jsignell marked this conversation as resolved.
Show resolved Hide resolved
d["links"] = [link.to_dict() for link in d["links"]]

return d

def __setstate__(self, state: dict[str, Any]) -> None:
"""Ensure that pystac knows how to decode the pickled object"""
d = state.copy()

if all(isinstance(link, dict) for link in d["links"]):
d["links"] = [Link.from_dict(link).set_owner(self) for link in d["links"]]

self.__dict__ = d

def set_self_href(self, href: str | None) -> None:
"""Sets the absolute HREF that is represented by the ``rel == 'self'``
:class:`~pystac.Link`.
Expand Down
38 changes: 38 additions & 0 deletions tests/test_item.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import json
import os
import pickle
import tempfile
import unittest
from copy import deepcopy
Expand Down Expand Up @@ -644,3 +645,40 @@ def test_invalid_error_message(item: Item) -> None:
with pytest.raises(STACValidationError) as error:
item.validate()
assert "can't have a collection" in str(error.value)


def test_pickle_with_no_links(item: Item) -> None:
roundtripped = pickle.loads(pickle.dumps(item))
for attr in ["id", "geometry", "bbox", "datetime", "links"]:
assert getattr(roundtripped, attr) == getattr(item, attr)


def test_pickle_with_hrefless_links(item: Item) -> None:
root = pystac.Catalog("root", "root")
a = pystac.Catalog("a", "a")

item.add_link(pystac.Link("related", a))
item.add_link(
pystac.Link("item", TestCases.get_path("data-files/item/sample-item.json"))
)
item.set_root(root)

roundtripped = pickle.loads(pickle.dumps(item))
for original, new in zip(item.links, roundtripped.links):
assert original.rel == new.rel
assert original.media_type == new.media_type
assert str(original.owner) == str(new.owner)
assert str(original.target) == str(new.target)


def test_pickle_with_only_href_links(item: Item) -> None:
item.add_link(
pystac.Link("item", TestCases.get_path("data-files/item/sample-item.json"))
)

roundtripped = pickle.loads(pickle.dumps(item))
for original, new in zip(item.links, roundtripped.links):
assert original.rel == new.rel
assert original.media_type == new.media_type
assert str(original.owner) == str(new.owner)
assert str(original.target) == str(new.target)