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

Mixin classes for tuple properties

tmxlib.helpers.tuple_mixin(name, full_property_name, subprop_names, doc=None)[source]

Create a class that provides “unpacked” attributes for a tuple attr.

Example:
tuple_mixin('PosMixin', 'pos', ['x', 'y']) has two settable properties x and y, such that self.pos == (self.x, self.y). The original property, pos in this case, must be provided by subclasses.
class tmxlib.helpers.PosMixin

Provides x, y properties.

Subclasses will need a pos property, a 2-tuple of values.

Note: setting one of the provided properties will set pos to a new tuple.

class tmxlib.helpers.SizeMixin[source]
class tmxlib.helpers.PixelPosMixin

Provides pixel_x, pixel_y properties.

Subclasses will need a pixel_pos property, a 2-tuple of values.

Note: setting one of the provided properties will set pixel_pos to a new tuple.

class tmxlib.helpers.PixelSizeMixin

Provides pixel_width, pixel_height properties.

Subclasses will need a pixel_size property, a 2-tuple of values.

Note: setting one of the provided properties will set pixel_size to a new tuple.

class tmxlib.helpers.TileSizeMixin

Provides tile_width, tile_height properties.

Subclasses will need a tile_size property, a 2-tuple of values.

Note: setting one of the provided properties will set tile_size to a new tuple.

Other mixins

class tmxlib.helpers.LayerElementMixin[source]

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

class tmxlib.helpers.TileMixin[source]

Bases: tmxlib.helpers.SizeMixin, tmxlib.helpers.PixelSizeMixin, tmxlib.helpers.PixelPosMixin, tmxlib.helpers.PosMixin, tmxlib.helpers.LayerElementMixin

Provides size based on pixel_size and the map

See the superclasses.

Helpers

class tmxlib.helpers.Property[source]

Trivial subclass of the property builtin. Allows custom attributes.