Skip to content

Commit 524da27

Browse files
authored
black (ssciwr#3)
* black * revert changes on example1
1 parent fa33b8c commit 524da27

File tree

3 files changed

+73
-2
lines changed

3 files changed

+73
-2
lines changed

Diff for: Material_Part3_Formatter/README.md

+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
# Code formatter
2+
A code formatter takes care of formatting your code so that it adheres to the styling standards. For Python, a popular code formatter is [black](https://black.readthedocs.io/en/stable/). Black is PEP 8 compliant but also adds some [own flavor](https://black.readthedocs.io/en/stable/the_black_code_style/current_style.html) to the code. Comparing code that has been formatted with black makes it easier to spot the differences. You can even try it out [online](https://black.vercel.app/?version=stable&state=_Td6WFoAAATm1rRGAgAhARYAAAB0L-Wj4ARsAnNdAD2IimZxl1N_WlkPinBFoXIfdFTaTVkGVeHShArYj9yPlDvwBA7LhGo8BvRQqDilPtgsfdKl-ha7EFp0Ma6lY_06IceKiVsJ3BpoICJM9wU1VJLD7l3qd5xTmo78LqThf9uibGWcWCD16LBOn0JK8rhhx_Gf2ClySDJtvm7zQJ1Z-Ipmv9D7I_zhjztfi2UTVsJp7917XToHBm2EoNZqyE8homtGskFIiif5EZthHQvvOj8S2gJx8_t_UpWp1ScpIsD_Xq83LX-B956I_EBIeNoGwZZPFC5zAIoMeiaC1jU-sdOHVucLJM_x-jkzMvK8Utdfvp9MMvKyTfb_BZoe0-FAc2ZVlXEpwYgJVAGdCXv3lQT4bpTXyBwDrDVrUeJDivSSwOvT8tlnuMrXoD1Sk2NZB5SHyNmZsfyAEqLALbUnhkX8hbt5U2yNQRDf1LQhuUIOii6k6H9wnDNRnBiQHUfzKfW1CLiThnuVFjlCxQhJ60u67n3EK38XxHkQdOocJXpBNO51E4-f9z2hj0EDTu_ScuqOiC9cI8qJ4grSZIOnnQLv9WPvmCzx5zib3JacesIxMVvZNQiljq_gL7udm1yeXQjENOrBWbfBEkv1P4izWeAysoJgZUhtZFwKFdoCGt2TXe3xQ-wVZFS5KoMPhGFDZGPKzpK15caQOnWobOHLKaL8eFA-qI44qZrMQ7sSLn04bYeenNR2Vxz7hvK0lJhkgKrpVfUnZrtF-e-ubeeUCThWus4jZbKlFBe2Kroz90Elij_UZBMFCcFo0CfIx5mGlrINrTJLhERszRMMDd39XsBDzpZIYV4TcG7HoMS_IF8aMAAAxI-5uTWXbUQAAY8F7QgAAP01Vc6xxGf7AgAAAAAEWVo=).
3+
4+
You first need to install black using
5+
```
6+
pip install black
7+
```
8+
You can then use black running it as
9+
```
10+
black Material_Part3_Formatter/example1.py
11+
```
12+
Please note that the formatter reformats the file in-place, that means, substituting the content of the original file!
13+
14+
Now you can check with flake8 if the file is compliant with PEP 8:
15+
```
16+
flake8 Material_Part3_Formatter/example1.py
17+
```
18+
You will notice that flake8 is not returning errors except a line length error for one of the comment lines: Note that black does not reformat comments other than inserting proper whitespace before and after the #.
19+
20+
## Black configuration
21+
Sometimes you only want to check what black would actually reformat. In order to do so, run
22+
```
23+
black Material_Part2_Formatter/example1.py --diff
24+
```
25+
or
26+
```
27+
black Material_Part2_Formatter/example1.py --diff --color
28+
```
29+
30+
## Black with jupyter notebooks
31+
32+
```
33+
pip install 'black[jupyter]'
34+
```

Diff for: Material_Part3_Formatter/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

+5-2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
[![Code style: black](https://img.shields.io/badge/code%20style-black-000000.svg)](https://github.com/psf/black)
2+
3+
14
# Python-best-practices-course
25
Material for the course "Python best practices", Scientific Software Center, Heidelberg University
36

@@ -28,8 +31,8 @@ Course date: Nov 8th 2022, 9:00AM - 1:00PM
2831

2932
## Course content
3033

31-
1. [PEP recommendations](Material_Part1_PEP/PEP_recommendations/README.md)
32-
1. Linting
34+
1. [PEP recommendations](Material_Part1_PEP/README.md)
35+
1. [Linting](Material_Part2_Linter/README.md)
3336
1. Code formatting
3437
1. Write better code: Examples
3538
1. Write better code: Pitfalls

0 commit comments

Comments
 (0)