Skip to content

Commit fa33b8c

Browse files
authored
Merge pull request ssciwr#2 from iulusoy/toc-linter
Toc linter
2 parents 9f5a5b1 + 70b8561 commit fa33b8c

File tree

6 files changed

+129
-1
lines changed

6 files changed

+129
-1
lines changed

Diff for: .flake8

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
[flake8]
2+
ignore = E303, E305
3+
max-line-length = 120

Diff for: PEP_recommendations/README.md renamed to Material_Part1_PEP/README.md

+1
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,7 @@ Make use of indentation when using continuation lines:
9999
def some_other():
100100
pass
101101

102+
102103
1. Encoding: The source file should always use UTF-8 encoding, and preferably without non-ASCII characters. It also makes your code more general if you use English words for identifiers (function names etc.).
103104

104105
1. Imports should be on separate lines:

Diff for: Material_Part2_Linter/README.md

+90
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
# Linter
2+
A code linter is there to highlight issues with the code that do not conform with a style guide, and that may lead to errors when interpreted or compiled. For python, a code linter makes sure that the code conforms with PEP 8 standards.
3+
4+
5+
## Flake8
6+
We will be using [flake8](https://flake8.pycqa.org/en/latest/), a popular linter for Python code.
7+
8+
First, you need to install flake8. You can do so by typing
9+
```
10+
pip install flake8
11+
```
12+
in your terminal. All of this will be demonstrated in the course. You can then invoke the linter using
13+
```
14+
flake8 path/to/code/to/check.py
15+
# or
16+
flake8 path/to/code/
17+
```
18+
in your terminal.
19+
20+
## Flake8 messages
21+
Flake8 will return error messages if your code does not adhere to the styling recommendation. You can find a list of all error codes [here](https://flake8.pycqa.org/en/latest/user/error-codes.html).
22+
23+
If you run flake8 on `example1.py`, you will get a list of error messages such as
24+
```
25+
Material_Part2_Linter/example1.py:6:1: E303 too many blank lines (3)
26+
Material_Part2_Linter/example1.py:7:1: E302 expected 2 blank lines, found 3
27+
Material_Part2_Linter/example1.py:7:65: E251 unexpected spaces around keyword / parameter equals
28+
Material_Part2_Linter/example1.py:7:67: E251 unexpected spaces around keyword / parameter equals
29+
Material_Part2_Linter/example1.py:11:80: E501 line too long (166 > 79 characters)
30+
Material_Part2_Linter/example1.py:27:11: E225 missing whitespace around operator
31+
Material_Part2_Linter/example1.py:32:18: W291 trailing whitespace
32+
Material_Part2_Linter/example1.py:34:1: E305 expected 2 blank lines after class or function definition, found 1
33+
Material_Part2_Linter/example1.py:34:12: E225 missing whitespace around operator
34+
Material_Part2_Linter/example1.py:36:41: W292 no newline at end of file
35+
```
36+
Flake8 tells you which line the error occurs (for example, line 6) and at which position in that line (for example, position 1), and then report the error code (E303) and the error type (too many blank lines - in the example it also tells that it found (3) blank lines).
37+
38+
Armed with this information, you can then navigate to the position in your code and correct the issues.
39+
40+
You can also select to report specific errors only:
41+
```
42+
flake8 --select E305 Material_Part2_Linter/example1.py
43+
```
44+
- this will return
45+
```
46+
Material_Part2_Linter/example1.py:32:1: E305 expected 2 blank lines after class or function definition, found 1
47+
```
48+
Or you can ask flake8 to ignore specific errors:
49+
```
50+
flake8 --ignore E305 Material_Part2_Linter/example1.py
51+
```
52+
This will report all errors other than E305. More than one error code is ignored using
53+
```
54+
flake8 --ignore E303,E305 Material_Part2_Linter/example1.py
55+
```
56+
Try out all the commands and familiarize yourself with the output.
57+
58+
## Configuring flake8
59+
In some cases, you may want to ignore flake8 errors or set limits differently, for example if you want to increase your line length to 120. You can reconfiger flake8 in a [number of different ways](https://flake8.pycqa.org/en/latest/user/configuration.html), and we will be using a flake8 configuration file.
60+
61+
In order to do so, place a `.flake8` file in the root of your folder. The top of the file contains the line `[flake8]` - see [the example](../.flake8). A list of ignored errors can be included following the `ignore` option.
62+
63+
If you want to ignore additional errors to the ones specified in your flake8 configuration file, use `extend-ignore` (instead of `ignore`) when invoking flake8 (just using `ignore` will overwrite the specifications in your .flake8 configuration).
64+
```
65+
flake8 --extend-ignore E305 Material_Part2_Linter/example1.py
66+
```
67+
68+
A good `.flake8` file will look like this
69+
```
70+
[flake8]
71+
extend-ignore = E203
72+
exclude =
73+
.git,
74+
__pycache__,
75+
docs/source/conf.py,
76+
old,
77+
build,
78+
dist
79+
max-complexity = 10
80+
```
81+
using `extend-ignore` not to overwrite previously defined options, and excluding directories for the linter to parse - so you can invoke flake8 on your complete package / source folder instead of selecting each file manually:
82+
```
83+
flake8 Material_Part1_PEP
84+
```
85+
**Task: Run the linter on your previously formatted code to see if you have missed any deviations in code style or obvious bugs.**
86+
87+
## Excurse: Flake8 for notebooks
88+
89+
90+

Diff for: Material_Part2_Linter/example1.py

+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
import os
2+
import glob
3+
4+
5+
6+
# find all png files in a folder
7+
def find_files(path=None, pattern="*.png", recursive=True, limit = 20) -> list:
8+
"""Find image files on the file system
9+
10+
:param path:
11+
The base directory where we are looking for the images. Defaults to None, which uses the XDG data directory if set or the current working directory otherwise.
12+
:param pattern:
13+
The naming pattern that the filename should match. Defaults to
14+
"*.png". Can be used to allow other patterns or to only include
15+
specific prefixes or suffixes.
16+
:param recursive:
17+
Whether to recurse into subdirectories.
18+
:param limit:
19+
The maximum number of images to be found. Defaults to 20.
20+
To return all images, set to None.
21+
"""
22+
if path is None:
23+
path = os.environ.get("XDG_DATA_HOME", ".")
24+
25+
result=list(glob.glob(f"{path}/{pattern}", recursive=recursive))
26+
27+
if limit is not None:
28+
result = result[:limit]
29+
30+
return result
31+
32+
if __name__=="__main__":
33+
list = find_files(path="./data/")
34+
print("Found files {}".format(list))

Diff for: README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ Course date: Nov 8th 2022, 9:00AM - 1:00PM
2828

2929
## Course content
3030

31-
1. PEP recommendations
31+
1. [PEP recommendations](Material_Part1_PEP/PEP_recommendations/README.md)
3232
1. Linting
3333
1. Code formatting
3434
1. Write better code: Examples

Diff for: data/IMG_2808.png

1.34 MB
Loading

0 commit comments

Comments
 (0)