Cache

arcade.cache.crate_str_from_values(*args, sep: str = '_') str[source]

Create a string from a list of parameters.

Example:

>> entries = ["blue", 5]
>> crate_str_from_list("blue", 5)
"blue_5"
Parameters:
  • params – List of parameters to create a string from.

  • sep – Separator to use between parameters.

arcade.cache.crate_str_from_list(entries: list[Any], sep: str = '_') str[source]

Create a string from a list of parameters.

Example:

>> entries = ["blue", 5]
>> crate_str_from_list(entries)
"blue_5"
Parameters:
  • entries – List of parameters to create a string from.

  • sep – Separator to use between parameters.

class arcade.cache.hit_box.HitBoxCache[source]

Bases:

A simple cache for hit box points for textures.

These are calculated when loading a texture depending on the selected hit box algorithm.

Points are stored as a tuple of xy points since it’s important that they are immutable.

VERSION = 1
flush() None[source]

Clear the cache.

get(name_or_texture: str | Texture) Point2List | None[source]

Get the hit box points for a texture with a given hash and hit box algorithm.

Example:

# Get cached hit box for texture
points = cache.get(texture)
# Get a cache entry by string
points = cache.get("hash|(0, 1, 2, 3)|simple|")
Parameters:

name_or_texture – The texture or cache name to get the hit box for

load(path: str | Path) None[source]

Load a json file containing hit boxes.

This adds the loaded hit boxes to the cache overwriting any existing entries and can therefore be called multiple times to populate it.

if the file extension is “.gz” the file will be compressed.

Parameters:

path – The path to the json file to load

put(name_or_texture: str | Texture, points: Point2List) None[source]

Store hit box points for a texture.

Example:

# Cache hit box for texture
cache.put(texture, points)
# Cache with custom string
cache.put("my_custom_points", points)
Parameters:
  • name_or_texture – The texture or cache name to store the hit box for

  • points – The hit box points

save(path: Path, indent: int = 0) None[source]

Save the hit box cache to disk.

This can be used to pre-populate the cache to reduce the time it takes to load textures.

if the file extension is “.gz” the file will be compressed.

Parameters:
  • path – The path to save the cache to

  • indent – The indentation level for the json file

class arcade.cache.texture.TextureBucket[source]

Bases:

A simple dict based cache for textures.

delete(name: str, raise_if_not_exist: bool = True) None[source]

Delete a texture from the cache by cache name.

Parameters:
  • name – The cache name of the texture

  • raise_if_not_exist – If True, raises KeyError if the entry does not exist

delete_by_value(texture: Texture) None[source]

Delete a texture from the cache by texture instance.

Parameters:

texture – The texture instance to delete

flush() None[source]

Clear the cache

get(name: str) Texture | None[source]

Get a texture from the cache by cache name.

Parameters:

name – The cache name of the texture

Returns:

The texture if found, otherwise None

put(name: str, texture: Texture) None[source]

Add a texture to the cache.

Parameters:
  • name – The cache name of the texture

  • texture – The texture to add

class arcade.cache.texture.TextureCache[source]

Bases:

A cache for Arcade textures.

The creation of a texture is an expensive operation for several reasons. * Loading an image from disk has a cost * Converting the image to a format suitable for OpenGL has a cost * Calculating the hash of the image has a cost * Creating the texture instance has a cost * Once the texture is created it’s expensive to calculate hit box points

This cache is intended to reduce the cost of creating textures by reusing existing ones. We also re-use the internal images on the existing textures for making different configurations of the same texture such as flipped and rotated versions including textures with different hit box configurations for the same image.

🧙 texture: Texture in self bool[source]

Check if a texture is in the cache

🧙 del self[name: str] None[source]

Delete a texture from the cache by cache name

🧙 self[name: str] Texture | None[source]

Get a texture from the cache by cache name

🧙 iter(self)[source]

Iterate over all unique textures

🧙 len(self) int[source]

Count the number of unique textures

🧙 self[name: str] = texture: Texture None[source]

Add a texture to the cache by cache name

delete(texture_or_name: Texture | str, raise_if_not_exist: bool = False) None[source]

Delete a texture from the cache by cache name.

Parameters:
  • texture_or_name – The texture or cache name to delete

  • raise_if_not_exist – If True, ignore errors when deleting

flush() None[source]

Clear the cache

get(name: str) Texture | None[source]

Get a texture from the cache by cache name

Parameters:

name – The cache name of the texture

Returns:

The texture if found, otherwise None

get_all_textures() set[Texture][source]

Get all textures in the cache

get_texture_by_filepath(file_path: str | Path, crop: tuple[int, int, int, int] = (0, 0, 0, 0)) Texture | None[source]

Get a texture from the cache by file path and crop values.

Parameters:
  • file_path – The path to the file the texture was loaded from

  • crop – The crop values used when creating the texture

get_with_config(hash: str, hit_box_algorithm: HitBoxAlgorithm) Texture | None[source]

Attempts to find a texture with a specific configuration.

Parameters:
  • hash – The image hash

  • hit_box_algorithm – The hit box algorithm to search for

Returns:

The texture if found, otherwise None

put(texture: Texture) None[source]

Add a texture to the cache. It’s important that the crop values and file path are correctly set on the texture before adding it to the cache.

Parameters:

texture – The texture to add

class arcade.cache.image_data.ImageDataCache[source]

Bases:

Simple cache for ImageData objects.

These are usually cached using the absolute path to the file or some custom unique name if generated from code.

We are not caching by image hash because this defeats the purpose of this cache. We want to obtain and image based on a value that can be constructed without knowing the pixel data.

The reasoning for caching the ImageData object instead of the PIL.Image object is to avoid re-calculating the hash in addition to eliminating the need to load it and convert the pixel format.

delete(name: str, raise_if_not_exist: bool = False) None[source]

Attempts to delete an entry from the cache.

Parameters:
  • name – Name of the image

  • raise_if_not_exist – If True, raises KeyError if the entry does not exist

flush()[source]

Clears the cache.

get(name: str) ImageData | None[source]

Attempts to retrieve an entry from the cache.

Parameters:

name – Name of the image

Returns:

ImageData instance or None if not found

put(name: str, image: ImageData)[source]

Add an image to the cache.

An entry can only be cached as either strong or weak, not both. If and existing entry is found, it will be replaced.

Parameters:
  • name – Name of the image

  • image – ImageData object