API
Package conway
Top-level package for conway
- class conway.FiniteGrid(N=10, boundary='zero', dump=False, load=False, filename='conway')[source]
Naive NxN grid with different boundary conditions. Cells are randomly assigned state (dead or alive).
- Parameters
N (int) – number of cells in the X and Y direction.
boundary (str) – boundary condition:
zero,reflecting, orperiodic.dump (bool) – pickle the generated FiniteGrid object. (Practical if you discover a nice pattern and you want to view it again.
load (bool) – if True, load a file with a pickled FiniteGrid object, e.g. to view its evolution again.
filename (str) – name of the file to be dumped or loaded.
In order to not have to implement special boundary rules, we make the grid (N+2)x(N+2) where the outer layers are ghost cells which are just there to make sure that index i-1, i+1, j-1 and j+1 exist for all cells (i,j):
. | . . . . | . --+---------+-- . | 0 0 1 0 | . . | 0 1 0 0 | . . | 0 0 1 0 | . . | 0 1 0 1 | . --+---------+-- . | . . . . | .
The state of the ghost cells is determined by the boundary conditions:
all zeros:
0 | 0 0 0 0 | 0 --+---------+-- 0 | 0 0 1 0 | 0 0 | 0 1 0 0 | 0 0 | 0 0 1 0 | 0 0 | 0 1 0 1 | 0 --+---------+-- 0 | 0 0 0 0 | 0
reflective boundary condition: the ghost cell has the same state as the cell on the inside of the boundary:
0 | 0 0 1 0 | 0 --+---------+-- 0 | 0 0 1 0 | 0 0 | 0 1 0 0 | 0 0 | 0 0 1 0 | 0 0 | 0 1 0 1 | 1 --+---------+-- 0 | 0 1 0 1 | 1
Periodic boundary condition: a ghost cell is a copy of the state at the other end, as if the finite grid is repeated in the x and y direction:
1 | 0 1 0 1 | 0 --+---------+-- 0 | 0 0 1 0 | 0 0 | 0 1 0 0 | 0 0 | 0 0 1 0 | 0 1 | 0 1 0 1 | 0 --+---------+-- 0 | 0 0 1 0 | 0
- apply_0bc()[source]
Apply the zero boundary condition, i.e. surround this FiniteGrid object by zeros.
- apply_bc(bc=None)[source]
Apply a boundary condition to this FiniteGrid object.
- Parameters
bc (str) – a string identifying the type of boundary condition we want to apply.
- apply_pbc()[source]
Apply the periodic boundary condition.
This can be viewed as:
the square being folded such that opposite ends along each axis are glued together. That first gives a tube and then a torus.
the square being part of an infinite repetition along each axis yielding a periodic pattern.
- apply_rbc()[source]
Apply the reflecting boundary condition, i.e. the surroundig elements have the same value as the value on the inside of the boundary.
- curse(stdscr, boundary=True, symbols=None)[source]
‘Print’ the FiniteGrid object to the terminal using the Python curses module.
This gives a nicer visualization because the successive generations are overwriting each other, thus being more close to an animation. The current implementation is limited to the size of the terminal.
- Parameters
stdscr – the curses wrapper object for the terminal.
boundary (bool) – if True, also prints the surrounding boundary layers.
symbols (list-like) – use
symbols[0]for denoting dead cells, andsymbols[1]for denoting living cells in the output. The defaults are `` `` andX.
- dump(filename='conway')[source]
Pickle this FiniteGrid object (save to file).
- Parameters
filename (str) – name of the pickle file to contain the pickled FiniteGrid object>
- evolve(n_generations=1, draw=True, stop_if_static=False, curse=None, interval=0.1)[source]
Let the system evolve over
generationsgenerations.- Parameters
n_generations (int) – the number of generation to evolve.
draw (bool) – if True, draw each generation on the terminal.
stop_if_static (bool) – if True stops evolving the system if it is a static state.
interval (float) – seconds to wait between drawing two successive generations.
- static load(filename='conway')[source]
Unpickle a pickled FiniteGrid object.
- Parameters
filename (str) – name of the pickle file containing the pickled FiniteGrid object.
- Returns
a FiniteGrid object
- Raises
RuntimeError if unpickling the file does not yield a FiniteGrid object.
- print(boundary=True, symbols=None)[source]
Print this FiniteGrid object to the terminal.
- Parameters
boundary (bool) – if True, also prints the surrounding boundary layers.
symbols (list-like) – use
symbols[0]for denoting dead cells, andsymbols[1]for denoting living cells in the output. The defaults are `` `` andX.