Skip to content

Commit

Permalink
predictive stepping: pdb. doc & test: lesson prep
Browse files Browse the repository at this point in the history
  • Loading branch information
colevandersWands committed Dec 2, 2024
1 parent 6b018d9 commit fb8aedc
Show file tree
Hide file tree
Showing 10 changed files with 339 additions and 111 deletions.
25 changes: 18 additions & 7 deletions 2_predictive_stepping/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -149,18 +149,29 @@ your study time and to measure your progress:

### Other Visualization Tools

Other options besides the debugger in VSCode:

- 🥚 You can use a trace table to manually execute a program with pencil and
paper.
- 🥚 You can use the built-in Python `trace` module from CLI to help understand
a script's execution:
- `$ python -m trace -t path/to/file.py`: prints each line as it's executed to
the console.
- `$ python -m trace -c path/to/file.py`: generates a new `.cover` file
indicating how many times each line of code was executed.
- 🥚 You can use [Python Tutor](http://pythontutor.com/) to step through,
visualize and predict program execution.
- Python Tutor has a more approachable visualization than the VSCode Debugger,
but can not handle large programs.
- This does not replace VSCode as a code editor.
- (optional) You can use [Thonny](https://thonny.org/) to write, step through,
- 🐣 You can use the built-in Python `trace` module from CLI to help understand
a script's execution:
- `$ python -m trace -t path/to/file.py`: prints each line as it's executed to
the console.
- `$ python -m trace -c path/to/file.py`: generates a new `.cover` file
indicating how many times each line of code was executed.
- 🐥 You can use the built-in python `pdb` module from CLI to step through a script in the interactive debugger:
- `$ python -m pdb path/to/file.py`: starts the script in pdb
- `(Pdb) step` steps to the next line
- `(Pdb) ll` prints the code surrounding your current line
- `(Pdb) pp <expression>` evaluates the expression and pretty-prints the result
- `(Pdb) where` prints the current callstack
- `(Pdb) help` prints the options available in the Python debugger
- `(Pdb) quit` quits the debugger
indicating how many times each line of code was executed.
- (_optional_) You can use [Thonny](https://thonny.org/) to write, step through,
visualize and debug small Python programs.
22 changes: 22 additions & 0 deletions 2_predictive_stepping/lesson_plan.md
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,28 @@ class: center, middle

class: center, middle

### Visualizing Programs: _[The `pdb` Module](https://docs.python.org/3/library/pdb.html)_

_(live demo)_

- `$ python -m pdb path/to/file.py`

- `(Pdb) step` steps to the next line

- `(Pdb) pp <expression>` pretty prints the variable's value to the console

- `(Pdb) ll` prints the current code level

- `(Pdb) quit`

???

`python -m pdb 2_predictive_stepping/examples/08_while_loops.py`

---

class: center, middle

### Visualizing Programs: _[VSCode Debugger](https://code.visualstudio.com/docs/python/debugging)_

_(live demo)_
Expand Down
Binary file added 3_documenting_and_testing/.assets/black_box.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added 3_documenting_and_testing/.assets/flowchart.jpeg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added 3_documenting_and_testing/.assets/flowchart.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
18 changes: 5 additions & 13 deletions 3_documenting_and_testing/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,25 +39,17 @@ master all of the skills introduced in this workshop.
### Function Documentation

- 🥚 You can read docstrings to understand a function's _behavior_.
- 🥚 You can import a module from the Python REPL and use `help(file_name)` to
read it's generated documentation:
1. `cd` into the folder containing the file you want to study
1. `$ python`
1. `$ import file_name` _use the file name without `.py`!_
1. `$ help(file_name)`
- 🥚 You can print a file's _docstring_ to the console using:
- `$ python -m pydoc path/to/file.py`
- then type `q` to quit
- 🥚 You can distinguish between a function's **_behavior_**, **_strategy_** and
**_implementation_**.
- 🥚 You can write a clear and complete _docstring_ to describe a function's
_behavior_.
- 🥚 You can write 2-3 _doctests_ to informally demonstrate a function's
_behavior_. (white space matters in a docstring test case!)
- 🥚 You can import a module from the Python REPL and use
`doctest.testmod(file_name, verbose=True)` to run a module's doctests:
1. `cd` into the folder containing the file you want to study
1. `$ python`
1. `$ import doctest`
1. `$ import file_name` _use the file name without `.py`!_
1. `$ doctest.testmod(file_name, verbose=True)`
- 🥚 You can run a file's _doctests_ from the console:
- `$ python -m doctest -v path/to/file.py`

### Function Implementation

Expand Down
38 changes: 19 additions & 19 deletions 3_documenting_and_testing/code_review_checklist.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,25 @@ the details become a habit.
- [ ] The file names match the function
- [ ] Test file is named `/tests/test_file_name.py`

## Files

- [ ] The file names match the function
- [ ] Module header in each file
- [ ] Module docstring in each file

## Unit Tests

- [ ] The test class has a helpful name in PascalCase
- [ ] The test class has a docstring
- Each unit test has
- [ ] A helpful name
- [ ] A clear docstring
- [ ] Only one assertion
- [ ] All tests pass
- [ ] (challenge) Tests for defensive assertions
- [ ] (challenge) Tests for many boundary cases
- [ ] (challenge) Includes black-box and glass-box tests

## Function Docstring

- [ ] behavior description
Expand All @@ -30,22 +49,3 @@ the details become a habit.
- [ ] Comments explain the strategy (if necessary)
- [ ] There are type annotations
- [ ] (challenge) The code includes defensive assertions

## Unit Test Suite

- [ ] The test class has a helpful name in PascalCase
- [ ] The test class has a docstring
- Each unit test has
- [ ] A helpful name
- [ ] A clear docstring
- [ ] Only one assertion
- [ ] All tests pass
- [ ] (challenge) Tests for defensive assertions
- [ ] (challenge) Tests for many boundary cases
- [ ] (challenge) Includes black-box and glass-box tests

## Files

- [ ] The file names match the function
- [ ] Module header in each file
- [ ] Module docstring in each file
26 changes: 13 additions & 13 deletions 3_documenting_and_testing/examples/fibonacci_list.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,28 +9,28 @@
# --- before documenting and testing ---


def mystery_0(a):
if a == 0:
return []
if a == 1:
return [0]
if a == 2:
return [0, 1]
# def mystery_0(a):
# if a == 0:
# return []
# if a == 1:
# return [0]
# if a == 2:
# return [0, 1]

b = [0, 1]
for c in range(2, a):
b.append(b[-1] + b[-2])
return b
# b = [0, 1]
# for c in range(2, a):
# b.append(b[-1] + b[-2])
# return b


# --- after documenting and testing ---


def fibonacci_list(sequence_length: int) -> list[int]:
"""generates a list containing the first n numbers of the Fibonacci sequence
"""Generates a list containing the first n numbers of the Fibonacci sequence.
Parameters:
sequence_length: int, greater than or equal to zero
sequence_length: int, greater than or equal to zero
Returns -> list[int] with the first n numbers of the Fibonacci sequence
Expand Down
Loading

0 comments on commit fb8aedc

Please sign in to comment.