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

Use slots in common classes #1434

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
### Changed

- Write STAC v1.1.0 ([#1427](https://github.com/stac-utils/pystac/pull/1427))
- Use `__slots__` for objects we expect to exist in large numbers ([#1434](https://github.com/stac-utils/pystac/pull/1434))

## [v1.11.0] - 2024-09-26

Expand Down
10 changes: 10 additions & 0 deletions pystac/asset.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,16 @@ class Asset:
object JSON.
"""

__slots__: tuple[str, ...] = (
"href",
"title",
"description",
"media_type",
"roles",
"owner",
"extra_fields",
)

href: str
"""Link to the asset object. Relative and absolute links are both allowed."""

Expand Down
14 changes: 13 additions & 1 deletion pystac/catalog.py
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,16 @@ class Catalog(STACObject):
:class:`~pystac.layout.BestPracticesLayoutStrategy`.
"""

__slots__: tuple[str, ...] = STACObject.__slots__ + (
"catalog_type",
"description",
"extra_fields",
"title",
"_resolved_objects",
"_stac_io",
"strategy",
)

catalog_type: CatalogType
"""The catalog type. Defaults to :attr:`CatalogType.ABSOLUTE_PUBLISHED`."""

Expand All @@ -159,7 +169,7 @@ class Catalog(STACObject):

STAC_OBJECT_TYPE = pystac.STACObjectType.CATALOG

_stac_io: pystac.StacIO | None = None
_stac_io: pystac.StacIO | None
"""Optional instance of StacIO that will be used by default
for any IO operations on objects contained by this catalog.
Set while reading in a catalog. This is set when a catalog
Expand Down Expand Up @@ -207,6 +217,8 @@ def __init__(

self._resolved_objects.cache(self)

self._stac_io = None

def __repr__(self) -> str:
return f"<Catalog id={self.id}>"

Expand Down
9 changes: 9 additions & 0 deletions pystac/collection.py
Original file line number Diff line number Diff line change
Expand Up @@ -479,6 +479,15 @@ class Collection(Catalog, Assets):
:class:`~pystac.layout.BestPracticesLayoutStrategy`.
"""

__slots__: tuple[str, ...] = Catalog.__slots__ + (
"extent",
"license",
"keywords",
"providers",
"summaries",
"assets",
)

description: str
"""Detailed multi-line description to fully explain the collection."""

Expand Down
43 changes: 30 additions & 13 deletions pystac/item.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,18 @@ class Item(STACObject, Assets):
:attr:`~pystac.Asset.owner` attribute set to the created Item.
"""

__slots__: tuple[str, ...] = STACObject.__slots__ + (
"assets",
"bbox",
"collection",
"collection_id",
"datetime",
"extra_fields",
"geometry",
"links",
"properties",
)

assets: dict[str, Asset]
"""Dictionary of :class:`~pystac.Asset` objects, each with a unique key."""

Expand Down Expand Up @@ -157,7 +169,8 @@ def __init__(
if href is not None:
self.set_self_href(href)

self.collection_id: str | None = None
self.collection_id = None
self.collection = None
if collection is None:
self.collection = None
else:
Expand All @@ -175,30 +188,34 @@ 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()
"""Ensure that pystac does not encode too much information when pickling."""
state = {slot: getattr(self, slot) for slot in self.__slots__}

d["links"] = [
state["links"] = [
(
link.to_dict(transform_href=False)
if link.get_href(transform_href=False)
else link
)
for link in d["links"]
for link in state["links"]
]

return d
return state

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

d["links"] = [
Link.from_dict(link).set_owner(self) if isinstance(link, dict) else link
for link in d["links"]
]
for slot in self.__slots__:
if slot == "links":
value = [
Link.from_dict(link).set_owner(self)
if isinstance(link, dict)
else link
for link in state["links"]
]
else:
value = state.get(slot) # type: ignore

self.__dict__ = d
setattr(self, slot, value)

def set_self_href(self, href: str | None) -> None:
"""Sets the absolute HREF that is represented by the ``rel == 'self'``
Expand Down
10 changes: 10 additions & 0 deletions pystac/link.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,16 @@ class Link(PathLike):
object JSON.
"""

__slots__: tuple[str, ...] = (
"rel",
"media_type",
"extra_fields",
"owner",
"_target_href",
"_target_object",
"_title",
)

rel: str | pystac.RelType
"""The relation of the link (e.g. 'child', 'item'). Registered rel Types are
preferred. See :class:`~pystac.RelType` for common media types."""
Expand Down
10 changes: 9 additions & 1 deletion pystac/stac_object.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,13 @@ class STACObject(ABC):
functionality through the implementing classes.
"""

__slots__: tuple[str, ...] = (
"id",
"links",
"stac_extensions",
"_allow_parent_to_override_href",
)

id: str
"""The ID of the STAC Object."""

Expand All @@ -53,12 +60,13 @@ class STACObject(ABC):

STAC_OBJECT_TYPE: STACObjectType

_allow_parent_to_override_href: bool = True
_allow_parent_to_override_href: bool
"""Private attribute for whether parent objects should override on normalization"""

def __init__(self, stac_extensions: list[str]) -> None:
self.links = []
self.stac_extensions = stac_extensions
self._allow_parent_to_override_href = True

def validate(
self,
Expand Down