Abstract Base Classes

Abstract base classes for user-defined coordinates.

The base classes in this module form an interface for using the functions in pygrates.moves. The user creates a coordinate class by explicitly subclassing one of the bases and implementing its abstract methods, or implicitly by implementing all of its methods. All coordinates need to be hashable and to define equality with coordinates of the same type: this can easily achieved with a named tuple or dataclass implementation.

Examples

>>> from dataclasses import dataclass
>>> @dataclass(frozen=True)
... class Point2D(pg.abc.DGCoords):
...   x: int
...   y: int
...
...   def children(self):
...     return Point2D(self.x + 1, self.y), Point2D(self.x, self.y + 1)
...
...   def parents(self):
...     return Point2D(self.x - 1, self.y), Point2D(self.x, self.y - 1)
...
>>> origin = Point2D(0, 0)
>>> isinstance(origin, pg.abc.DGCoords)
True
class pygrates.abc.GCoords[source]

Bases: ABC

Base class for graph coordinates.

Interface for using the functions in pygrates.moves. User-created coordinate class implicitly defines an undirected or directed graph.

Methods to implement are __eq__, __hash__ and adjacent.

abstract __eq__(other: object) bool[source]

Return True if self is equal to other coordinate.

Parameters:

other (Self) – Coordinate object of the same type as self.

Returns:

True if passed coordinate is equal to self.

Return type:

bool

abstract __hash__() int[source]

Return hash value of self.

Returns:

Hash value of self.

Return type:

int

abstract adjacent() Iterable[Self][source]

Return iterable containing adjacent coordinates.

Returns:

Iterable containing coordinates adjacent to self, of the same type.

Return type:

Iterable[Self]

is_adjacent(other: Self) bool[source]

Return True if self is adjacent to other coordinate.

Parameters:

other (Self) – Coordinate object of the same type as self.

Returns:

True if self is adjacent to passed coordinate.

Return type:

bool

class pygrates.abc.DGCoords[source]

Bases: GCoords, ABC

Base class for directed graph coordinates.

Interface for using the functions in pygrates.moves. User-created coordinate class implicitly defines a directed graph.

Methods to implement are __eq__, __hash__, children, and parents.

abstract __eq__(other: object) bool

Return True if self is equal to other coordinate.

Parameters:

other (Self) – Coordinate object of the same type as self.

Returns:

True if passed coordinate is equal to self.

Return type:

bool

abstract __hash__() int

Return hash value of self.

Returns:

Hash value of self.

Return type:

int

adjacent() Iterable[Self][source]

Return iterable containing adjacent coordinates.

Returns:

Iterable containing coordinates adjacent to self, of the same type.

Return type:

Iterable[Self]

abstract children() Iterable[Self][source]

Return iterable containing child coordinates.

Returns:

Iterable containing child coordinates of self, of the same type.

Return type:

Iterable[Self]

is_adjacent(other: Self) bool

Return True if self is adjacent to other coordinate.

Parameters:

other (Self) – Coordinate object of the same type as self.

Returns:

True if self is adjacent to passed coordinate.

Return type:

bool

is_child(other: Self) bool[source]

Return True if self is a child of other coordinate.

Parameters:

other (Self) – Coordinate object of the same type as self.

Returns:

True if self is a child of passed coordinate.

Return type:

bool

is_parent(other: Self) bool[source]

Return True if self is a parent of other coordinate.

Parameters:

other (Self) – Coordinate object of the same type as self.

Returns:

True if self is a parent of passed coordinate.

Return type:

bool

abstract parents() Iterable[Self][source]

Return iterable containing parent coordinates.

Returns:

Iterable containing parent coordinates of self, of the same type.

Return type:

Iterable[Self]