Skip to content

Importing Modules

Pieces of Code

Our code has grown quite large, we should split it into smaller pieces. How about separating it into the following files:

  • constants.py, which will hold all the constant values we use in our program
  • simulation.py, which contains all the functions used for simulating our growth model
  • evaluation.py, which holds functions for all the statistical evaluation
  • __main__.py, which ties it all together and runs the actual simulation

The Contants File

We begin by moving out the constants, since this is probably the easiest task.

# constants.py
INDIVIDUALS_PER_GROUP = 10
FOOD_PER_INDIVIDUAL = 0.25
START_DAY = 1

Of course the rest of our program needs to know about these constants. What we can do to make the code from one Python file in another one is importing it.

So, in our original growth_model.py we add the line

from constants import INDIVIDUALS_PER_GROUP, FOOD_PER_INDIVIDUAL, START_DAY

In this case constants (without the .py ending) is the name of the file from which to import something. The part after the import specifies what to import. It is recommended to only import what you actually need as opposed to just importing everything.

The Simulation File

Next, lets move the functions simulate_day(…) and input_positive_integer(…) into the file simulation.py. One could argue that input_positive_integer(…) is not actually part of the simulation and deserves its own file, so each file only deals with one topic.

What do you think?

We also have to pay attention that we now no longer need the FOOD_PER_INDIVIDUAL and INDIVIDUALS_PER_GROUP in the growth_model.py but in the simulation.py.

# simulation.py
from constants import INDIVIDUALS_PER_GROUP, FOOD_PER_INDIVIDUAL

def input_positive_integer(ask_for_what):
    

def simulate_day(initial_population, initial_food):
    
# growth_model.py
from constants import START_DAY
from simulation import input_positive_integer, simulate_day

# Simulation starting parameters
starting_population = input_positive_integer("starting population")
food_per_day = input_positive_integer("amount of food per day")
simulation_duration = input_positive_integer("simulation duration (in days)")


The Evaluation File

All the statistical evaluation can be put into a separate file as well. We should gather all these steps into one function, while we are at it.

# evaluation.py
def statistical_evaluation(population_data):
    gathered_values = len(population_data)
    lowest_population = min(population_data)
    highest_population = max(population_data)
    average_population = sum(population_data) / gathered_values

    print("We gathered", gathered_values, "data points")
    print("Minimum:", lowest_population, "individuals")
    print("Maximum:", highest_population, "individuals")
    print("Average:", average_population, "individuals")

    # This function just does its thing and does not produce a value.
    # Thus we can leave out the return here.

from evaluation import statistical_evaluation



# Calculate some statistical values
statistical_evaluation(population_over_time)

The Main File

If we have a program that is composed of more than one file, it is customary to name the one file that launches everything __main__.py so it can be spotted easily. In this case we could rename our growth_model.py and that is all.

Sometimes we have programs that do everything in one file. Naming them all __main__.py would be a bit inconvenient, so there is an alternative for that case.

# Do all the importing, setup and definitions beforehand

if __name__ == "__main__":
    # Do the main work
    # This only will be run if the file is executed directly by Python, not if it is imported

Key Points

  • Splitting programs into dedicated files can be a good idea
  • To access variables or functions defined in other files, they can be imported
  • It is easier to tie a program together then to split it up.
Code Checkpoint

This is the code that we have so far: