├── .flake8 ├── README.md ├── assets ├── black.gif ├── complete.gif ├── flake8.gif └── isort.gif ├── data └── weight-height.csv ├── environment-dev.yml ├── environment.yml ├── pep8.sh └── src └── linear_regresion.py /.flake8: -------------------------------------------------------------------------------- 1 | [flake8] 2 | extended-ignore = E203 3 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Python Best Practices 2 | 3 | **How can you make your code shine with isort, Black, Flake8 and Pylint?** 4 | 5 | In this repository, I would like to show some guidelines and best practice tips 6 | on how to write Python code. 7 | 8 | We can use a simple deck of programs to get our [code 9 | styling](https://medium.com/semantixbr/how-to-make-your-code-shine-with-gitlab-ci-pipelines-48ade99192d1) 10 | done. We can use **isort** for sorting the library imports (yes, imports have a 11 | suggested order), we can check the existence of undesired artifacts using 12 | **Flake8** and **Pylint**, and we can keep the code within the same style using 13 | **Black**. 14 | 15 | Those tools can be configured to be [PEP8](https://peps.python.org/pep-0008/) 16 | compliant. **PEP8 — Python Enhancement Proposal**, is a style guide that 17 | provides guidelines and best practices suggestions on how to write Python code. 18 | 19 | ## Python tools 20 | 21 | * Importing sorting with [isort](https://pycqa.github.io/isort/) 22 | * Formatting with [Black](https://github.com/psf/black) 23 | * Linting with [Flake8](https://flake8.pycqa.org/en/latest/) 24 | * Bug and quality checking with [Pylint](https://www.pylint.org/) 25 | 26 | ## Project Setup 27 | 28 | ### Clone this repository 29 | 30 | ```shell 31 | (base)$: git clone git@github.com:mafda/python_best_practices.git 32 | (base)$: cd python_best_practices 33 | ``` 34 | 35 | ### Configure environment 36 | 37 | - Create the conda environment 38 | 39 | ```shell 40 | (base)$: conda env create -f environment.yml 41 | ``` 42 | 43 | - Activate the environment 44 | 45 | ```shell 46 | (base)$: conda activate linear-regression 47 | ``` 48 | 49 | - And run 50 | 51 | ```shell 52 | (linear-regression)$: sh pep8.sh 53 | ``` 54 | 55 | ## Use these tools in your project and clean your code! 56 | 57 | ```shell 58 | (base)$: conda env create -f environment-dev.yml 59 | (base)$: conda activate best-practices 60 | (best-practices)$: cd your-project 61 | ``` 62 | 63 | ### isort 64 | 65 | You could use `isort .` or `isort . --check-only` 66 | 67 | ![isort](assets/isort.gif) 68 | 69 | ### Black 70 | 71 | You could use `black --line-length 79 .` or `black --line-length 79 --check .` 72 | 73 | ![black](assets/black.gif) 74 | 75 | ### Flake8 76 | 77 | You could use `flake8 .` 78 | 79 | ![flake8](assets/flake8.gif) 80 | 81 | Fix errors: 82 | 83 | ![flake8](assets/complete.gif) 84 | 85 | ### PyLint 86 | 87 | You could use `find . -type f -name "*.py" | xargs pylint` 88 | 89 | --- 90 | 91 | made with 💙 by [mafda](https://mafda.github.io/) 92 | -------------------------------------------------------------------------------- /assets/black.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mafda/python_best_practices/9c845565b7dcdd9a0179160825bf4a62f46144f1/assets/black.gif -------------------------------------------------------------------------------- /assets/complete.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mafda/python_best_practices/9c845565b7dcdd9a0179160825bf4a62f46144f1/assets/complete.gif -------------------------------------------------------------------------------- /assets/flake8.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mafda/python_best_practices/9c845565b7dcdd9a0179160825bf4a62f46144f1/assets/flake8.gif -------------------------------------------------------------------------------- /assets/isort.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/mafda/python_best_practices/9c845565b7dcdd9a0179160825bf4a62f46144f1/assets/isort.gif -------------------------------------------------------------------------------- /environment-dev.yml: -------------------------------------------------------------------------------- 1 | name: best-practices 2 | channels: 3 | - conda-forge 4 | dependencies: 5 | - black=22.6.0 6 | - flake8=4.0.1 7 | - isort=5.10.1 8 | - pylint=2.14.4 9 | -------------------------------------------------------------------------------- /environment.yml: -------------------------------------------------------------------------------- 1 | name: linear-regression 2 | channels: 3 | - conda-forge 4 | dependencies: 5 | - black=22.6.0 6 | - flake8=4.0.1 7 | - isort=5.10.1 8 | - matplotlib=3.4.2 9 | - pandas=1.4.2 10 | - pylint=2.14.4 11 | - python=3.8 12 | - tensorflow-cpu=2.8.1 13 | -------------------------------------------------------------------------------- /pep8.sh: -------------------------------------------------------------------------------- 1 | #!/bin/bash 2 | 3 | clear 4 | 5 | for file in $(find ./src/ -type f -name "*.py") 6 | do 7 | isort ${file} 8 | black --line-length 79 ${file} 9 | flake8 ${file} 10 | done 11 | -------------------------------------------------------------------------------- /src/linear_regresion.py: -------------------------------------------------------------------------------- 1 | """Linear regression is a simple machine learning algorithm. 2 | """ 3 | 4 | import matplotlib.pyplot as plt 5 | import pandas as pd 6 | from tensorflow.keras.layers import Dense 7 | from tensorflow.keras.models import Sequential 8 | from tensorflow.keras.optimizers import Adam 9 | 10 | 11 | def main(): 12 | """Main function to solve a linear regression problem.""" 13 | weight_height_df = pd.read_csv("data/weight-height.csv") 14 | weight_height_df.plot( 15 | kind="scatter", 16 | x="Height", 17 | y="Weight", 18 | title="Weight(lb) and Height(in) in adults", 19 | ) 20 | 21 | x_true = weight_height_df[["Height"]].values 22 | y_true = weight_height_df["Weight"].values 23 | 24 | model = Sequential() 25 | model.add(Dense(1, input_shape=(1,))) 26 | 27 | model.compile( 28 | Adam(learning_rate=0.6), 29 | loss="mean_squared_error", 30 | metrics=["mse"], 31 | ) 32 | 33 | history = model.fit(x_true, y_true, epochs=50, verbose=1) 34 | history.history.keys() 35 | plt.plot(history.history["mse"]) 36 | 37 | score = model.evaluate(x_true, y_true) 38 | print("Test score:", score) 39 | 40 | y_pred = model.predict(x_true) 41 | weight_height_df.plot( 42 | kind="scatter", 43 | x="Height", 44 | y="Weight", 45 | title="Weight and Height in adults", 46 | ) 47 | 48 | plt.plot(x_true, y_pred, color="red") 49 | 50 | 51 | if __name__ == "__main__": 52 | main() 53 | --------------------------------------------------------------------------------