A Python Enhancement Proposal (PEP) is a design document providing information to the Python community, or describing a new feature for Python or its processes or environment
09.12.2022
Example
n!=n⋅(n−1)⋅(n−2)⋅...⋅2⋅1=n⋅(n−1)!
# this file is named factorial.pydeffactorial(n):if n < 2:
return1return n * factorial(n - 1)
print(f"value is: {factorial(4)}")
print(f"value is: {factorial('4')}")
(pre-commits-python-example-py3.11):~/good-scientific-software-for-free-python-demo(main)$ python factorial.py
value is: 24
Traceback (most recent call last):
File "/home/sunnivin/NGI/slides/good-scientific-software-for-free/good-scientific-software-for-free-python-demo/factorial.py", line 14, in <module>
print(f"value is: {factorial('4')}")
^^^^^^^^^^^^^^
File "/home/sunnivin/NGI/slides/good-scientific-software-for-free/good-scientific-software-for-free-python-demo/factorial.py", line 8, in factorial
if n < 2:
^^^^^
TypeError: '<'not supported between instances of 'str'and'int'
09.12.2022
Type hints
Update function to include type hints
# this file is named factorial.pydeffactorial(n: int) -> int:if n < 2:
return1return n * factorial(n - 1)
print(f"value is: {factorial(4)}")
print(f"value is: {factorial(4.5)}")
(pre-commits-python-example-py3.11):~/good-scientific-software-for-free-python-demo(main)$ python factorial.py
value is: 24
value is: 39.375
09.12.2022
Type hints
Was this pythonically correct?
(pre-commits-python-example-py3.11):~/good-scientific-software-for-free-python-demo(main)$ python factorial.py
value is: 24
value is: 39.375
Check with mypy
(pre-commits-python-example-py3.11):~/good-scientific-software-for-free-python-demo(main)$ mypy factorial.py
factorial.py:14: error: Argument 1 to "factorial" has incompatible type"float"; expected "int" [arg-type]
Found 1 error in1 file (checked 1 source file)
# Correct: aligned with opening delimiter.
foo = long_function_name(var_one, var_two,
var_three, var_four)
# Wrong: arguments on first line forbidden when not using vertical alignment.
foo = long_function_name(var_one, var_two,
var_three, var_four)
Never use the characters ‘l’ (lowercase letter el), ‘O’ (uppercase letter > oh), or ‘I’ (uppercase letter eye) as single character variable names.
In some fonts, these characters are indistinguishable from the numerals one and zero. When tempted to use ‘l’, use ‘L’ instead.
l : string = "my_string"1
L : float = 4.5
09.12.2022
Style guide
The following code can run correctly, but is not correctly formatted according to PEP8
# this file is named factorial.pyfrom pathlib import Path
import math
l = 4deffactorial(
n: int) -> int :if n < 2:
return1return n * factorial(n - 1)
print(f"value is: {factorial(l)}")
import math
from pathlib import Path
l = 4deffactorial(
n: int) -> int :if n < 2:
return1return n * factorial(n - 1)
print(f"value is: {factorial(l)}")
09.12.2022
Flake8
(pre-commits-python-example-py3.11):~/good-scientific-software-for-free-python-demo(main)$ flake8 factorial.py
factorial.py:1:1: F401 'math' imported but unused
factorial.py:2:1: F401 'pathlib.Path' imported but unused
factorial.py:4:1: E741 ambiguous variable name 'l'
# this file is named factorial.py
my_variable = 4deffactorial(
n: int) -> int :if n < 2:
return1return n * factorial(n - 1)
print(f"value is: {factorial(my_variable)}")
09.12.2022
Black
(pre-commits-python-example-py3.11):~/good-scientific-software-for-free-python-demo(main)$ black factorial.py
reformatted factorial.py
All done!
1 file reformatted.
# this file is named factorial.py
my_variable = 4deffactorial(n: int) -> int:if n < 2:
return1return n * factorial(n - 1)
print(f"value is: {factorial(my_variable)}")
_footer: "![width:90 height:40](figures/logo/NGI/NGI_logo_transparent.gif) *Figure credit: [Ali Bati](http://www.alibati.com/horse)* "
# What is a good *enough* scientific model?
- Use something simplified to learn about the real world
![bg right w:400 h:350](figures/illustrations/horse.png)
---
# What is a good *enough* code?
- Use something simplified **to write a simplified code** to learn something about the real world
![bg right w:400 h:350](figures/illustrations/horse.png)
---
## "free" quality code
- Use type hints
- (Try) to follow the Python Enhancement Proposals (PEPs)
<!-- _class: split-text-image
# The PEPs
<div class=ldiv>
A Python Enhancement Proposal [(PEP)](https://peps.python.org/pep-0000/) is a design document for python code
## Why should **you** care about the PEPs?
<span style="color:#F5F5F5">
- *Code is read more often then it is written* - Guido Van Rossum
- Standardization
- Automation
</span>
</div>
<div class=rdiv>
# The PEPs
<div class=ldiv>
A Python Enhancement Proposal [(PEP)](https://peps.python.org/pep-0000/) is a design document for python code
## Why should **you** care about the PEPs?
- *Code is read more often then it is written* - Guido Van Rossum
- Standardization
- Automation
</div>
<div class=rdiv>
![w:400 h:425](figures/illustrations/dependency.png)
*Image credit: [xkcd](https://xkcd.com/2347/)*
</div>
---
<!-- _class: split-text-image
# The *zen* of scientific programming
<div class=ldiv>
##
##
##
- Find the right tool for the right job
</div>
<div class=rdiv>
##
![w:450 h:325](figures/illustrations/development_speed_quick_hacks.png)
*Credit: Radovan Bast*
</div>