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

Flattening the Interface - Code

00:00 Welcome to section two, where you’ll learn how to manage the public interface. Now, keeping in mind the issues you had with the interface of the basic example, you’re now going to improve this interface.

00:12 And the first step in achieving that is to add the __init__.py file to the surpluscalculator folder. Now, why do you want to add it to the surpluscalculator folder here at the top of the structure? Well, remember that the __init__.py file makes its containing folder a regular package.

00:34 And that is what you’re trying to achieve. So right-click and add __init__ .py. Hit Enter. And here you go. You’ve created an empty __init__.py file.

00:50 But that in itself is enough to make surpluscalculator a regular package. Congratulations. That is a big first step. Now, it doesn’t quite fix the user interface issues you were facing. So that is what you’ll be working on right now.

01:09 Remember, what you’re trying to achieve is to have all the functions that the user needs show up in the namespace, irrespective of where these functions live in the package structure.

01:22 That is called flattening the API because the user doesn’t need to know the structure of the API. All the functions are brought up to the top level of the public user interface.

01:34 Now, to do that, you need to import these functions into the __init__.py file. Let me show you how to do that for the two income functions. So the income functions, they live in this income.py file. So calculate_random_income() and calculate_fixed_income().

01:53 Now, income lives at the same level as __init__.py. They’re both modules that live in surpluscalculator. So in __init__.py, I’m going to use relative import to import the income module.

02:10 So from .income, the dot representing the current folder, import, and then the two functions that you’d like to appear in the public user interface. So those were calculate_random_income()

02:29 and calculate_fixed_income().

02:33 Now, please do the same thing for outgo. So from .outgo import calculate_random_outgo() and calculate_fixed_outgo().

02:50 And the last thing to do is to import the simulate_surplus() function. That lives in the run_sims module, which itself lives in the simulations subdirectory.

03:01 So from .simulations

03:06 .run_sims, so from there, import simulate_surplus().

03:15 Now, of course, you as the package designer, you still have to know that there’s a subdirectory called simulations and that within there, there’s the run_sims module and that within the run_sims module, there’s this function called simulate_surplus().

03:30 So you as the package designer, you still have to know the structure, of course. You designed it, but the key is that the user will no longer have to know the structure.

03:41 And how that works, you’ll see in the next lesson.

Become a Member to join the conversation.