Texture related docstring improvements by einarf · Pull Request #2299 · pythonarcade/arcade · GitHub
Skip to content
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
10 changes: 6 additions & 4 deletions arcade/cache/__init__.py
28 changes: 19 additions & 9 deletions arcade/cache/hit_box.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,11 @@ def get(self, name_or_texture: str | Texture) -> Point2List | None:
# Get a cache entry by string
points = cache.get("hash|(0, 1, 2, 3)|simple|")

:param keys: List of keys to use for the cache entry
:param hit_box_algorithm: The hit box algorithm used
Args:
keys:
The texture or cache name to get the hit box for
hit_box_algorithm:
The hit box algorithm used
"""
from arcade import Texture

Expand All @@ -81,8 +84,11 @@ def put(self, name_or_texture: str | Texture, points: Point2List) -> None:
# Cache with custom string
cache.put("my_custom_points", points)

:param keys: List of keys to use for the cache entry
:param points: The hit box points
Args:
keys:
The texture or cache name to store the hit box for
points:
The hit box points
"""
from arcade import Texture

Expand All @@ -105,6 +111,9 @@ def load(self, path: str | Path) -> None:
entries and can therefore be called multiple times to populate it.

if the file extension is ".gz" the file will be compressed.

Args:
path: The path to the json file to load
"""
path = resolve(path)
if path.suffix == ".gz":
Expand All @@ -126,8 +135,11 @@ def save(self, path: Path, indent: int = 0) -> None:

if the file extension is ".gz" the file will be compressed.

:param path: The path to save the cache to
:param indent: The indentation level for the json file
Args:
path:
The path to save the cache to
indent:
The indentation level for the json file
"""
if indent == 0:
data_str = json.dumps(self._entries)
Expand All @@ -143,9 +155,7 @@ def save(self, path: Path, indent: int = 0) -> None:
fd.write(data)

def flush(self) -> None:
"""
Clear the cache.
"""
"""Clear the cache."""
self._entries.clear()

def __repr__(self) -> str:
Expand Down
18 changes: 12 additions & 6 deletions arcade/cache/image_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,9 @@ def put(self, name: str, image: "ImageData"):
An entry can only be cached as either strong or weak, not both.
If and existing entry is found, it will be replaced.

:param name: Name of the image
:param image: ImageData object
Args:
name: Name of the image
image: ImageData object
"""
from arcade.texture import ImageData

Expand All @@ -46,17 +47,22 @@ def get(self, name: str) -> ImageData | None:
"""
Attempts to retrieve an entry from the cache.

:param name: Name of the image
:return: ImageData object or None if not found
Args:
name: Name of the image
Returns:
ImageData instance or ``None`` if not found
"""
return self._entries.get(name)

def delete(self, name: str, raise_if_not_exist: bool = False) -> None:
"""
Attempts to delete an entry from the cache.

:param name: Name of the image
:param raise_if_not_exist: If True, raises KeyError if the entry does not exist
Args:
name:
Name of the image
raise_if_not_exist:
If ``True``, raises ``KeyError`` if the entry does not exist
"""
try:
del self._entries[name]
Expand Down
64 changes: 55 additions & 9 deletions arcade/cache/texture.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,25 +19,59 @@ def __init__(self):
self._entries: dict[str, Texture] = {}

def put(self, name: str, texture: Texture) -> None:
"""
Add a texture to the cache.

Args:
name:
The cache name of the texture
texture:
The texture to add
"""
self._entries[name] = texture

def get(self, name: str) -> Texture | None:
"""
Get a texture from the cache by cache name.

Args:
name:
The cache name of the texture
Returns:
The texture if found, otherwise ``None``
"""
return self._entries.get(name)

def delete(self, name: str, raise_if_not_exist: bool = True) -> None:
"""
Delete a texture from the cache by cache name.

Args:
name:
The cache name of the texture
raise_if_not_exist:
If ``True``, raises ``KeyError`` if the entry does not exist
"""
try:
del self._entries[name]
except KeyError:
if raise_if_not_exist:
raise

def delete_by_value(self, texture: "Texture") -> None:
"""
Delete a texture from the cache by texture instance.

Args:
texture: The texture instance to delete
"""
for name, value in self._entries.items():
if value is texture:
del self._entries[name]
return

def flush(self) -> None:
"""Clear the cache"""
self._entries.clear()

def __len__(self) -> int:
Expand Down Expand Up @@ -72,7 +106,8 @@ def put(self, texture: "Texture") -> None:
and file path are correctly set on the texture before adding it to
the cache.

:param texture: The texture to add
Args:
texture: The texture to add
"""
self._entries.put(texture.cache_name, texture)

Expand All @@ -87,18 +122,24 @@ def get(self, name: str) -> Texture | None:
"""
Get a texture from the cache by cache name

:param name: The cache name of the texture
:return: The texture if found, otherwise None
Args:
name: The cache name of the texture
Returns:
The texture if found, otherwise ``None``
"""
return self._entries.get(name)

def get_with_config(self, hash: str, hit_box_algorithm: "HitBoxAlgorithm") -> Texture | None:
"""
Attempts to find a texture with a specific configuration.

:param hash: The image hash
:param hit_box_algorithm: The hit box algorithm to search for
:return: The texture if found, otherwise None
Args:
hash:
The image hash
hit_box_algorithm:
The hit box algorithm to search for
Returns:
The texture if found, otherwise ``None``
"""
from arcade import Texture

Expand All @@ -116,7 +157,9 @@ def get_texture_by_filepath(
"""
Get a texture from the cache by file path and crop values.

:param file_path: The path to the file the texture was loaded from
Args:
file_path: The path to the file the texture was loaded from
crop: The crop values used when creating the texture
"""
from arcade import Texture

Expand All @@ -127,8 +170,11 @@ def delete(self, texture_or_name: Texture | str, raise_if_not_exist: bool = Fals
"""
Delete a texture from the cache by cache name.

:param texture_or_name: The texture or cache name to delete
:param ignore_error: If True, ignore errors when deleting
Args:
texture_or_name:
The texture or cache name to delete
raise_if_not_exist:
If ``True``, ignore errors when deleting
"""
if isinstance(texture_or_name, Texture):
texture = texture_or_name
Expand Down
16 changes: 8 additions & 8 deletions arcade/hitbox/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,8 @@ def calculate_hit_box_points_simple(image: Image, *args) -> Point2List:
Given an RGBA image, this returns points that make up a hit box around it. Attempts
to trim out transparent pixels.

:param image: Image get hit box from.

:Returns: List of points
Args:
image: Image get hit box from.
"""
return algo_simple.calculate(image)

Expand All @@ -40,11 +39,12 @@ def calculate_hit_box_points_detailed(
Given an RGBA image, this returns points that make up a hit box around it. Attempts
to trim out transparent pixels.

:param image: Image get hit box from.
:param hit_box_detail: How detailed to make the hit box. There's a
trade-off in number of points vs. accuracy.

:Returns: List of points
Args:
image:
Image get hit box from.
hit_box_detail:
How detailed to make the hit box. There's a
trade-off in number of points vs. accuracy.
"""
return algo_detailed.calculate(image, detail=hit_box_detail)

Expand Down
51 changes: 34 additions & 17 deletions arcade/hitbox/base.py
Loading