Package: lifepy
LifePyInstallationInstall via pip:python -m pip install lifepyTutorial on how to use the lifepyGenerate a simulation of sizem*n. In this example, a 10x10 simulation will be generated, using a random array for the initial position of live and dead cells.fromlifepyimportlifepysimulator=lifepy.Simulator(m_size=10,n_size=10,mode='ASCII',live_char='#',dead_char='-')simulator.generate_array()Themodeargument determines how to show the simulation. By default it is set toDEFAULT, 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 isASCII. ForASCIIthe character used for live cells and dead cells can be specified with thelive_charanddead_characcordingly. By default#is used for live cells and-is used for dead cells.You can see the current simulation by using theget_simulationmethod:simulator.get_simulation(printout=True)Output:--#-##-#--
##-#----#-
##-#######
-#-######-
-#--#-#--#
###---##-#
#-####-###
#----#--#-
---###-#-#
-##-##-#--Theget_simulationmethod returns the string used to represet the simulation(like the one shown in the output). Theprintoutparameter prints the simulation when set toTrueand is used for other methods as well. By default,printoutis set toFalsein all the methods.The simulation can advance in two ways.Step by stepThestepmethod 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 simulationThecontinuous_simulationmethod does thestepmethod until aKeyboardInterruptexception occurs, or until all life in the simulation has ended. Sincecontinuous_simulationuses thecursesmodule it does not support ANSI escape sequences. Therefore, while oncontinuous_simulationthe mode isASCII, and after the simulation is interupted or it ends, the mode is set back to its original value.fromlifepyimportlifepysimulator2=lifepy.Simulator(m_size=30,n_size=50,mode='DEFAULT')simulator2.generate_array()simulator2.continuous_simulation(step_deyal=.1,printout=True)Output:Thestep_delaydetermines the time (in seconds) between each step.When taking stepsIt is important to take into account that them*nsize 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 them*nsize are never generated.Loading an arrayInstead 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.fromlifepyimportlifepyimportnumpysimulator3=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 arrayTheget_arraymethod returns a copy of the array being used for the simulation in its current step. For the dimensions of the array, the methodsget_m_size(for rows) andget_n_size(for columns) will return said dimensions.NotesWhen 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, theget_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.