LifePy
Installation
Install via pip:
python -m pip install lifepy
Tutorial on how to use the lifepy
Generate a simulation of size m*n. In this example, a 10x10 simulation will be generated, using a random array for the initial position of live and dead cells.
from lifepy import lifepy
simulator = lifepy.Simulator(m_size=10, n_size=10, mode='ASCII', live_char='#', dead_char='-')
simulator.generate_array()
The mode argument determines how to show the simulation. By default it is set to DEFAULT, where live cells are shown as white blocks and dead cells are black blocks, for this mode, ANSI escape sequences are used. The other available mode is ASCII. For ASCII the character used for live cells and dead cells can be specified with the live_char and dead_char accordingly. By default # is used for live cells and - is used for dead cells.
You can see the current simulation by using the get_simulation method:
simulator.get_simulation(printout=True)
Output:
--#-##-#--
##-#----#-
##-#######
-#-######-
-#--#-#--#
###---##-#
#-####-###
#----#--#-
---###-#-#
-##-##-#--
The get_simulation method returns the string used to represet the simulation(like the one shown in the output). The printout parameter prints the simulation when set to True and is used for other methods as well. By default, printout is set to False in all the methods.
The simulation can advance in two ways.
Step by step
The step method only moves the simulation foward once. In other words, the simulation does one step.
Using the same simulation as before:
simulation.step(printout=True)
Output:
-####-----
#--------#
---------#
-#--------
----#----#
#--------#
#-####---#
-##-------
-###---#--
--#--#--#-
Continuous simulation
The continuous_simulation method does the step method until a KeyboardInterrupt exception occurs, or until all life in the simulation has ended. Since continuous_simulation uses the curses module it does not support ANSI escape sequences. Therefore, while on continuous_simulation the mode is ASCII, and after the simulation is interupted or it ends, the mode is set back to its original value.
from lifepy import lifepy
simulator2 = lifepy.Simulator(m_size=30, n_size=50, mode='DEFAULT')
simulator2.generate_array()
simulator2.continuous_simulation(step_deyal=.1,printout=True)
Output:
The step_delay determines the time (in seconds) between each step.
When taking steps
It is important to take into account that the m*n size specified at the start is the size of the array, but also the size of the simulation. Therefore, any live cells that 'should' generate but are outside the m*n size are never generated.
Loading an array
Instead of generating a random array for the simulation, a predetermined one can be loaded. However the size of the array must equal the size of the simulation.
from lifepy import lifepy
import numpy
simulator3 = lifepy.Simulator(m_size=10, n_size=10, mode='ASCII')
pre_array = numpy.array([[1,1,1,1,1,1,1,1,1,1],
[0,0,0,0,0,0,0,0,0,0],
[1,1,1,1,1,1,1,1,1,1],
[0,0,0,0,0,0,0,0,0,0],
[1,1,1,1,1,1,1,1,1,1],
[0,0,0,0,0,0,0,0,0,0],
[1,1,1,1,1,1,1,1,1,1],
[0,0,0,0,0,0,0,0,0,0],
[1,1,1,1,1,1,1,1,1,1],
[0,0,0,0,0,0,0,0,0,0]])
simulator3.load_array(pre_array)
simulator3.get_simulation(printout=True)
Output:
##########
----------
##########
----------
##########
----------
##########
----------
##########
----------
Get values from the array
The get_array method returns a copy of the array being used for the simulation in its current step. For the dimensions of the array, the methods get_m_size (for rows) and
get_n_size (for columns) will return said dimensions.
Notes
-
When an array is being used for the simulation, it will have a 'border' of zeros. This was done for preventing out of index errors when checking for live cells. Example:
Original array: [[1,1],[1,1]] -> Array simulated: [[0,0,0],[0,1,1,0],[0,1,1,0],[0,0,0]]
However, the
get_arraymethod will NOT return the simulated array, it will return a properly 'cropped' array. -
It is recommended to use m and n values that fit within the screen when printing the simulation. Otherwise, it most likely will appear in an odd way.
Metadata
Release files for lifepy 1.0.6
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| lifepy-1.0.6.tar.gz | 5.6 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| lifepy-1.0.6-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 11.3 kB
Release files / lifepy-1.0.6.tar.gz
| Download URL | lifepy-1.0.6.tar.gz |
|---|---|
| Size | 5.6 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
3cf5dde083ae5386753d7306f78b25271b92ae7c1cda99ef78dfca3eef6358a3
|
|
BLAKE2b-256 checksum How to use checksums |
e519ff6f061af526dfadb53359a750c2557743868fbc2a79aaab6a1729da33a9
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/3.4.2 importlib_metadata/4.8.1 pkginfo/1.7.1 requests/2.22.0 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.8.10
|
Release files / lifepy-1.0.6-py3-none-any.whl
| Download URL | lifepy-1.0.6-py3-none-any.whl |
|---|---|
| Size | 5.7 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
80ca8c5aa574dda01d5f178444d1bdbfdb5de6e661e84dbb3e06105e97157d3c
|
|
BLAKE2b-256 checksum How to use checksums |
2f2b8c2f8453ff86ef4264854d6d05bb0bda1cae7c2dc9431c9391d5a2a874b6
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/3.4.2 importlib_metadata/4.8.1 pkginfo/1.7.1 requests/2.22.0 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.8.10
|