By the end of this lab, you will be able to:
Note : even if you are used to a different Python editor (like WingIDE or Idle), you must complete all parts of this lab, so that you become familiar with PyCharm-specific tools like the debugger, highlighting problems in your code, etc.
doctest and pytest.
Another important outcome is that you will have met several other people who are taking CSC148.
To start off the lab, prepare to meet some interesting people! Navigate between breakout rooms, introduce yourself to classmates and find a group for this lab. You may use breakout rooms 1-16 to work with your group. Make sure one person is screen-sharing.
Each person in a partnership should set up for working on the course by completing the Setting up your personal computer section. Before moving forwards, let each member of your team share and make sure that they have completed the Software guide. We want to make sure every student is set up for this course!
Choose one member’s computer to use for the remainder of the lab. But make sure each member understands every step taken (and that you each complete the “Submitting work on MarkUs individually” on your own MarkUs account)!
lab1 folder.lab1 folder, select “New -> File” to create a new file, and enter the
filename names.txt.Congratulations! If you’ve both reached this point, you and your lab partner should each be set up for CSC148.
Please review the Function Design Recipe handout with your group. This document provides a template for how to approach writing functions that we’ll use through this course.
The Function Design Recipe has us include example function calls in our docstring, and the final step is to
test our code using these example calls. A module called doctest allows us to run these
automatically, to see if our code does what the examples show!
Download this Python module called search.py,
saving it in your lab1 folder. Open this file in PyCharm, but don’t worry about reading
the code right now.
Run the doctests in the module as follows:
search.py.To see what happens when doctests fail, temporarily change one of the doctest examples so that correct code would not pass.
Rerun the doctests and observe how PyCharm reports doctest failures. Then repair the doctest example and rerun the doctests to confirm that all is well.
That was great, but we don’t want to clog our docstrings with too many test cases. A module called
pytest allows us to write further unit tests in a separate module.
test_search.py into
your lab1 folder. Read through this file.5 in the list
[0, 5, 10, 15, 20, 25, 30, 35, 40].test_search.py and selecting “Run ‘pytest in
test_search’”.Now let’s try out the PyCharm debugger to narrow in on the bug.
binary_search. Click near the line number to
create a red circle. This is called a breakpoint, and you can think of it as saying “stop
here”.test_search.py and select “Debug ‘pytest in test_search’”.
If you have time, use the information provided by the debugger to determine what went wrong. If you realize you have stepped past an important event, you can restart the debugger. You may find a memory model drawing helpful.
If you still have time left, try to fix the bug. How do you know you’ve succeeded? Run the tests again! Well, not quite, because you do not have a thorough set of unit tests.
Your final task is to identify and implement a systematic and thorough set of tests, and see if you really did fix the bug. Try to answer the following questions:
If you reached here, great! Here are some suggestions for things to do.
We hope you’ve had some fun and met some students with similar interests. You may later want to form study groups, so you should consider exchanging contact information. Don’t just talk to your partner—talk to all the students you met at the start of the lab.
Take out those worksheets from our first few lectures and make sure you’ve finished them. Since you’re in a computer, now is a great time to check your understanding: type in each code snippet into the Python Console (or into a separate file to run), and see that the output matches what you expected based on your diagrams. Don’t be shy about asking your TAs questions here, too!
For extra practice with programming in Python, we’ve adapted a set of in-class exercises from an older CSC108 instance. You can find the full description in the worksheets below:
Download the starter code random_story.py and follow the instructions contained within.