Locked learning resources

Join us and get access to thousands of tutorials and a community of expert Pythonistas.

Unlock This Lesson

Locked learning resources

This lesson is for members only. Join us and get access to thousands of tutorials and a community of expert Pythonistas.

Unlock This Lesson

Using the Basic Example in the REPL

00:00 So to see how the user interface works and what the code actually does, I invite you to open up the REPL. Just please make sure that you open up the REPL from within the folder that holds the surpluscalculator folder.

00:16 Because from there you can import surpluscalculator.

00:21 The first thing to do is to check the namespace of surpluscalculator. So use dir()

00:30 and the namespace doesn’t actually hold a ton of useful stuff. It has a number of dunder methods, but there are no names of functions or anything that will help you remember what to do.

00:42 So you’ll have to remember that there was an outgo module, there was an income module, and then there was a subfolder called simulations in which there was a run_sims module.

00:55 So you’re going to have to import all these pieces. So firstly, from surpluscalculator import income.

01:05 I’m going to use the up arrow now to repeat the previous line and I’m also going to import outgo. And then use the up arrow again. I’d like to import run_sims, but that was sitting in a subfolder called simulations.

01:22 So I’m going to have to type a dot after surpluscalculator, then simulations, and then import run_sims.

01:32 So these are all the things you need to import. You can now start using it. The first thing to do is to choose an income function. So income_function

01:44 and you have to remember that that sits in the income module that you’ve imported. Also, you’ll have to remember the name of the two options that you had.

01:54 The first one was to calculate a random income. The second one was to calculate a fixed income. I just happen to remember that, so I’m going to use the random one.

02:06 Then I’m going to need parameters to go into this calculate_random_income() function, and that was a list. So I’m going to call that income_inputs, and that’s a list. I’m going to pick from 500 to 2,000.

02:25 And then the same thing for the outgo side. So I need to select an outgo function. I have the same two options, but of course they now live in the outgo module.

02:38 And again, I’m going to use the random one, so calculate_random_outgo().

02:45 And then I’m going to need outgo_inputs.

02:51 That is also a list, and I’m going to choose between 1,000 and 1,500. The last thing to do is then actually do the simulation. So for that you need to go to the run_sims module, which you’ve imported, but then you need to go to the function called simulate_surplus().

03:15 Not simulate_surplus, you see, this is the problem of not having a clear user interface, so simulate_surplus(). And that needs the four input parameters: income_function, income_inputs, outgo_function, and outgo_inputs.

03:38 And when you hit Enter, you’ll get 12 simulated surplus numbers. So these are the income minus the outgo for the 12 future months. Please remember, these numbers will very likely be different on your screen because these are random samples.

03:55 Don’t be upset if you have different numbers. That’s kind of the key of doing random sampling. So that is the user interface. Now, there are a few things I don’t like about the user interface. Firstly, when you used dir() all the way at the start, the output of that, so the namespace, didn’t hold anything useful. So that’s something I’d like to change, because you now had to remember the structure of the code. You had to remember that there was an income module, an outgo module, and then a run_sims module, which was actually sitting in a subdirectory called simulations.

04:35 And even when you remembered all that, you then still had to remember what the actual functions in those modules were. So here you had to use income. and then remember that the name of the function was calculate_random_income(), and the same applies to the outgo and to the running of the simulations.

04:55 So this is where __init__.py will come in. It’ll help you clean up this user interface, and that you’ll cover in the next lesson.

Become a Member to join the conversation.