4.1.1. Protocols

In Python a protocol is a specification for structural subtyping (static duck-typing), e.g., see https://www.python.org/dev/peps/pep-0544/. This is very similar to what is called a “concept” in C++, e.g., see https://en.wikipedia.org/wiki/Concept_(generic_programming). Python for example has the Iterator protocol that specifies how to implement objects one can iterate over.

This page describes protocols in MØD that are implemented by several classes in the Python interface. Note that these classes are not real types, but merely serves as a documentation device.

class mod.protocols.Graph

The undirected graph protocol.

In addition to the following elements, a graph is also equality comparable.

numVertices

(Read-only) The number of vertices in the graph.

Type:

int

vertices

(Read-only) An iterable of all vertices in the graph.

Type:

VertexRange

numEdges

(Read-only) The number of edges in the graph.

Type:

int

edges

(Read-only) An iterable of all edges in the graph.

Type:

EdgeRange

class Vertex

A descriptor of either a vertex in a graph, or a null vertex.

Besides the operations below, descriptors are

  • equality comparable,

  • totally ordered, and

  • hashable

__init__()

Constructs a null descriptor.

isNull()
Returns:

whether this is a null descriptor or not.

Return type:

bool

id

(Read-only) The index of the vertex. A non-negative number. The IDs are not guaranteed in general to be consequtive. See the documentation for concrete graphs for sepcific additional guarantees.

Type:

int

Raises:

LogicError if it is a null descriptor.

graph

(Read-only) The graph the vertex belongs to.

Type:

Graph

Raises:

LogicError if it is a null descriptor.

degree

(Read-only) The degree of the vertex.

Type:

int

Raises:

LogicError if it is a null descriptor.

incidentEdges

(Read-only) A range of incident edges to this vertex.

Type:

Graph.IncidentEdgeRange

Raises:

LogicError if it is a null descriptor.

class Edge

A descriptor of either an edge in a graph, or a null edge.

Besides the operations below, descriptors are

  • equality comparable, and

  • totally ordered

__init__()

Constructs a null descriptor.

__bool__()
Returns:

not isNull()

Return type:

bool

isNull()
Returns:

whether this is a null descriptor or not.

Return type:

bool

graph

(Read-only) The graph the edge belongs to.

Type:

Graph

Raises:

LogicError if it is a null descriptor.

source

(Read-only) The source vertex of the edge.

Type:

Graph.Vertex

Raises:

LogicError if it is a null descriptor.

target

(Read-only) The target vertex of the edge.

Type:

Graph.Vertex

Raises:

LogicError if it is a null descriptor.

class VertexRange

An iterable of all vertices in the graph.

class EdgeRange

An iterable of all edges in the graph.

class IncidentEdgeRange

An iterable of all edges incident to a specific vertex in the graph.

class mod.protocols.LabelledGraph

An extension of the Graph protocol, with access to label information on vertices and edges.

class Vertex

An extension of the Graph.Vertex protocol.

stringLabel

(Read-only) The string label of the vertex.

Type:

str

Raises:

LogicError if it is a null descriptor.

atomId

(Read-only) The atom ID of the vertex.

Type:

AtomId

Raises:

LogicError if it is a null descriptor.

isotope

(Read-only) The isotope of the vertex.

Type:

Isotope

Raises:

LogicError if it is a null descriptor.

charge

(Read-only) The charge of the vertex.

Type:

Charge

Raises:

LogicError if it is a null descriptor.

printStereo()
printStereo(p)

Print the stereo configuration for the vertex.

Parameters:

p (GraphPrinter) – the printing options used for the depiction.

Returns:

the name of the PDF-file that will be compiled in post-processing.

Return type:

str

Raises:

LogicError if it is a null descriptor.

class Edge

An extension of the Graph.Edge protocol.

stringLabel

(Read-only) The string label of the edge.

Type:

str

Raises:

LogicError if it is a null descriptor.

bondType

(Read-only) The bond type of the edge.

Type:

BondType

Raises:

LogicError if it is a null descriptor.

class mod.protocols.VertexMap

The protocol implemented by classes that represent a map of vertices from a domain Graph to a codomain Graph.

domain

(Read-only) The graph for which the vertices form the domain of the map.

Type:

Graph

codomain

(Read-only) The graph for which the vertices form the codomain of the map.

Type:

Graph

__getitem__(v)
Parameters:

v (Graph.Vertex) – the vertex to return the image of.

Returns:

the image of the given domain vertex. May return a null vertex if the map is partial.

Return type:

Graph.Vertex

Raises:

LogicError if v is a null vertex.

Raises:

LogicError if v does not belong to the domain graph for the map.

inverse(v)
Parameters:

v (Graph.Vertex) – the codomain vertex to return the domain vertex for.

Returns:

the domain vertex that maps to the given codomain vertex. May return a null vertex if non exist.

Return type:

Graph.Vertex

Raises:

LogicError if v is a null vertex.

Raises:

LogicError if v does not belong to the codomain graph for the map.