Lab 11 - Making a Module and Copying a List
Directions for Labs
- Submit your completed lab file online to CourSys (not Canvas). Labs are marked on completion, not correctness, so complete each part to the best of your ability and learn!
- It is recommended that students attend in-person lab sections for lots of help!
- If you would like to attend an in-person lab section, you are welcome to come to any (or more than one) lab section.
- There is no need to attend the section you are enrolled in.
- There is no need to attend any lab sections: there is no attendance taken.
- You can complete the lab on your own time or own computer if you like.
- While completing these labs, you are encouraged to help your classmates and receive as much help as you like. Assignments, however, are individual work and you must not work with another person on assignments.
Testing a Function
- Inside your
cmpt120
folder, create a new folder forlab11
and make alab11.py
file. -
Implement the following
alternate_zeros(...)
function:def alternate_zeros(data): """ Return a _copy_ of the list `data`, but every other value should be 0 Example: When passed in [10, 20, 30, 40, 50], it should return a new list of [10, 0, 30, 0, 50] data: The input list of numbers Returns: New list with every other element 0 """
-
Hint - Copy
You need to copy the list into a new list because the argument is an alias to the original, and the original must not be changed. Look up in the notes how to make a copy of a list. -
Hint - Loop Index
Create a loop through the indexes of the numbers in the list. You want to set every other one (the odd ones) to zero.
-
- Test your function with at least one call where you pass in a list and print the result.
- Test your function using asserts:
- Rather than relying on reading the output to ensure your code works, we use
assert
statements. - An
assert
checks if some condition is true. If it is, the program can continue. However, if the condition is false, it crashes the program and prints an error message. They are very helpful for debugging!- For example, you might have:
assert alternate_zeros([10, 20, 30, 40, 50]) == [10, 0, 30, 0, 50]
- For example, you might have:
- Try adding an assert that is incorrect and see what it does. For example:
assert alternate_zeros([10, 20, 30, 40, 50]) == [1] # ASSERT FAILS
- Create a new function
test_alternate_zero()
which calls youralternate_zeros(...)
with at least five different arguments, and asserts that the answer is what you expect. - Test for different edge cases; try breaking your code!
-
Hint - Alias Test
Add a test to ensure that the function is correctly copying the list argument vs changing the list argument itself. You'll need to first create a list in your calling code, then callalternate_zeros(...)
on it, and then finally assert that the original list is still the same.
- Rather than relying on reading the output to ensure your code works, we use
Make a Module
- Inside your
cmpt120/lab11/
folder, create a new file namedmy_module.py
. - Move your code for creating the
alternate_zeros(...)
function into your newly createdmy_module.py
file. - Modify your
lab11.py
code to work with the new module.-
Hint - Import
You need to import your new module into the lab11.py file:import my_module
-
Hint - Call Module
You'll need to change all of the code in lab11.py to call the function in the module:my_module.alternate_zeros(...)
-
- Run your tests to prove everything works.
- You will need to run your
lab11.py
file, not the module. - Note how fast it was to run the tests to ensure you didn't break anything! This is a powerful part of using good tests: they allow you to change your code quickly without fear of having unknowingly broken anything.
- You will need to run your
Experiment with a Module
- Use the integrated debugger in VS Code to step through your code.
- Set a breakpoint on one of your
assert
statements. - Start debugging your
lab11.py
file. - When the debugger breaks on your breakpoint, step into the
alternate_zeros(...)
function and step through it. - Remove the breakpoint in
lab11.py
- Set a breakpoint on one of your
- Try a breakpoint in your module:
- Set a breakpoint in your
test_alternate_zero()
function. - Start debugging your
lab11.py
file. - Note if it hits your breakpoint in the other file.
- Using the debugger is an effective way of debugging larger programs. You should plan to use it when working on the project.
- Set a breakpoint in your
- Try running the
my_module.py
file. Does it do anything?- Then add a
print("Hello world, from the module!")
statement in the module outside of any function. Run the module. Understand if it prints anything.
- Then add a
- With that
print(...)
statement still outside of all functions in the module, re-runlab11.sol
.- What happens? (Just think about it, nothing to write yet).
- Try setting a break-point on the print statement, and see what triggers executing the print statement.
-
Hint - Call Stack
When you have hit your print statement's breakpoint, on the left-hand pane expand the Call Stack. Click on each of the rows listed there to see what code called your print statement.
-
- Generally, our modules should have all of our code inside functions.
- Remove the
print(...)
statement from the main area of your module so it no longer prints the undesired message when loading. - Note: For the project, ensure that your module does not have any extra statements in its main area. TAs will be expecting to test your module using the test program, not running any of your test code still in the module's main area.
- Remove the
Understanding Questions
Answer these understanding questions in your lab11.py
file:
- Given a alias to a list, how can we copy the list?
- What happens if you set a breakpoint in your module's function, and then debug the other file which calls that function. Does the breakpoint still work?
- What happens when you try to run your module file when all statements in the module are inside functions?
- When you have a statement in your module that is outside of all functions (in the main area), what happens when another Python file imports that file?
- What does an
assert
do?
Submission
Submit your lab11.py
and my_module.py
files to CourSys by Sunday midnight. Remember you can submit up to 3 days late with no penalty if needed (but please plan to get it done on time to stay up to date on the course!)
Topics Covered
- Completing functions
- Implement the body of a function that was given only as a prototype.
- Work with making a copy of a list.
- Write function to do testing using
assert
- Make a module
- Create files, put functions in correct spot.
- Debugging with a module; running code in other file.
- Investigate content in 'main' of module.