Skip to content

Commit

Permalink
Can check Python style
Browse files Browse the repository at this point in the history
  • Loading branch information
richelbilderbeek committed Oct 10, 2023
1 parent 56867b3 commit ee2ef82
Show file tree
Hide file tree
Showing 3 changed files with 77 additions and 9 deletions.
1 change: 1 addition & 0 deletions lesson_plans/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@ day_3.html
day_3.ipynb
day_3.tex
day_3.pdf
temp_ruff_results.txt
20 changes: 20 additions & 0 deletions lesson_plans/check_python_style.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
#!/bin/bash
#
#

# Ignore the first ruff error
ruff day_3.qmd > temp_ruff_results.txt 2>&1

last_line=$(cat temp_ruff_results.txt | tail -n 1)
# echo "last_line: ${last_line}"
n_errors_plus_one=$(echo "${last_line}" | cut -d ' ' -f 2)
# echo "Number of errors plus one: ${n_errors_plus_one}"
n_errors=$((n_errors_plus_one-1))
echo "Number of errors: ${n_errors}"

exit ${n_errors}

# FAILS: will still always give an error
# Remove the part between '---'
#tail -n +11 day_3.qmd > temp_day_3.qmd
#ruff temp_day_3.qmd
65 changes: 56 additions & 9 deletions lesson_plans/day_3.qmd
Original file line number Diff line number Diff line change
Expand Up @@ -78,18 +78,65 @@ assert denominator != 0.0
value = numerator / denominator
```

Use assert extensively @sutter2004cpp@stroustrup2018cpp@mcconnell2004code@liberty2002sams@lakos1996large.
Use assert extensively @sutter2004cpp@stroustrup2018cpp@mcconnell2004code
@liberty2002sams@lakos1996large.

## `assert` references 1/2

* @sutter2004cpp Chapter 68: '
Assert liberally to document internal assumptions and invariants'
* @stroustrup2018cpp (3rd edition) Advice 24.5.18:
'Explicitly express preconditions, postconditions,
and other assertions as assertions'
* @mcconnell2004code Chapter 8.2:
'Use assertions to document and verify preconditions and postconditions'
* @mcconnell2004code Chapter 8.2:
'Use assertions for conditions that should never occur'.
* @liberty2002sams Chapter 'assert()': 'Use assert freely'

## `assert` references
## `assert` references 2/2

* @sutter2004cpp Chapter 68: 'Assert liberally to document internal assumptions and invariants'
* @stroustrup2018cpp (3rd edition) Advice 24.5.18: 'Explicitly express preconditions, postconditions, and other assertions as assertions'
* @mcconnell2004code Chapter 8.2: 'Use assertions to document and verify preconditions and postconditions'
* @mcconnell2004code Chapter 8.2: 'Use assertions for conditions that should never occur'.
* @liberty2002sams Chapter 'assert()': 'Use assert freely'
* @lakos1996largeChapter 2.6: 'The use of assert statements can help to document the assumptions you make when implementing your code
* @stroustrup2018cpp (4th edition) page 884: '[13] Use static_assert() and assert() extensively'
* @lakos1996large Chapter 2.6:
'The use of assert statements can help to document the assumptions
you make when implementing your code
* @stroustrup2018cpp (4th edition) page 884:
'[13] Use static_assert() and assert() extensively'

## Problem

```{python}
#| echo: true
#| eval: true
a = 3
b = 4
# Calculate the Euclidean distance
# using Pythagoras' theorem
c = ((a * a) + (b * b)) ** 0.5
```

How to express ourselves in code?

## Solution

```{python}
#| echo: false
#| eval: true
def calc_euclidian_distance(a, b):
return ((a * a) + (b * b)) ** 0.5
```

```{python}
#| echo: true
#| eval: true
a = 3
b = 4
c = calc_euclidian_distance(a, b)
```

We use a function.

## Break

![](forgetting.png)

## References

0 comments on commit ee2ef82

Please sign in to comment.