Grid
Grid#
- class discopy.drawing.grid.Grid(rows)[source]#
Bases:
object
A grid is a list of rows, a row is a list of cells.
- Parameters
rows (list[list[discopy.drawing.grid.Cell]]) – The list of lists of cells inside the grid.
- Return type
None
>>> from discopy.monoidal import * >>> x = Ty('x') >>> cup, cap = Box('cup', x @ x, Ty()), Box('cap', Ty(), x @ x) >>> snake = x @ cap >> cup @ x >>> grid = Grid.from_diagram(snake) >>> print(grid) Grid([Wire(1, x)], [Wire(1, x), Cell(3, 8, cap)], [Wire(1, x), Wire(4, x), Wire(7, x)], [Cell(0, 5, cup), Wire(7, x)], [Wire(7, x)])
- property max: int#
The maximum horizontal coordinate of a grid.
- property min: int#
The minimum horizontal coordinate of a grid.
- to_html()[source]#
Turn a grid into an html table.
Examples
>>> from discopy.monoidal import * >>> x = Ty('x') >>> cup, cap = Box('cup', x @ x, Ty()), Box('cap', Ty(), x @ x) >>> unit = Box('unit', Ty(), x) >>> snake = x @ cap >> cup @ x >>> table = snake.to_grid().to_html()
>>> from lxml.etree import tostring >>> print(tostring(table, pretty_print=True ... ).decode().strip()) <div> <style>.diagram .wire { border-left: 4px solid; ...</style> <table class="diagram"> <tr> <td class="wire">x</td> <td/> <td/> <td/> <td/> <td/> <td/> </tr> <tr> <td colspan="1"/> <td class="wire" colspan="2"/> <td class="box" colspan="5">cap</td> </tr> <tr> <td colspan="1"/> <td class="wire" colspan="3"/> <td class="wire" colspan="3">x</td> <td class="wire" colspan="1">x</td> </tr> <tr> <td class="box" colspan="5">cup</td> <td colspan="2"/> <td class="wire" colspan="1"/> </tr> <tr> <td colspan="7"/> <td class="wire" colspan="1"/> </tr> </table> </div>
>>> spiral = cap >> cap @ x @ x >> x @ x @ x @ unit @ x\ ... >> x @ cap @ x @ x @ x @ x\ ... >> x @ x @ unit[::-1] @ x @ x @ x @ x\ ... >> x @ cup @ x @ x @ x >> x @ cup @ x >> cup >>> spiral.to_grid().to_html().write( ... "docs/_static/drawing/example.html", pretty_print=True)
- Return type
lxml.etree.ElementTree
- to_ascii(_debug=False)[source]#
Turn a grid into an ascii drawing.
Examples
>>> from discopy.monoidal import * >>> x = Ty('x') >>> f = Box('f', x, x @ x) >>> diagram = (f @ f[::-1] >> f @ f[::-1]).foliation() >>> print(diagram.to_grid().to_ascii()) | | | ---f--- -f- | | | -f- --f-- | | | >>> cup, cap = Box('-', x @ x, Ty()), Box('-', Ty(), x @ x) >>> unit = Box('o', Ty(), x) >>> spiral = cap >> cap @ x @ x >> x @ x @ x @ unit @ x\ ... >> x @ cap @ x @ x @ x @ x\ ... >> x @ x @ unit[::-1] @ x @ x @ x @ x\ ... >> x @ cup @ x @ x @ x >> x @ cup @ x >> cup >>> print(spiral.to_grid().to_ascii()) ------- | | ---------- | | | | | | | | | o | | | | | | | ---- | | | | | | | | | | | | | o | | | | | | | | | | | ------- | | | | | | | | ---- | | | -------------------
- Return type
str
- static from_diagram(diagram)[source]#
Layout a diagram on a grid.
The first row is a list of
Wire
cells, then for each layer of the diagram there are two rows: the first for the boxes and the wires in between them, the second is a list ofWire
for the outputs.- Parameters
diagram (discopy.monoidal.Diagram) – The diagram to layout on a grid.
- Return type
>>> from discopy.monoidal import * >>> x = Ty('x') >>> f, s = Box('f', x, x @ x), Box('s', Ty(), Ty()) >>> diagram = ( ... f @ f[::-1] >> x @ s @ x @ x >> f @ f[::-1]).foliation() >>> print(diagram.to_grid()) Grid([Wire(1, x), Wire(15, x), Wire(17, x)], [Cell(0, 12, f), Cell(14, 18, f[::-1])], [Wire(1, x), Wire(11, x), Wire(15, x)], [Cell(0, 4, f), Cell(6, 8, s), Cell(10, 16, f[::-1])], [Wire(1, x), Wire(3, x), Wire(11, x)])