The tmxlib.helpers module

Exceptions

exception tmxlib.helpers.UsedTilesetError[source]

Raised when trying to remove a tileset from a map that is uses its tiles

exception tmxlib.helpers.TilesetNotInMapError[source]

Used when trying to use a tile from a tileset that’s not in the map

NamedElementList

class tmxlib.helpers.NamedElementList(lst=None)[source]

A list that supports indexing by element name, as a convenience, etc

lst[some_name] means the first element where element.name == some_name. The dict-like get method is provided.

Additionally, NamedElementList subclasses can use several hooks to control how their elements are stored or what is allowed as elements.

get(index_or_name, default=None)[source]

Same as __getitem__, but a returns default if not found

insert(index_or_name, value)[source]

Same as list.insert, except indices may be names instead of ints.

insert_after(index_or_name, value)[source]

Insert the new value after the position specified by index_or_name

For numerical indexes, the same as insert(index + 1, value). Useful when indexing by strings.

move(index_or_name, amount)[source]

Move an item by the specified number of indexes

amount can be negative. For example, “move layer down” translates to layers.move(idx, -1)

The method will clamp out-of range amounts, so, for eample, lst.move(0, -1) will do nothing.

Hooks for subclasses:

modification_context(*args, **kwds)[source]

Context in which all modifications take place.

The default implementation nullifies the modifications if an exception is raised.

Note that the manager may nest, in which case the outermost one should be treated as an atomic operation.

retrieved_value(item)[source]

Called when an item is being retrieved from the list.

Return the object that will actually be retrieved.

This method must undo any modifications that stored_value does.

stored_value(item)[source]

Called when an item is being inserted into the list.

Return the object that will actually be stored.

To prevent incompatible items, subclasses may raise an exception here.

This method must undo any modifications that retrieved_value does.

Internal helpers and mixins

Dict conversion helpers

tmxlib.helpers.from_dict_method(func)[source]

Decorator for from_dict classmethods

Takes a copy of the second argument (dct), and makes sure it is empty at the end.

tmxlib.helpers.assert_item(dct, key, expected_value)[source]

Asserts that dct[key] == expected_value

Helpers for tuple properties

tmxlib.helpers.unpacked_properties(full_prop_name, count=2)[source]

Return properties that “unpack” a tuple property

For example, if a class defines:

x, y = unpacked_properties('pos')

then obj.x will return the same as obj.pos[0], and setting obj.x will update obj.pos to (new x, old y).

class tmxlib.helpers.SizeMixin[source]
width

Getter/setter for self.size[0]

height

Getter/setter for self.size[1]

_wrap_coords(x, y)[source]

Normalize coordinates for indexing/slicing

Positive values are unchanged, negative ones wrap around using self.width & self.height

Other mixins

class tmxlib.helpers.LayerElementMixin[source]

Provides a map attribute extracted from the object’s layer.

class tmxlib.helpers.TileMixin[source]

Provides size based on pixel_size and the map

width

Getter/setter for self.size[0]

height

Getter/setter for self.size[1]

tile_width

Getter/setter for self.tile_size[0]

tile_height

Getter/setter for self.tile_size[1]

pixel_x

Getter/setter for self.pixel_pos[0]

pixel_y

Getter/setter for self.pixel_pos[1]

x

Getter/setter for self.pos[0]

y

Getter/setter for self.pos[1]

Helpers

class tmxlib.helpers.Property[source]

Trivial subclass of the property builtin. Allows custom attributes.