Modules Lab #
In this lab, we will learn how to organize our functions into modules. Modules are files of reusable function. You’ve already encountered modules before!
[0] Setup #
๐ Github Repo: github.com/Making-with-Code/lab_modules
๐ป Go into the
lab_modules
cd lab_modules
git submodule update --init --recursive
poetry shell
[1] What is a module? #
In the previous lab, we used functions to break up a big program into smaller programs. Modules do the same thing, but on a larger scale!
So let’s say you wrote a function in one file. What if you want to re-use a function you wrote in a different file? Copy it over? It feels like there should be a better way.
Today we are going to learn how to import code from one module into another. But first, let’s get some vocabulary straight:
- All python code lives in
files
that end in.py
. They’re just plain text files that you can edit using Atom (or Microsoft Word if you really want. Don’t though. You’ll regret it.) - Python thinks of each file as a
module
. Each module is its own little bubble world. - Python thinks of each directory containing
.py
files as apackage
. A package is a bundle of modules. So yourunit_00
directory is also a package.
[How to import a module] #
When you want to use something from another module, you need to import
it.
We have actually already been doing this. Every one of our programs so far has started with from turtle import *
. But what is this turtle
? It’s a module! And it lives in a file called turtle.py
that some people wrote for you.
There are actually three different ways to get forward
.
First, is the way you are used to doing it. This way imports all of the functions in turtle.py
. It’s quick and easy, but it’s kind of sloppy and it’s not always a very good idea.
from turtle import *
forward(100)
Or, you can import just the forward
function from turtle
:
from turtle import forward
forward(100)
Or, you can import the Turtle
objects. We will learn more about this in Unit 02: Games.
You can import turtle
:
import turtle
turtle.forward(100)
Imagine this scenario:
from bodily_functions import eat
from refrigerator import *
from trash_can import *
eat(chicken_sandwich)
You can see that the eat
function came from bodily_functions
, but where did that chicken_sandwich
come from? The refrigerator
? Or the trash_can
? By importing just what we need from other modules, we can make it clear where everything came from, and we can make sure we don’t import stuff we don’t want. (What else did we import from the trash_can
? Do we want that in our program?)
[2] Tree #
๐ป
Let’s start by going into tree_drawing
folder.
cd tree_drawing
This folder has 3 files:
basic_shapes.py
tree_parts.py
tree.py
atom .
๐Take a look at
basic_shapes.py
and tree_parts.py
. Note how tree_parts.py
uses functions from basic_shapes.py
.
๐ป
Currently, tree.py
is empty. It’s up to you to write function to draw a full tree. Your tree_full()
function should be able to draw a tree of any size.
Be sure to consider:
- what modules will you need to import?
- how can you combine exisiting functions to draw a full tree?
When you run tree
.py it look like:
[3] Drawing Package #
Now we are going to use a package full of fancy drawing functions to turbocharge your turtle.
Packages always include documentation to communicate to users how to use the included software.
This lab will require you to read documentation provided in the README.md
file.
โ CHECKPOINT:
๐ Open the documentation for examples of all the functions available in the
drawing
package. It has the following modules:
shapes.py
movement.py
lines.py
๐ป
cd
out of the /tree
folder.
cd ..
Here you should have:
/lab_modules
/drawing
packagefancy_drawing_example.py
- an example usage of the drawing packagefancy_drawing.py
- an empty file for you to create your own drawing
[Example] #
๐ป Start by running:
fancy_drawing_example.py
python fancy_drawing_example.py
Consider:
- Why does the drawing just appear?
- What modules are used from the drawing package?
- How do the colors repeat?
๐ป Experiment with editing the code. Try make your own version of this pattern! Here is just one of many potential patterns:
๐ Explore more color options HERE
[Fancy Drawing] #
๐ป
In fancy_drawing.py
, it is up to you to create a drawing of your choosing using the modules in the drawing package. It must include uses of at least 3 different modules from the drawing package.
atom fancy_drawing.py
โ CHECKPOINT:
Be prepared to share your drawing!
[4] Deliverables #
โกโจ
Once you’ve successfully completed the sequence be sure to fill out this Google form.
๐ป Push your work to Github:
- git status
- git add fancy_drawing.py
- git status
- git commit -m “describe your drawing and your process here”
be sure to customize this message, do not copy and paste this line
- git push