Reading and writing — tock documentation

# Reading and writing

## Module tock.tables

class tock.tables.Table(rows, num_header_cols=1, num_header_rows=1)

  A simple class that just stores a list of lists of strings.

  Parameters:

    - rows – list of lists of strings

    - num_header_rows (int) – number of header rows

    - num_header_cols (int) – number of header columns

  num_header_cols

    Number of header rows

  num_header_rows

    Number of header columns

  rows

    The table contents

tock.tables.from_table(table)

  Convert a Table to a Machine.

  Example

  | 0 |  | 1 |  | & |
  --- | --- | --- | --- | --- | --- | ---
  | 0 | & | 1 | & | $ | &
  >@q1 |  |  |  |  |  | q2,$
  q2 |  | q2,0 |  | q2,1 |  | q3,&
  q3 | q3,& |  | q3,& |  | q4,& |
  @q4 |  |  |  |  |  |

  The first two rows, because they have empty first cells, are assumed to be header rows.

  In a header row, cells “fill” to the right. In the above example, the first row effectively has cells 0, 0, 1, 1, &, &.

tock.tables.read_csv(filename)

  Reads a CSV file containing a tabular description of a transition function (see from_table).

tock.tables.read_excel(filename, sheet=None)

  Reads an Excel file containing a tabular description of a transition function (see from_table).

tock.tables.to_table(m)

  Converts a Machine to a Table.

tock.tables.write_csv(m, filename)

  Writes Machine m to file named by filename.

## Module tock.graphs

class tock.graphs.Graph(attrs=None)

  A directed graph. Both nodes and edges can have a dict of attributes.

  Nodes can be any object that implements __hash__ and __eq__.

  If g is a Graph and v is a node, v’s attributes can be accessed as g.nodes[v]. If u and v are nodes, edge (u, v)’s attributes can be accessed as g.edges[u][v].

  add_edge(u, v, attrs=None)

    Add edge from u to v to graph with attributes attrs.

  add_node(v, attrs=None)

    Add node v to graph with attributes attrs.

  has_edge(u, v)

    Remove edge from u to v.

  has_path()

    Returns True iff there is a path from the start node to an accept node.

  only_path()

    Finds the only path from the start node. If there is more than one, raises ValueError.

  remove_node(v)

    Remove node v, as well as any edges incident to v.

  shortest_path()

    Finds the shortest path from the start node to an accept node. If there is more than one, chooses one arbitrarily.

tock.graphs.from_graph(g)

  Converts a Graph to a Machine.

tock.graphs.read_tgf(filename)

  Reads a file in Trivial Graph Format. Edge labels are read into the label attribute.

tock.graphs.to_graph(m)

  Converts a Machine to a Graph.

tock.graphs.write_dot(x, filename)

  Writes a Machine or Graph to file named filename in GraphViz (DOT) format.