Loading video player…

Building the Basic Example

00:00 So you’re ready to look at some code. Now, before you do, I’d highly recommend that you download the code. I won’t be typing out the code, that would take a bit too long, so you won’t be able to follow along.

00:12 Because the intention of this course isn’t really to explain how a surplus model works, it’s to explain how the __init__.py module fits into this code and how it changes the user interface. Having said that, of course, I will talk you through the code at a high level. So if you download the code, you’ll find a folder called surpluscalculator.

00:35 And that surpluscalculator folder has two modules in it, income and outgo. It has a subfolder called simulations, which itself has a subfolder called output in which you could store output if you so wanted to. You’d have to adjust the code for that, but there you go, that’s what that folder is for. And then in simulations, there’s the run_sims module.

00:58 The first one to look at is the income module. This one has two functions. The first one on line four, which calculates a random income based on an income list. So line five will show you that it just samples from a uniform distribution between the minimum and the maximum of that list.

01:19 Line eight then calculates a fixed income, and maybe bizarrely, the input parameter there is also a list. And then line nine takes the first element from that list. Now the reason why the input parameter is a list is just to keep the user interface consistent between functions on line four and eight.

01:43 The second module I’d like to cover is the outgo module, and that has two functions in there that you might find suspiciously similar to the ones in the income module, because they are in fact the same. So on line four, you calculate a random outgo based on random sampling from a uniform distribution between the minimum and the maximum of your outgo list. And on line eight, you calculate the fixed outgo, which is just the first element of your outgo list. So that’s quick. The final module to talk about is the run_sims module, and that’s in the simulations folder. So here is simulations.

02:24 If I open up the folder here, you see here is run_sims. So what’s happening here? Well, on line three, there’s the definition of a module level constant, which is NUMBER_OF_SIMS.

02:35 And that is 12 because you’ll want to sample for the 12 future months. On line six then, this is where you actually simulate the surplus. And this function takes four input parameters: income_function, income_input_list, which are the parameters that go with the income function you’ll select in the user interface.

02:57 And then there’s the outgo_function and the outgo_input_list. Again, the outgo_input_list holds the parameters that go with the outgo function that you’ll select in the user interface.

03:09 And line nine then just simulates the income for 12 months using a list comprehension, and lines 13 and 14 do the same thing for the outgo. Then finally, in line 17, you return the surplus, which is income minus outgo.

03:27 So i minus o for i and o from the sim_income_list and sim_outgo_list calculated in lines nine and 13.

03:38 So that’s the code: two income functions to choose from, two outgo functions to choose from, and then running the simulations. And crucially, nowhere in the structure is the __init__.py file. In the next lesson, you’ll use this code to see what the user interface and the output looks like.

Become a Member to join the conversation.